Thread: Simple question from a newbee

  1. #1
    Unregistered
    Guest

    Lightbulb Simple question from a newbee

    Having a problem when the user selects 1 and enters the info then presses 2 to display the info it is displaying garbage. I know it is because if the if statements, but I can't think of a way to resolve the problem. Any help would be appreciated.





    struct employee {

    float rate;
    float hours;
    float gross;
    float tax;
    float net;
    };
    #include<stdio.h>
    int menu();
    struct employee info(void);
    void display(struct employee);
    int main()
    {
    int sel;
    struct employee emp;
    sel = menu();
    if(sel== 1)
    emp = info(), main();
    if (sel == 2)
    struct employee emp;
    display (emp);


    return 0;
    }
    struct employee info(void)
    {
    float hours;
    float rate;
    float gross;
    float tax;
    float net;
    struct employee emp;
    printf("Enter employee's payrate: ");
    scanf("%f", &rate);
    printf("Enter employee's hours worked: ");
    scanf("%f", &hours);
    emp.hours = hours;
    emp.rate= rate;
    gross = hours * rate;
    emp.gross = gross;
    tax = gross * .21;
    emp.tax = tax;
    net = gross * .79;
    emp.net = net;
    return emp;
    }


    int menu()
    { int sel;

    puts("Menu\nPress 1 to input data\nPress 2 to display data\nPress 0 to Quit");
    scanf("%d", &sel);
    if (sel>2)
    puts("\aInvalid Selection"), menu();
    return sel;
    }

    void display(struct employee emp)

    {
    puts("Name SSN Gross Pay Taxes Net");
    printf("%6.2f %6.2f %6.2f", emp.gross, emp.tax, emp.net);

    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    for a start, whats with the comma.
    Code:
    if (sel>2) 
    puts("\aInvalid Selection"), menu(); 
    
    /* should be - dont forget braces */
    
    if (sel>2) 
    {
               puts("\aInvalid Selection"); 
                menu(); 
    }
    and please use code tags
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Secondly, don't do recursive calls to main(). Its bad practice.

    Use a switch() to determine the value of sel in main(). It will look better.

    this is your main(), tidied up (but not corrected):
    Code:
    int main(void)
    {
    	/*~~~~~~~~~~~~~~~~*/
    	int				sel;
    	struct employee emp;
    	/*~~~~~~~~~~~~~~~~*/
    
    	sel = menu();
    	if (sel == 1) 
    	{
    		emp = info();
    		main();
    	}
    	if (sel == 2)	
    	{
    		struct employee emp;  /* Whats going on here?! */
    	}
    
    	display(emp);
    
    	return 0;
    }
    Its clear that the if (sel ==2) bit is doing wrong! You cannot redeclare a varible (emp) here, and you wouldn't want to.

    This would be a better main():
    Code:
    int main(void)
    {
    	int sel;
    	struct employee emp;
    	while ((sel = menu()) != 0)
    	{
    		switch (sel)
    		{
    			case 1: emp = info(); break;
    			case 2: display(emp); break;
    			default: break;
    		}
    	}
    
    	return 0;
    }
    THe display() function prints results now, but I haven't checked it they are correct or not.
    Use code tags when posting code please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest

    Thumbs up

    Thanks for your help guys

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by bigtamscot
    for a start, whats with the comma.
    Code:
    if (sel>2) 
    puts("\aInvalid Selection"), menu();
    The comma is a way of splitting commands. Eg:
    Code:
    #include <stdio.h>
    int main(void)
    {
    	int	sel;
    
    	if (1) putchar('1'), putchar('2'),putchar('3'), putchar('\n');
    	else printf ("Never get here\n");
    	if (1) 
    	{
    		putchar('4');putchar('5');putchar('6');
    	}
    	else printf ("Never get here either\n");
    	return (0);
    }
    Personally, I don't *think* that the commas are used very often, except in for() statements, EG
    >for (i = 0, j = 0, i < 10; i++, j++)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (1) putchar('1'), putchar('2'),putchar('3'), putchar('\n');
    Which is what {} are supposed to be for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM