Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Scanf problem

opensourceopensource Member Posts: 25
[b][red]This message was edited by opensource at 2002-9-26 9:22:55[/red][/b][hr]
Here is simple code I wrote. Whenever it executes, it will prompt for selection, But it does not wait for input to its second question. How do I fix. Do I have to flush the Input buffer?
#include
main(){
short int ip;
char c;
unsigned char uc;
long l;
unsigned short int si;
float f;
double d;
char in, ans='y';
int i;

while(ans!='n'){

printf(" 1. Char
2. Unsigned Char
3. Short Int
4. Unsigned Short Int
5
Long
6.Float
7.Double
");

scanf("%c", &in);
switch(in){
case '1': printf("Char = %d", sizeof(c)); break;
case '2': printf("Unsigned Char =%d", sizeof(uc)); break;
case '3': printf("Short Int = %d", sizeof(ip)); break;
case '4': printf("Unsigned Short Int=%d", sizeof(si));break;
case '5': printf("Long = %d", sizeof(l)); break;
case '6': printf("Float size=%d", sizeof(f)); break;
default: printf("You have to make a selection to find the size of data type

");
} //End of switch
printf("Do you want to continue ");
scanf("%c",&ans);
}//End of While

}//End of Main


Comments

  • slicer69slicer69 Member Posts: 272
    Yeah, you probably have a carrage return or newlin in the input buffer left over. So try calling getchar() before the second prompt.

    : [b][red]This message was edited by opensource at 2002-9-26 9:22:55[/red][/b][hr]
    : Here is simple code I wrote. Whenever it executes, it will prompt for selection, But it does not wait for input to its second question. How do I fix. Do I have to flush the Input buffer?
    : #include
    : main(){
    : short int ip;
    : char c;
    : unsigned char uc;
    : long l;
    : unsigned short int si;
    : float f;
    : double d;
    : char in, ans='y';
    : int i;
    :
    : while(ans!='n'){
    :
    : printf(" 1. Char
    2. Unsigned Char
    3. Short Int
    4. Unsigned Short Int
    5
    : Long
    6.Float
    7.Double
    ");
    :
    : scanf("%c", &in);
    : switch(in){
    : case '1': printf("Char = %d", sizeof(c)); break;
    : case '2': printf("Unsigned Char =%d", sizeof(uc)); break;
    : case '3': printf("Short Int = %d", sizeof(ip)); break;
    : case '4': printf("Unsigned Short Int=%d", sizeof(si));break;
    : case '5': printf("Long = %d", sizeof(l)); break;
    : case '6': printf("Float size=%d", sizeof(f)); break;
    : default: printf("You have to make a selection to find the size of data type
    :
    ");
    : } //End of switch
    : printf("Do you want to continue ");
    : scanf("%c",&ans);
    : }//End of While
    :
    : }//End of Main
    :
    :
    :

  • WarlockzzzWarlockzzz Member Posts: 131
    [b][blue]This should work :
    [code]
    #include

    main(){
    char c, in, ans='y';
    short int ip; long l; float f; double d;
    unsigned char uc; unsigned short int si; int i;

    while(ans!='n'){
    printf(" 1. Char
    2. Unsigned Char
    3. Short Int
    4. Unsigned Short Int
    5 Long
    6.Float
    7.Double
    ");

    scanf("%c", &in); getchar(); // * see below
    switch(in){
    case '1': printf("Char = %d", sizeof(c)); break;
    case '2': printf("Unsigned Char =%d", sizeof(uc)); break;
    case '3': printf("Short Int = %d", sizeof(ip)); break;
    case '4': printf("Unsigned Short Int=%d", sizeof(si)); break;
    case '5': printf("Long = %d", sizeof(l)); break;
    case '6': printf("Float size=%d", sizeof(f)); break;
    default : printf("You have to make a selection to find the size of data type
    ");
    } //End of switch

    printf("Do you want to continue ");
    scanf("%c",&ans); getchar(); // * see below
    }//End of While
    }//End of Main[/code]
    * As for the getchar(); after the scanf, i do not quite remember why i had to put it here, but i know that when i learn it in school, they told us to put a getchar after to make sure that the enter is taken when you use a second scanf or something like that.
    [hr]
    [/blue][/b]
    Warlockzzz
    "I am not sleeping, I am slimply preparing myself to work!"

  • Shawn CarterShawn Carter Member Posts: 0

    ___ ( http://forcoder.org ) free ebooks and video tutorials about / PL/SQL Python C# Delphi Java R PHP JavaScript MATLAB C++ Go Ruby Visual Basic .NET Scratch Objective-C C Visual Basic Swift Perl Assembly Transact-SQL ML D COBOL Erlang Lua Awk VBScript FoxPro Kotlin Rust Prolog Dart Scheme F# SAS Scala Ada Clojure ABAP Crystal Fortran Alice Lisp LabVIEW Hack Apex Bash Julia Logo / ____________

Sign In or Register to comment.