storing in an array

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1209 Views - Last Post: 27 September 2012 - 07:37 PM Rate Topic: -----

#1 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

storing in an array

Posted 25 September 2012 - 06:31 PM

Hi I'm trying to figure out how to store the user input for each employee they enter in an array. For example the program asks for the number of employees and then asks for their name, hours worked, and pay rate. I can't seem to figure out how to store the employee info in an array every time it goes through the for loop. Can someone help with this?

Here is the program:

void main(void)
{
	/* Variable Declarations */
	/*-----------------------*/
	
	int number_of_employees;
	char first_name[30];
	char last_name[30];
	int x;
	float employee_hours;
	float employee_hourly_pay;
	
	printf("Welcome to the Payroll Calculator\n\n");
		
	do
	{
		printf("Enter the number of employees(1-10): ");
		scanf("%i", &number_of_employees);
		fflush(stdin);
		
		if(number_of_employees < 1 || number_of_employees > 10)
		{
			printf("\nError: Number of employees must be between 1 and 10, please re-enter!\n\n");
		}
	}while(number_of_employees < 1 || number_of_employees > 10);
	
	for(x = 1; x < number_of_employees; x++)
	{
		printf("\n\nEnter the name for employee #%i: ", x);
		scanf("%s %s", &first_name, &last_name);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", first_name, last_name);
		scanf("%.1f", &employee_hours);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s: ", first_name, last_name);
		scanf("%.2f", &employee_hourly_pay);
		fflush(stdin);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: storing in an array

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: storing in an array

Posted 25 September 2012 - 06:51 PM

Have you covered structs and/or classes in your class? If not yet, that's okay.

Anyway, in your code, where is your array for the hours and the pay? I see your character arrays for the name strings, but where is your array for the set of name strings?
Was This Post Helpful? 0
  • +
  • -

#3 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 25 September 2012 - 07:12 PM

We've briefly covered structs so I don't understand it that much yet, is that something I should be using? Or should I change it to float employee_hours[10], float employee_hourly_pay[10]?

For the char array should it look like this in the for loop:
[code]for(x = 1; x < number_of_employees; x++)
	{
		int i = 0;
		printf("\n\nEnter the name for employee #%i: ", x);
		scanf("%s %s", &first_name[i], &last_name[i]);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", first_name, last_name);
		scanf("%.1f", &employee_hours);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s: ", first_name, last_name);
		scanf("%.2f", &employee_hourly_pay);
		fflush(stdin);
		i++;
	}

This post has been edited by ferguson32: 25 September 2012 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: storing in an array

Posted 25 September 2012 - 07:25 PM

Yes, you should use structs. When you see yourself maintaining parallel arrays, it's a good indicator that you should be using an array of structs instead.

Of course, some teachers want their students to first go through the pain of trying to maintain parallel arrays in an earlier assignment to hammer in the value of have a struct that makes things so much easier. This is why I asked if your teacher has taught you structs yet, or not.

Anyway, let's say I wanted to collect planets and their albedos, the bad way to do it would be:
char name[8][20];
float albedo[8];

for(int i = 0; i < 8; i++)
{
    printf("Enter the planet name: ");
    sscanf("%s", name[i]);
    printf("Enter %s's albedo: ", name[i]);
    sscanf("%f", &albedo[i]);
}



The better way to do this would be:
typedef struct
{
    char name[20];
    float albedo;
} Planet;

Planet planets[8];

for(int i = 0; i < 8; i++)
{
    printf("Enter the planet name: ");
    sscanf("%s", planet[i].name);
    printf("Enter %s's albedo: ", planet[i].name);
    sscanf("%d", &planet[i].albedo);
}


Was This Post Helpful? 0
  • +
  • -

#5 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 25 September 2012 - 07:50 PM

Like this:

typedef struct {
	char name[30];
	float employee_hours;
	float employee_hourly_pay;
}employee

for(x = 1; x < number_of_employees; x++)
	{
		
		printf("\n\nEnter the name for employee #%i: ", x);
		scanf("%s", &employee.[i]name);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", name);
		scanf("%.1f", &employee_hours);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s: ", first_name, last_name);
		scanf("%.2f", &employee_hourly_pay);
		fflush(stdin);
		
	}

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: storing in an array

Posted 25 September 2012 - 10:21 PM

You are getting there... try compiling it and see if you can fix the compile errors and warnings on your own. One thing that I will point out is that you'll probably get a crash due to line 14, because of the mismatch between your format string and the actual parameters that you pass. If you are using GCC as your compiler, you should get a warning about it.

Feel free to post here again if you have questions as you try to get your code to compile and run.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: storing in an array

Posted 26 September 2012 - 02:46 AM

It's int main(), not void main()

Do NOT use fflush(stdin);, as it invokes undefined behavior.

for(x = 1; x < number_of_employees; x++)


Take out a piece of paper and write down a number for what you'd enter for number_of_employees. Under that, write the value of x for each iteration of the loop on a different line. Is that loop correct?
Was This Post Helpful? 0
  • +
  • -

#8 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 26 September 2012 - 05:08 AM

Ok this is what I put together I'm getting errors but not sure what to change. The first error is
the scanf line where it asks for the employees first name and last name, not sure what's wrong there. Here is the code:


#include <stdio.h>




void main(void)
{
	/* Variable Declarations */
	/*-----------------------*/
	
	int number_of_employees;
	int x;
	typedef struct 
	{
		char first_name[30];
		char last_name[30];
		float employee_hours[10];
		float employee_hourly_pay[10];
	}employee;
	
	
	printf("Welcome to the Payroll Calculator\n\n");
		
	do
	{
		printf("Enter the number of employees(1-10): ");
		scanf("%i", &number_of_employees);
		fflush(stdin);
		
		if(number_of_employees < 1 || number_of_employees > 10)
		{
			printf("\nError: Number of employees must be between 1 and 10, please re-enter!\n\n");
		}
	}while(number_of_employees < 1 || number_of_employees > 10);
	
	for(x = 1; x < number_of_employees; x++)
	{
		int i = 0;
		printf("\n\nEnter the name for employee #%i: ", x);
		scanf("%s %s", &employee.first_name[i], &employee.last_name[i]);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", first_name, last_name);
		scanf("%.1f", &employee.employee_hours[i]);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s: ", first_name, last_name);
		scanf("%.2f", &employee.employee_hourly_pay[i]);
		fflush(stdin);
		i++;
	}
}


JackOfAlltrades my professor has it set up that way so that's the way he wants main. And I have to use fflush(stdin) after all scanf lines or else points get deducted when I submit my assignment.
Was This Post Helpful? 0
  • +
  • -

#9 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 26 September 2012 - 08:35 AM

is the problem in the struct? Do I have it declared wrong?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: storing in an array

Posted 26 September 2012 - 09:06 AM

Look at my pseudo code and notice where I put the indexer.
Was This Post Helpful? 0
  • +
  • -

#11 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 26 September 2012 - 09:41 AM

Ok I have it like this now but it's still giving me errors. This first error is pointing at the first scanf line asking for the first and last name. The error says "parse error before 'employee'

for(x = 0; x < number_of_employees; x++)
	{
		int i = 1;
		printf("\n\nEnter the name for employee #%i: ", i);
		scanf("%s %s", &employee[x].first_name, &employee[x].last_name);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", employee[x].first_name, employee[x].last_name);
		scanf("%.1f", &employee[x].employee_hours);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s: ", employee[x].first_name, employee[x]last_name);
		scanf("%.2f", &employee[x].employee_hourly_pay[x]);
		fflush(stdin);
		i++;
	}

Was This Post Helpful? 0
  • +
  • -

#12 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 26 September 2012 - 09:53 AM

figured it out hopefully I can figure the rest of it out too!
Was This Post Helpful? 0
  • +
  • -

#13 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 27 September 2012 - 05:22 AM

Ok so I think what i've done so far is correct, but I keep getting errors of "compatibility types" and "invalid operands to binary *" in my for loop that I can't seem to figure out what to do. Here is the whole program :

#include <stdio.h>

float overtime_pay(float hourly_rate[10], float hours_worked[10])
{
	float overtime_pay[10];
	overtime_pay[10] = (hours_worked[10] - 40) * hourly_rate[10] * 1.5;
	return overtime_pay[10];
}

float federal_tax(float gross_pay[10])
{
	float federal_tax[10];
	federal_tax[10] = gross_pay[10] * .2;
	return federal_tax[10];
}

float net_pay(float gross_pay[10], float federal_tax[10])
{
	float net_pay[10];
	net_pay[10] = gross_pay[10] - federal_tax[10];
	return net_pay[10];
}

void main(void)
{
	/* Variable Declarations */
	/*-----------------------*/
	
	int number_of_employees;
	int x;
	float total_gross, total_fed, total_net;
	typedef struct 
	{
		char first_name[30];
		char last_name[30];
		float employee_hours[10];
		float employee_hourly_pay[10];
		float overtime_pay[10];
		float gross_pay[10];
		float federal_tax[10];
		float net_pay[10];
	}Employee;
		Employee employee[10];
	
	printf("Welcome to the Payroll Calculator\n\n");
		
	do
	{
		printf("Enter the number of employees(1-10): ");
		scanf("%i", &number_of_employees);
		fflush(stdin);
		
		if(number_of_employees < 1 || number_of_employees > 10)
		{
			printf("\nError: Number of employees must be between 1 and 10, please re-enter!\n\n");
		}
	}while(number_of_employees < 1 || number_of_employees > 10);
	
	for(x = 0; x < number_of_employees; x++)
	{	
		printf("\n\nEnter the name for employee #%i: ", x + 1);
		scanf("%s %s", &employee[x].first_name, &employee[x].last_name);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", employee[x].first_name, employee[x].last_name);
		scanf("%.1f", &employee[x].employee_hours);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s %s: ", employee[x].first_name, employee[x].last_name);
		scanf("%.2f", &employee[x].employee_hourly_pay);
		fflush(stdin);	
		
		if(employee[x].employee_hours > 40)
		{
			employee[x].overtime_pay = overtime_pay(employee[x].employee_hours, employee[x].employee_hourly_pay);
			employee[x].gross_pay = 40 * employee[x].employee_hours + employee[x].overtime_pay;
		}
		else
		{
			employee[x].gross_pay = employee[x].employee_hours * employee[x].employee_hourly_pay;
		}
		
		employee[x].federal_tax = federal_tax(employee[x].gross_pay);
		
		employee[x].net_pay = net_pay(employee[x].gross_pay, employee[x].federal_tax);
		
		total_gross = total_gross + employee[x].gross_pay;
		total_fed = total_fed + employee[x].federal_tax;
		total_net = total_net + employee[x].net_pay;
	} /* End for loop */
	
	
	
} /* End Main */

Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: storing in an array

Posted 27 September 2012 - 08:55 AM

Thank you for giving us the verbatim error string. (We really appreciate it. You don't know how many people try to "summarize" and end up sending us on wild goose chases.) Now to help us some more, can you indicate which lines the errors being reported on?

A few quick points though:
* void main(void) should be int main().
* If you have an array of 10 employees already, why do still have an array of 10 employee_hours in the struct?
Was This Post Helpful? 0
  • +
  • -

#15 ferguson32   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: storing in an array

Posted 27 September 2012 - 12:15 PM

I was able to fix the errors and get the program working. Now the only problem is I have a second for loop that is supposed to display the calculated gross pay, federal tax, and net pay for each employee but they all show up as 0.00

I'm not sure if there is something wrong with the calculations in the first for loop or what. Here is the whole program again:

float overtime_pay(float hourly_rate, float hours_worked)
{
	float overtime_pay;
	overtime_pay = (hours_worked - 40) * hourly_rate * 1.5;
	return overtime_pay;
}

float federal_tax(float gross_pay)
{
	float federal_tax;
	federal_tax = gross_pay * .2;
	return federal_tax;
}

float net_pay(float gross_pay, float federal_tax)
{
	float net_pay;
	net_pay = gross_pay - federal_tax;
	return net_pay;
}

int main()
{
	/* Variable Declarations */
	/*-----------------------*/
	
	int number_of_employees;
	int x, i;
	float total_gross, total_fed, total_net;
	typedef struct 
	{
		char first_name[30];
		char last_name[30];
		float employee_hours;
		float employee_hourly_pay;
		float overtime_pay;
		float gross_pay;
		float federal_tax;
		float net_pay;
	}Employee;
		Employee employee[10];
	
	printf("Welcome to the Payroll Calculator\n\n");
		
	do
	{
		printf("Enter the number of employees(1-10): ");
		scanf("%i", &number_of_employees);
		fflush(stdin);
		
		if(number_of_employees < 1 || number_of_employees > 10)
		{
			printf("\nError: Number of employees must be between 1 and 10, please re-enter!\n\n");
		}
	}while(number_of_employees < 1 || number_of_employees > 10);
	
	for(x = 0; x < number_of_employees; x++)
	{	
		printf("\n\nEnter the name for employee #%i: ", x + 1);
		scanf("%s %s", &employee[x].first_name, &employee[x].last_name);
		fflush(stdin);
		
		printf("Enter the number of hours for %s %s: ", employee[x].first_name, employee[x].last_name);
		scanf("%.1f", &employee[x].employee_hours);
		fflush(stdin);
		
		printf("Enter the hourly rate for %s %s: ", employee[x].first_name, employee[x].last_name);
		scanf("%.2f", &employee[x].employee_hourly_pay);
		fflush(stdin);	
		
		if(employee[x].employee_hours > 40)
		{
			employee[x].overtime_pay = overtime_pay(employee[x].employee_hours, employee[x].employee_hourly_pay);
			employee[x].gross_pay = 40 * employee[x].employee_hours + employee[x].overtime_pay;
		}
		else
		{
			employee[x].gross_pay = employee[x].employee_hours * employee[x].employee_hourly_pay;
		}
		
		employee[x].federal_tax = federal_tax(employee[x].gross_pay);
		
		employee[x].net_pay = net_pay(employee[x].gross_pay, employee[x].federal_tax);
		
		total_gross = total_gross + employee[x].gross_pay;
		total_fed = total_fed + employee[x].federal_tax;
		total_net = total_net + employee[x].net_pay;
	} /* End for loop */
	
	printf("\n\nPayroll Report\n");
	printf("    --------------\n\n");
	
	for(i = 0; i < number_of_employees; i++)
	{
		printf("%s %s\n\n", employee[i].first_name, employee[i].last_name);
		printf("Gross Pay: $ %.2f\n", employee[i].gross_pay);
		printf("Federal Tax: $ %.2f\n", employee[i].federal_tax);
		printf("Net Pay: $ %.2f\n\n", employee[i].net_pay);	
	}
	
	
	
} /* End Main */

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2