2 replies
please i need help on this c programming code, i am using struct array and pointer.

the problem is i can prompt the user for input and collect input but when i use for loop to print out the stored value that was key in by the user it displays wrong output

this is the code.

#include<stdio.h>
#include<string.h>
int main()
{
int i;
struct employee
{
struct personal_info
{
char name[20];
char id[20];
char phone_number[12];
int startdate;
}p;

struct salary_info
{
double basic_salary;
double hours_worked;

}s;
struct net
{
double netIncome;
}n
}emp[5];

struct employee *ptr;

ptr = emp;
printf("\t\tEMPLOYEE PERSONAL DATA FORM\n\n");
for(i = 0; i < 5; i++)
{
printf("\t\t EMPLOYEE %d Record \n", i +1);
printf("Enter Name %d :", i + 1);
scanf(" %s", &emp[i].p.name);

printf("Enter ID %d :", i + 1);
scanf(" %s", &emp[i].p.id);

printf("Enter Phone Number %d :", i + 1);
scanf(" %s", &emp[i].p.phone_number);

printf("Enter Start Date %d :", i + 1);
scanf(" %d", &emp[i].p.startdate);

printf("Enter Basic Salary %d :", i + 1);
scanf(" %f", &emp[i].s.basic_salary);

printf("Enter Hours Worked %d :", i + 1);
scanf(" %d", &emp[i].s.hours_worked);

printf("\n");
printf("\n");

}

for(i = 0; i < 5; i++)
{
if(ptr->s.hours_worked <= 20)
{
ptr->n.netIncome = (ptr->s.hours_worked * 3.5)-(0.11+0.03);
}
else if(ptr->s.hours_worked > 20)
{
ptr->n.netIncome = ((ptr->s.hours_worked)* 4.2)-(0.11+0.03);
}
ptr++;
}

// SOME THING IS WRONG THIS FOR LOOP PLS I NEED HELP

printf("\t\t PRINTING OUT EMPLOYEE INFORMATION");
printf("\n\n\n");

for(i = 0; i < 5; i++)
{
printf("\t\t Employe number %d information", i+1);
printf("\n");
printf("\t\t Employee Basic Salary... %d", ptr->s.basic_salary);
printf("\n");
printf("\t\t Employee Hours Worked... %d", ptr->s.hours_worked);
printf("\n");
printf("\t\t Employee NetIncome... %d", ptr->n.netIncome);

ptr++;
}

return 0;

}
#programming
  • Profile picture of the author SoftwareGeek
    The datatypes that you have declared for the variables in your struct and the format strings that you use in scanf and printf do not match. When you access the struct using pointers, the datatypes have to match. If they do not match, the system will read incorrect number of bytes from the memory.

    For example,

    double basic_salary;
    double hours_worked;

    then it is used like this,
    scanf(" %f", &emp[i].s.basic_salary);
    scanf(" %d", &emp[i].s.hours_worked);
    printf("\t\t Employee Basic Salary... %d", ptr->s.basic_salary);
    printf("\t\t Employee Hours Worked... %d", ptr->s.hours_worked);

    Change it to be the following,
    scanf(" %lf", &emp[i].s.basic_salary);
    scanf(" %lf", &emp[i].s.hours_worked);
    printf("\t\t Employee Basic Salary... %f", ptr->s.basic_salary);
    printf("\t\t Employee Hours Worked... %f", ptr->s.hours_worked);

    Notice the different format string. Make the similar change for "netIncome" as well.

    In general, it is good practice to eliminate all compiler warnings in the code. They usually give you warnings about these kinds of issues.
    {{ DiscussionBoard.errors[8778554].message }}
    • Profile picture of the author softwarewarden
      Also scanf("%s",...) seems to breaks at least on visual studio 2005 on space char ' '
      you can try this


      char *readline(char *message, int max_len)
      {
      char *buffer = (char *)malloc(max_len);
      int ch = 0;
      int counter = 0;
      printf(
      "%s", message);
      while (ch = getchar())
      {
      if ((counter == max_len) || ch == '\n')
      {
      buffer[counter] =
      '\0';
      break;
      }
      buffer[counter] = ch;
      counter++;
      }
      return buffer;
      }

      char *name = readline("Enter Enployee name: ", 128);

      and use these functions to convert the string to the correct type



      atoi Convert string to integer (function ) atof Convert string to double (function ) strtol Convert string to long integer (function )
      {{ DiscussionBoard.errors[8779673].message }}

Trending Topics