Problem with Do while loop

This is a discussion on Problem with Do while loop within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a function that promote users to enter an arithmetic operater, if they enter a ...

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

    Problem with Do while loop

    Hi,

    I am trying to write a function that promote users to enter an arithmetic operater, if they enter a wrong character i have to promote them again to enter it.

    It work fine if they enter right arithmetic operator, but if they enter wrong character, the do while loop i have loop twise for no reason, i cant find the reason. Please help me out. ( more over number of loops increase if i put more than 1 charactor, if i put one wrong charactor its only loop twice but if i put 10 it look like 11 times.)

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char ope;
        do
        {
            printf("Enter an arithmetic operator (+, -, *, /): ");
            scanf("%c",&ope);
            if (ope=='+')
                printf("You have entered: %c\n",ope);
                    else
                     ;
        }
        while (ope!='+');
    }
    Last edited by prasdhy2008; 10-16-2010 at 02:53 PM. Reason: wrong tital

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    We want to absorb the enter key.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char ope;
        do
        {
            printf("Enter an arithmetic operator (+, -, *, /): ");
            scanf("%c",&ope);
    	getchar(); // absorb ENTER
    	
            if (ope=='+')
                printf("You have entered: %c\n",ope);
                    else
                     ;
        }
        while (ope!='+');
    }

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    The newbies love scanf.

    Code:
    #include <stdio.h>
    
    int main() {
        int x;
        do {
            printf("Enter an arithmetic operator (+, -, *, /): ");
            x = getchar();
        } while (c != '+');
        printf("You entered %c\n", x);
    }
    That should work for you.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    scanf("%c") will read, at most, one character. Any other characters you type will hang around until the next call to an input function. Even if you only type one character, you're still hitting enter, meaning a newline will get read the next time around, hence your extra looping.

    The easiest way to fix this is to just use fgets() to read a line and look at the first character in the string.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    my teacher told us to use a but i have no clue how to use it

    char newline;
    scanf("%c", &newline);

    I am not sure i am suppose to use getchar(); coz we never learn that in the class.


    THank you for your help

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    I am unsure why it didn't work. scanf("%c\n", &myStr); didn't work for me either. I knew getchar() would work, so there it is.

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by prasdhy2008 View Post
    my teacher told us to use a but i have no clue how to use it

    char newline;
    scanf("%c", &newline);

    I am not sure i am suppose to use getchar(); coz we never learn that in the class.


    THank you for your help
    Damn, really? I should teach C programming.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Babkockdood your solution works, but they cut marks ifwe use something we are not suppose to.

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by prasdhy2008 View Post
    Babkockdood your solution works, but they cut marks ifwe use something we are not suppose to.
    Replace x = getchar(); with scanf("%c", &x); then.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Quote Originally Posted by prasdhy2008 View Post
    Babkockdood your solution works, but they cut marks ifwe use something we are not suppose to.
    Then use scanf agian to grab the newline that is still in the input buffer right after your
    Code:
    scanf("%c",&ope);
    Or change the scanf to get both at the same time.

    *Note this will not work if you enter more then one character, or if you press enter first.
    You would need to keep looping until you hit a newline to make sure you grab all the text the user enters.
    Last edited by dylan; 10-16-2010 at 04:11 PM. Reason: Too slow to press submit ;-)

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Here is a function which clears you input buffer. Call this after your scanf function call.

    Code:
    void clear_buffer( void )
    {
         int ch;
         while( ( ch = getchar()) != '\n' && ch != EOF );
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

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