Thread: Simple logic problem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    Simple logic problem

    Code:
    printf("\n Are your a Male [1] or Female [2] : ");
    fflush(stdin);
    scanf("%d",&pick1);
    while (pick1 != 1 && pick1 != 2 )
    {
    printf("Enter a correct choice : ");
    fflush(stdin);
    scanf("%d",&pick1);
    }
    Hi guys i was just curious about this code here...if i enter a letter first the program ends abruptly breaking out of the while loop but if i enter a integer say 5 then enter a letter it works just how i want it to why does the code break out of the while loop and how can i improve this code its simple yes ikw but i was thinking of doing some dialog type of program like in swtor if you have ever played it

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    Well, not sure why you were having problems. I couldn't replicate that result in Code::Blocks. It worked as expected when I executed your code with my stuff added in.

    Code :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <math.h>
    #include <dos.h>
    #include <windows.h>
    #ifdef _WIN32
    #define SYSTEM Windows
    #endif // _WIN32
    int main() {
        int pick1 = 0;
        printf("\n Are your a Male [1] or Female [2] : ");
        fflush(stdin);
        scanf("%d",&pick1);
        while (pick1 != 1 && pick1 != 2 ){
        printf("Enter a correct choice : ");
        fflush(stdin);scanf("%d",&pick1);}
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Code:
     if (pick1 == 1)
       {
           printf("\n Ok Man");
       }
        else
        printf("\n Ok Girl");
    add this code ^^ to see what i mean if you input a letter it will say ok girl instead of going to the error message
    i put the value of pick1 into a next variable in the while loop and it is fine now though

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    Did you check the return value from your scanf() call?

    add this code ^^ to see what i mean if you input a letter it will say ok girl instead of going to the error message
    That's because when you entered a character when scanf() was expecting a number the stream failed. When the stream failed it didn't retrieve any value. So since you didn't get any input pick1 is equal to 0, the value that you used in the initialization.

    Also the fflush() function is not designed to work with input streams, only output streams are supported in the standard. Using this function on an input stream invokes undefined behavior.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Did you check the return value from your scanf() call?


    That's because when you entered a character when scanf() was expecting a number the stream failed. When the stream failed it didn't retrieve any value. So since you didn't get any input pick1 is equal to 0, the value that you used in the initialization.

    Also the fflush() function is not designed to work with input streams, only output streams are supported in the standard. Using this function on an input stream invokes undefined behavior.

    Jim
    oh ok thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 16
    Last Post: 11-09-2011, 01:43 PM
  3. Simple Logic problem
    By will of fortune in forum C Programming
    Replies: 4
    Last Post: 03-06-2010, 02:25 PM
  4. Simple Bouncing Logic
    By SMurf in forum Game Programming
    Replies: 4
    Last Post: 10-27-2006, 10:37 AM
  5. Replies: 5
    Last Post: 01-31-2006, 01:54 AM