Function call

This is a discussion on Function call within the C Programming forums, part of the General Programming Boards category; Code: typedef struct{ int ID; char name[50]; double hor_total; }group; group salesman[SIZE]; void sale_menu(); void AddSalesMan(); int nextid(); void sale_menu(int ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Function call

    Code:
    typedef struct{
    		int ID;
    		char name[50];
    		double hor_total;
    	}group;
    	group salesman[SIZE];
    
    
    void sale_menu();
    void AddSalesMan();
    int nextid();
    
    void sale_menu(int menu)
    {
    	printf("Sales Processing System\n");
    	printf("-----------------------\n\n");
    	printf("1. Add Salesman Records\n");
    	printf("2. Reports Generation\n");
    	printf("3. Modify Salesman Records\n");
    	printf("4. Delete Salesman Records\n\n");
    	printf("0. Exit\n");
    	printf("Your choice: ");
    	scanf("%d", &menu);
    }
    
    void AddSalesMan()
    {
    	FILE *inSales;
    	int a, b;
    
    
    	if ( (inSales = fopen("sales.txt","a")) == NULL)
    		printf("Cannot open sales.txt file.");
    	else
    	{
    		fprintf(inSales,"\n");
    		for(a=0; a<SIZE; a++)
    		{
    			fprintf(inSales,"S%04d|",salesman[a].ID);
    
    
    			for (b=0; b<4; b++)
    			{
    				printf("Quarter %d: ",b+1);
    				scanf(" %lf", &sale[a][b]);
    				fprintf(inSales,"%lf|",sale[a][b]);
    			}	
    		}	
    	}
    	fclose(inSales);
    }
    
    
    int nextid(int currentId, int isReset)
    {
    	static int lastId = 0;
    	if (isReset)
    	{
    		lastId = currentId;
    	}
    	return ++lastId;
    	
    }
    
    
    
    
    // function main begins program executionvoid main()
    {
    	// file pointer for nextid.txt
    	FILE *readId;
    	// variable declaration
    	int a, b, c = 0, last, getMenu = 0; 
    	double ver_total = 0, max = 0; 
    	int id = 0, lastId, resetId = 0;
    
    
    	
    	// if readId(open nextid.txt) is NULL, display message
    	if ( (readId = fopen("nextid.txt","r")) == NULL)
    		printf("Cannot open nextid.txt file");
    	else
    	{
    		// read previous id numbering from nextid.txt
    		fscanf(readId,"%d",&lastId);
    		// Pass lastId and resetId arguments to nextid function call
    		nextid(lastId, resetId);
    	}
    	// close readId file pointer
    	fclose(readId);
    
    
    	// Initialize salesman id and salesman horizontal total value
    	for(a=0; a<SIZE; a++) 
    	{
    		salesman[a].ID = nextid(lastId, resetId);
    		salesman[a].hor_total = 0;
    	}
    
    
    	// call sale_menu function
    	sale_menu(getMenu);
    	
    	// if getMenu = 1, call AddSalesMan function
    	if (getMenu == 1)
    		AddSalesMan();
    }
    Why AddSalesMan() function cannot execute?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Because sale_menu() cannot update getMenu because it is passed by value, rather than by reference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    How to pass by reference?

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    By using pointers.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    600
    Make your prototype like this
    Code:
    void sale_menu(int* menu)
    and pass it in main like this
    Code:
    sale_menu(&getMenu);
    But then you will have to change your scanf in your function to
    Code:
    scanf("%d", menu);
    Read up on pointers
    Last edited by camel-man; 10-28-2012 at 05:39 PM.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int sale_menu();
    int report_menu();
    void QtrReport();
    
    
    int sale_menu()
    {
    	int menu;
    	printf("Sales Processing System\n");
    	printf("-----------------------\n\n");
    	printf("1. Add Salesman Records\n");
    	printf("2. Reports Generation\n");
    	printf("3. Modify Salesman Records\n");
    	printf("4. Delete Salesman Records\n\n");
    	printf("0. Exit\n");
    	printf("Your choice: ");
    	scanf("%d", &menu);
    	return menu;
    	printf("\n");
    }
    
    
    int report_menu()
    {
    	int report;
    	printf("Reports Generation\n");
    	printf("------------------\n\n");
    	printf("1. View Quarterly Sales Report\n");
    	printf("2. View Individual Salesman Commission\n\n");
    	printf("0. Return to Main Menu\n");
    	printf("Your choice: ");
    	scanf("%d", &report);
    	return report;
    }
    
    
    void QtrReport()
    {
    	printf("QtrReport\n");
    	
    }
    
    
    // function main begins program execution
    void main()
    {
    	// variable declaration
    	int getMenu = 0, getReport = 0; 
    	// call sale_menu function
    	getMenu = sale_menu();
    	printf("\n");
    	// if getMenu = 1, call AddSalesMan function
    	switch(getMenu)
    	{
    		case 1:
    			break;
    
    
    		case 2:
    			report_menu();
    			
    			switch (getReport = report_menu())
    			{
    				case 1:
    					QtrReport();
    					break;
    			}
    			break;
    	}
    
    
    	system("pause");
    	
    }
    select 2 then 1 two times to test it.

    Why view Quarterly Sales Report function need to insert 2 times in order to execute? Can solve the issue without using pointer?

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    How soon you forget things! main returns int, not void!

    Quote Originally Posted by ulti-killer View Post
    select 2 then 1 two times to test it.

    Why view Quarterly Sales Report function need to insert 2 times in order to execute? Can solve the issue without using pointer?
    How many times are you calling report_menu()?

    And you need pointers for scanf() and printf(), because printf technically takes a pointer to char as its first argument. I don't see anywhere else in your program that uses pointers. I don't understand what you are asking to "solve the issue without using pointer".

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    I change the main to int still the same result.

    I plan to use while loop to loop report_menu() until the user wants to stop the program.

    I mean the idea in post#5

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by ulti-killer
    Can solve the issue without using pointer?
    Quote Originally Posted by ulti-killer
    I mean the idea in post#5
    If you do not want to pass a pointer to the menu selection, i.e., to use an out parameter, then just return the menu selection as an int, i.e., change your function to have an int return type (and change the calling code accordingly).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    My post#6 is same as u mention but it doesn't work well.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Name:  output.jpg
Views: 123
Size:  59.2 KB

    I need to input 2 times on Report Generation in order to execute QtrReport function.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    That is because you call the report_menu function twice when you only needed to call it once.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Quote Originally Posted by laserlight View Post
    If you do not want to pass a pointer to the menu selection, i.e., to use an out parameter, then just return the menu selection as an int, i.e., change your function to have an int return type (and change the calling code accordingly).
    Is this overhead of passing the object by value will consume more memory?

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    1,099
    All objects are passed by value in C - a pointer is also a type of object. Typically int is 4-8 bytes and a pointer is 4-8 bytes. These objects can also be loaded directly into a CPU register, in which case the "memory consumption" of such an object is zero.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. How to call a function from a function pointer
    By Richardcavell in forum C Programming
    Replies: 9
    Last Post: 02-16-2011, 03:22 AM
  4. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  5. Replies: 2
    Last Post: 06-21-2005, 02:41 PM

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