help with input and scanf

This is a discussion on help with input and scanf within the C Programming forums, part of the General Programming Boards category; Hi, I've been having a little trouble debugging my friends code here. At the last scanf while executing the program, ...

  1. #1
    kpw
    kpw is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    9

    Unhappy help with input and scanf

    Hi, I've been having a little trouble debugging my friends code here. At the last scanf while executing the program, it seems to just skip it with a enter stroke or something, and doesn't allow me to enter y or n. Any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok quick fix but far from perfect you shouldn't really use scanf.... better would be fgets() and use atof() and atoi() to convert string input to numbers.

    anyway:-

    Code:
    #include <stdio.h>
    
    /* change values */
    #define HALFDOLLARS .50
    #define QUARTERS    .25
    #define DIMES       .10
    #define NICKLES     .05
    #define PENNIES     .01
    
    int main(void)
    {
    	char response[10]={0};
    
    	while (response[0] != 'n') {
    		int halfdollars_num = 0,
    		quarters_num = 0,
    		dimes_num = 0,
    		nickles_num = 0,
    		pennies_num = 0;
    
    		double total_change = 0.0;
    
    		/* ask and enter the number of coins */
    		printf("\nEnter the number of Half-Dollars in the drawer: ");
    		scanf("%d", &halfdollars_num);
    
    		printf("\nEnter the number of quarters in the drawer: ");
    		scanf("%d", &quarters_num);
    
    		printf("\nEnter the number of dimes in the drawer: ");
    		scanf("%d", &dimes_num);
    
    		printf("\nEnter the number of nickles in the drawer: ");
    		scanf("%d", &nickles_num);
    
    		printf("\nEnter the number of pennies in the drawer: ");
    		scanf("%d", &pennies_num);
    
    		/* calculate */
    		total_change += (double)(quarters_num * QUARTERS);
    		total_change += (double)(halfdollars_num * HALFDOLLARS);
    		total_change += (double)(dimes_num * DIMES);
    		total_change += (double)(nickles_num * NICKLES);
    		total_change += (double)(pennies_num * PENNIES);
    
    		/* print total change */
    		printf("\nTotal change = %2.2f\n", total_change);		
    
    		/* ask if the user wants to do it again */
    		printf("\nDo you want to process another drawer? ");
    		scanf("%s", &response);
    	}
    	return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    Unhappy Cant use that

    Unfortunetly we havent learned that in my class and professor will not accept that. I think he wasnt scanf or getchar() this stinks. I know some other people in my class got it to work.

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    If you're looking for a quick fix, put;

    fflush(stdin);

    right before you ask about whether or not to do another drawer. This isn't portable, but it works on Dev-C++ (and I assume MSVC++)

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    :(

    Professor wont accept that either. It has to be either scanf ot getchar(). Do you think my look structure is wrong? or I have to do another type of a loop?

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Nope, your loop looks fine...

    If you can only use getchar() and scanf, then just put

    getchar();

    before you ask about the drawer....

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i only used scanf() in fixing your code...... why is it no good for you?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825

    Speculation

    Well, s_c, pure speculation, but this sounds like a really early assignment in a beginning class - maybe they can't use arrays or strings yet... Speculation, tho...

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok without the array code fixed!
    Code:
    #include <stdio.h>
    
    /* change values */
    #define HALFDOLLARS .50
    #define QUARTERS    .25
    #define DIMES       .10
    #define NICKLES     .05
    #define PENNIES     .01
    
    int main(void)
    {
    	char response=0;
    
    	while (response != 'n') {
    		int halfdollars_num = 0,
    		quarters_num = 0,
    		dimes_num = 0,
    		nickles_num = 0,
    		pennies_num = 0;
    
    		double total_change = 0.0;
    
    		/* ask and enter the number of coins */
    		printf("\nEnter the number of Half-Dollars in the drawer: ");
    		scanf("%d", &halfdollars_num);
    
    		printf("\nEnter the number of quarters in the drawer: ");
    		scanf("%d", &quarters_num);
    
    		printf("\nEnter the number of dimes in the drawer: ");
    		scanf("%d", &dimes_num);
    
    		printf("\nEnter the number of nickles in the drawer: ");
    		scanf("%d", &nickles_num);
    
    		printf("\nEnter the number of pennies in the drawer: ");
    		scanf("%d", &pennies_num);
    
    		/* calculate */
    		total_change += (double)(quarters_num * QUARTERS);
    		total_change += (double)(halfdollars_num * HALFDOLLARS);
    		total_change += (double)(dimes_num * DIMES);
    		total_change += (double)(nickles_num * NICKLES);
    		total_change += (double)(pennies_num * PENNIES);
    
    		/* print total change */
    		printf("\nTotal change = %2.2f\n", total_change);		
    
    		/* ask if the user wants to do it again */
    		printf("\nDo you want to process another drawer? ");
    		while (getchar() != '\n');
    		response=getchar();
    	}
    	return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    9
    I'm new but here's my 2 cents worth.
    try one space between" and %s", and take the & out .
    TRY IT ONE TIME then call me any name that comes to mind.
    good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf doesn't like order of input
    By yougene in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 12:59 PM
  2. input row of integers via scanf into an array...
    By noodles355 in forum C Programming
    Replies: 1
    Last Post: 11-20-2006, 10:12 AM
  3. reading an input line using scanf() with %c
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 04-04-2004, 04:10 PM
  4. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 04:54 PM
  5. Limiting scanf input
    By SMurf in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 06:39 AM

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