Second scanf call with char does not work

This is a discussion on Second scanf call with char does not work within the C Programming forums, part of the General Programming Boards category; Hello, This is my first post to the forum, I have a question regarding the scanf function. My program seems ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Second scanf call with char does not work

    Hello,


    This is my first post to the forum, I have a question regarding the scanf function. My program seems to find a random value to use on the second scanf call in my program. I'm using scanf with a char on both occasions. It does not ask for user input on the second scanf function. How can I fix this, do I have to use something else than scanf?

    Here is my simple code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int package_i;
    char internet, package, phone;
    float cost;
    
    main () {
        printf("Welcome to XYZ Cable Providers\nWould you like to subscribe to Internet? (Y or N): ");
        scanf("%c", &internet);
    
    if (internet == 'Y') {
    
    printf("Choose package 1, 2, or 3: ");
    scanf("%d", &package_i);
    
    switch (package_i) {
    
    case 1:
    cost = 15.99;
    break;
    
    case 2:
    cost = 21.99;
    break;
    
    case 3:
    cost = 25.99;
    break;
    
    default:
    break;
    
    
     }
    
    }
    
    
    printf("Your total cost is %.2f!", cost);
    
    printf("Would you like to subscribe to Home Telephone? (Y or N): ");  
    scanf("%c", phone);
    
    
    }
    Thanks for the help,

    Matthew
    Last edited by madwizzy; 02-15-2011 at 08:55 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to do something with the \n (newline character) that is being entered. The FAQ should give you various ways to handle it. You press Y and then hit ENTER, which is 2 characters. You only store one, and the other is left in the input stream, and picked up by the next scanf call.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before a scanf() to get a char, be sure the input stream is clean. No you can't use fflush(stdin).

    You can add getchar() after the scanf() which is BEFORE the scanf() for the getchar(). This problem sometimes will afflict other scanf() formats, as well. Usually, it's the one's for a char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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