# trouble with function

This is a discussion on trouble with function within the C Programming forums, part of the General Programming Boards category; Hi, please help, I am having a trouble with first function get_total_pay, I am asking for weekly pay and gross ...

1. ## trouble with function

Hi, please help, I am having a trouble with first function get_total_pay, I am asking for weekly pay and gross sales for that week to calculate and fill total_pay array, but program stops on first line and exit . Can anyone explain me what I am doing wrong.
Thank you.

Code:
```#include<stdio.h>

int total_pay[]={0};
int weekly_salary;
int gross;
int A=0, B=0, C=0; D=0; E=0; F=0; G=0; H=0; I=0;
int i;

void get_total_pay(void)
{

printf("\nWeekly salary:");
scanf("%i",weekly_salary);

printf("Gross sales:");
scanf("%i",gross);

total_pay[i]=weekly_salary+(gross*0.009);

}

void get_ranges(int y)
{

for(i=0; i<y; ++i)
{
if(total_pay[i]>=200&&total_pay[i]<=299)
++A;
else if(total_pay[i]>=300&&total_pay[i]<=399)
++B;
else if(total_pay[i]>=400&&total_pay[i]<=499)
++C;
else if(total_pay[i]>=500&&total_pay[i]<=599)
++D;
else if(total_pay[i]>=600&&total_pay[i]<=699)
++E;
else if(total_pay[i]>=700&&total_pay[i]<=799)
++F;
else if(total_pay[i]>=800&&total_pay[i]<=899)
++G;
else if(total_pay[i]>=900&&total_pay[i] <=999)
++H;
else
++I;
}

}

void print_ranges(void)
{
printf("Ranges   #of salesperson's\n");
printf("==========================\n");
printf("  A\t%i",A);
printf("  B\t%i",B);
printf("  C\t%i",C);
printf("  D\t%i",D);
printf("  E\t%i",E);
printf("  F\t%i",F);
printf("  G\t%i",G);
printf("  H\t%i",H);
printf("  I\t%i",I);

}

main()
{
int Done=1;
int salesperson=0;

while(Done)
{
++salesperson;

printf("Entering data for salesperson %i",salesperson);

get_total_pay();

printf("1- next salesperson\n0- exit");
scanf("%i", &Done);
}

get_ranges(salesperson);
print_ranges();

}```

2. scanf requires pointers to where the data should be stored (as in &weekly_salary).

3. OMG , thank you very much tabstop.

4. Code:
`int A=0, B=0, C=0; D=0; E=0; F=0; G=0; H=0; I=0;`
This line should not compile - you have semicolons where you should have commas.

--
Mats

5. Thank you , after scanf was fixed, I noticed this mistake and it also fixed it.
Thank you very much.

6. DEVELOPMENT TIP #1

You should be writing in smaller steps and making sure everything works before you add more code. Doing anything else is a bad habit which will give you a drooping mouth and wringing hands eventually.

7. why dont you make a structure and pass it to the function as a ptr should make stuff much easier and better than global variables.

8. Thank you for your advise MK27. Lolguy the problem is that we did not learn structures yet, but anyway thank you for advise, I appriciate it.

9. Perhaps your could indentation could have been done much better?

-ssharish

10. I am breaking my head by trying to understand WHAT TO DO TO MAKE IT WORK. I must be do not understand functions as I need to. my function get_ranges must count A's B's ...ranges, and to print them I wrote another function- IT DOESN'T WORK and I don't see why, Here is what I made so far:
Code:
```#include<stdio.h>

int total_pay[50];
int weekly_salary[50];
int gross[50];
int A, B, C, D, E, F, G, H, I;
int i, salesperson;

void get_total_pay(void)
{
int Done=1;
int salesperson=0;

while(Done)
{
++salesperson;

printf("Entering data for salesperson %i",salesperson);

printf("\nWeekly salary:");
scanf("%i",&weekly_salary[salesperson]);

printf("Gross sales:");
scanf("%i",&gross[salesperson]);

printf("1- next salesperson\n0- exit");
scanf("%i", &Done);
}
for(i=1; i<=salesperson; ++i)
{
total_pay[i]=weekly_salary[i]+(gross[i]*0.09);
}

}

void get_ranges(int total_pay[50], int salesperson)
{

for(i=1; i<=salesperson; ++i)
{

if(total_pay[i]>=200&&total_pay[i]<=299)
++A;
else if(total_pay[i]>=300&&total_pay[i]<=399)
++B;
else if(total_pay[i]>=400&&total_pay[i]<=499)
++C;
else if(total_pay[i]>=500&&total_pay[i]<=599)
++D;
else if(total_pay[i]>=600&&total_pay[i]<=699)
++E;
else if(total_pay[i]>=700&&total_pay[i]<=799)
++F;
else if(total_pay[i]>=800&&total_pay[i]<=899)
++G;
else if(total_pay[i]>=900&&total_pay[i]<=999)
++H;
else
++I;
}
}

void print_ranges(void)
{
printf("Ranges   #of salesperson's\n");
printf("==========================\n");
printf("  A\t\t%i\n",A);
printf("  B\t\t%i\n",B);
printf("  C\t\t%i\n",C);
printf("  D\t\t%i\n",D);
printf("  E\t\t%i\n",E);
printf("  F\t\t%i\n",F);
printf("  G\t\t%i\n",G);
printf("  H\t\t%i\n",H);
printf("  I\t\t%i\n",I);

}

main()
{

get_total_pay();

get_ranges(total_pay[50], salesperson);

print_ranges();

}```

11. Well, ok you have got two salesperson variables declared which iis in the function and the other one as global. The control will always look at the vriables which are local to that scope and then look at the global. Now in your case salesperson in the get_ranges is gonna be something juck since the varibale is not been decalred. Perhaps i should say that it should 0 since it is declared globally. So when you enter the get_ranges function the salesperson value would 0. If you want to get the value of salesperson then you might have to return the valye at the get_totalpay fuunction and store then in the global. And try using global variables. They are not stable and dangeours since it can be changes at any point from anyother functions!

I would see no problem with the get_total_pay function now!

-ssharish

12. Thank you, I wish I would have a teacher like you, your explanation very understandable and since I am learning not just a programming but an English also it very important to me.
I have another question, if an array declared in globals and I am using this array in a function do I need to pass the array to that function . Exemple: I have array total_pay declared in globals and I am using this array in a get_ranges function, do I have to pass it to function?
thank you.

13. I guess I answeared it to myself, no I don't need to, I did so and now program works works,

Wow finally my homework done, thanks to everyone who helped.

Popular pages Recent additions