Error in logic

This is a discussion on Error in logic within the C Programming forums, part of the General Programming Boards category; I believe that I am making a silly mistake that I just can't see. I need a fresh pair of ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    Error in logic

    I believe that I am making a silly mistake that I just can't see. I need a fresh pair of eyes to look over my code and point out what I'm doing wrong. This is what I want to happen: The user has to enter an integer (1 or 2) If anything but an integer is entered the program prints "Invalid input, exiting game" and the program terminates. Now, if an integer is entered but it is not (1 or 2) I wan't to keep prompting the user to "Enter a valid number 1 or 2." The problem I'm having is that if an integer is entered but it is not (1 or 2) I have to enter the value again before the program continues: For example here is a sample walk through:

    Enter 1 for X and 2 for O: 3 <return> // user entered 3 and pressed return

    3 <return>// user entered 3 again and pressed return

    Enter a valid number 1 or 2 to continue // now the program is running, but it should
    // have run after the first 3 was entered

    Code:
      char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
        printf("Enter 1 for X and 2 for O: ");
        scanf("%d",&move);
        
           if(scanf("%d",&move)!=1){
                printf("Invalid Input exiting game\n");
                exit(1);
            }
        
        while(move!=1 && move !=2){
            printf("Enter a valid number 1 or 2 to continue\n");
            scanf("%d",&move);
        }
        
        if(move==1 || move== 2){
            
        printf("\n\n");
        printf("%c |%c| %c \n",board[0][0],board[0][1],board[0][2]);
        printf("----------\n");
        printf("%c |%c| %c \n",board[1][0],board[1][1],board[1][2]);
        printf("----------\n");
        printf("%c |%c| %c \n\n",board[2][0],board[2][1],board[2][2]);
        
        }
         
          return;

    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Code:
     
        char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
        printf("Enter 1 for X and 2 for O: ");
        scanf("&#37;d",&move);
    ///////////////////////
    delete this scanf("%d",&move) ,and it will be ok!

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    Talking

    Quote Originally Posted by Beair.GQ View Post
    Code:
     
        char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
        printf("Enter 1 for X and 2 for O: ");
        scanf("%d",&move);
    ///////////////////////
    delete this scanf("%d",&move) ,and it will be ok!
    Wow, I knew I was doing something silly. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 06:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 11:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 09:10 PM

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