Thread: Need help!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Need help!

    ok so why is it in my program that i cant input a character? it just says that it equals the number 10
    insert
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    	double T;
    	int qty;
    	char ch;
    
    
    	printf("Enter the number of items\n");
    	scanf("%d", &qty);
    
    	printf("Enter the price\n");
    	scanf("%lf", &T);
    
    	printf("Enter a class\n");
    	scanf("%c", &ch);
    
    	printf("%d\n",ch);
    
    	
    
    		return 0;
    }
    Last edited by stormforce; 09-23-2010 at 06:39 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are printing the character as an integer -- not a character. Use %c in your printf() statement. Also, when you read in a double, use %lf instead of %f.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    no i mean when i run this program im allowed to enter 2 inputs and then the program ends before i can even input a char.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() leaves a newline char behind, and that is ASCII value of 10, and you're reading it - so it looks like your scanf() for a char is being skipped.

    To stop it, put this line, immediately after each scanf() line of code:

    getchar();

    which will pull the newline char off the keyboard buffer, and leave it "clear".

    Edit: To be more precise, scanf() can skip a newline char in the keyboard buffer IF it's looking for a data type that is bigger than a tiny char data type.

    But - when scanf() is looking for a char, it can NOT skip a newline char, because that IS just the size of data it is expecting.

    So it takes your newline char, says it's just what the doctor ordered, thank you very much, and continues on. Not nice, but true.

    Only one good reason why scanf() should not be used for getting user input, later on. It's "fussy" and relies on the data being strictly formatted, and/or you having a very good understanding of just how scanf() works.

    Later, look to use fgets(myBuffer, sizeof myBuffer, filePointer);

    It is a beauty for user input!
    Last edited by Adak; 09-23-2010 at 06:49 PM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    thank you that works

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    scanf("%f", &T);
    When you enter a float, and hit enter, there is the float number that you entered, as well as the newline from hitting enter on your keyboard. scanf() will only take the float, and will leave the newline '\n' in the buffer. On the next call to scanf(), you ask for a char. In this case, because the newline is already sitting in the buffer, that satisfies scanf(), and it stores it into ch. Then, since scanf() has done its bit, printf() gets called, and prints out the value of ch which, in decimal (integer) format, is 10. See here for what I mean.

    Edit: Obviously everyone types faster than I do.

Popular pages Recent additions subscribe to a feed