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. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    scanf requires pointers to where the data should be stored (as in &weekly_salary).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    OMG , thank you very much tabstop.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you , after scanf was fixed, I noticed this mistake and it also fixed it.
    Thank you very much.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    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. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Perhaps your could indentation could have been done much better?

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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 subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  2. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21