Question on scanf();

This is a discussion on Question on scanf(); within the C Programming forums, part of the General Programming Boards category; Hello, comrades. I have a question on this useless program I wrote. Notice the line /*45*/ and /*46*/. This program ...

  1. #1
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26

    Question on scanf();

    Hello, comrades. I have a question on this useless program I wrote. Notice the line /*45*/ and /*46*/. This program didn't function correctly when I runned it with one scanf(), [which it should've worked?], however, when I wrote as below at /*46*/, [scanf() after scanf()], it worked, why is this so?

    Code:
    /* This was originally written to calculate my salary */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    main ()
    {
    	int wage;
    	int hours;
    	int days;
    	int subtotal;
    
    	int fee;
    	int total;
    	char choice;
    
    	/* Form here goes calculation of gross profit */
    
    	printf("How much do you get paid an hour? >$");
    	scanf("%d", &wage);
    	if(!wage) { 
    		printf("Input error... terminating the program.\n\a");
    		exit(1);
    	} 
    	
    	printf("How many hours do you work a day? > ");
    	scanf("%d", &hours);
    	if(!hours) { 
    		printf("Input error... terminating the program.\n\a");
    		exit(1);
    	}
    	
    	printf("How many days do you work in a month? > ");
    	scanf("%d", &days);
    	if(!days) { 
    		printf("Input error... terminating the program.\n\a");
    		exit(1);
    	}
    	
    	subtotal = wage * hours * days;
    
    	/* From here goes transportation fee calculation*/
    
    	printf("Do you use any public transportation? (y/n) > ");
    /*45*/	scanf("%c", &choice);
    /*46*/	scanf("%c", &choice);
    	
    	if(choice == 'y') {
    		printf("How much does it cost you in a day? >$");
    		scanf("%d", &fee);
    			if(!fee) { 
    				printf("Input error... terminating the program.\n\a");
    				exit(1);
    			} 
    	printf("Your salary is $%d\n", subtotal - fee * days);
    	
    
    	} else if(choice == 'n') {
    		printf("Your salary is $%d\n", subtotal);
    
    	} else { 
    		printf("Input error... terminating the program.\n\a");
    	}
    
    	return 0;
    }
    And also, as you probably have noticed, there are repetition of this below with defferent arguments, [!wage, !fee, !days, etc]. Is there any way to make this little more efficient?
    Code:
    if(!wage) { 
    		printf("Input error... terminating the program.\n\a");
    		exit(1);
    	}
    Btw: Hello! I'm new to this forum!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385 (read Option 2)

    Quote Originally Posted by http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1043372399&id=1043284385
    There are two problems with using scanf() to get a number:

    First, validation/error handling is poor. If the user enters 1234ZZ as their number, scanf() will read 1234 into the int variable, and leave the ZZ in the input buffer. Although the user has entered what appears to be an invalid number, the program hasn't actually noticed, and will continue processing as if all is well.
    In other words, your probably leaving the linebreak (\n) in the input buffer...

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    This is roughly how I tend to get a single character y/n answer:
    Code:
    #include <stdio.h>
    
    int main()
    {	
        char choice='x'; //Initialise no not y/n
        
        while(choice != 'y' && choice != 'n') //Loop until y or n entered
        {
           printf("Do you use any public transportation? (y/n) > ");
           choice=getchar();
           getchar(); //Nullify return char
        }
        
        printf("Choice: %c", choice);
        getchar();
    }

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Fine except getchar() return's an int , and see the FAQ about clearing the input stream (stdin in this case)... :P

  5. #5
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26
    I see.. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 07:48 AM
  4. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 09:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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