Thread: Scanf for char doesn't work in loop

  1. #1
    Unregistered
    Guest

    Scanf for char doesn't work in loop

    What am I doing wrong here? I've tried this in the main, I've tried it in a function using a return statement, and here is my attempt to pass a pointer to a character (a math operator--this is supposed to be a calculator program) into a loop. It goes in the first time, but then after that it ignores the input of the character and just prints out the statements without waiting for input. Can you look at this program fragment and tell me what to do to fix it?

    Thanks a lot.
    Donna

    #include <stdio.h>

    void get_operator(char *lop);
    int get_operand();

    void main(){
    // int num1=0;
    char op;
    int num2=0;
    // int accum=0;
    int i;

    for (i =1; i < 4; ++i){
    get_operator(&op);
    printf("the operator is %c\n", op);

    num2 = get_operand();
    printf("operand is %d\n", num2);
    }
    printf("the final result is %d\n", num2);
    }

    void get_operator( char *lop){
    printf("enter a math operator or q to quit> ");
    scanf("%c", lop);
    }
    int get_operand(){
    int lnum2; /* local variable for operand */
    printf("enter an integer >");
    scanf("%d", &lnum2);
    return lnum2;
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    you are missing the address of operator

    scanf("%c", &lop);
    T

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    AAAAAAAAHHHH
    scanf(), I never use it, it's dangerous(has a tendency to leave newline characters in the buffer) use getch() instead or for numeric data use gets() and then sscanf()

  4. #4
    Unregistered
    Guest
    The & in front of the variable made no difference. It still ignored the thing the 2nd time through.

    Tried using getc--but it said that this was only for unions or structures. I'm just trying to enter one little character!

    What is the syntax for getc when reading from direct input, not from a file?


    Donna

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    try
    ch = getc(stdin);
    or
    ch = getchar();
    or
    ch = getch();
    the last one needs <conio.h>

  6. #6
    Unregistered
    Guest
    Thanks for all your suggestions.

    Just wanted to let you know that my instructor told me the solution is to put a space in front of the character reference in the scan statement:
    scanf(" c", &lop); (or in this case, just lop since it is a pointer already)

    Huh!

    Donna

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Put this after the scan:

    while(getchar() != '\n') continue;

    clears the standard input buffer.

  8. #8
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Putting space after before the % does the trick. I once ran into this in one of my first C assignments.

    scanf(" %c", &mychar);

    The reason this works is that the first space flushes stdin and then lets the user input the character.

    Cheers.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  9. #9
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by SPOOK
    try
    ch = getc(stdin);
    or
    ch = getchar();
    or
    ch = getch();
    the last one needs <conio.h>
    getch() is not ansi C , one cant expect everyone to have a conio.h in their implementation

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Engineer
    Putting space after before the % does the trick. I once ran into this in one of my first C assignments.

    scanf(" %c", &mychar);

    The reason this works is that the first space flushes stdin and then lets the user input the character.

    Cheers.
    It may work but then again it should not
    from K&R
    "The format string may contain
    .Blanks and and tabs which are ignored
    .ordinary characters(not %)which are expect to match the next non white-space character of the input stream.
    .conversion specifications , consisting of a % ,an optional assignment supression character * ,an optional number specifying the field width ,an optional h,l or L indicating the width of the target , and a conversion character
    "

    So for a ansi compliant compiler scanf("%c",&mychar); and
    scanf(" %c",&mychar) should lead to the same effect as blanks and tabs are supposed to be ignored .

  11. #11
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    In my best opinion, avoid scanf. I was recently having problems with it and I came to the conclusion that there are numberous ways to get around it. For instance, the ol' fgets and sscanf routine. That works great.

    --Garfield

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a simple loop...
    By Optimus Prime in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 09:28 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. endless loop for scanf - plz help
    By owi_just in forum C Programming
    Replies: 18
    Last Post: 03-20-2005, 01:41 PM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM