scanf/printf unexpected behavior

This is a discussion on scanf/printf unexpected behavior within the C Programming forums, part of the General Programming Boards category; Ok, so over the summer I started making a simple tic-tac-toe program in my spare time to get some practice ...

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

    scanf/printf unexpected behavior

    Ok, so over the summer I started making a simple tic-tac-toe program in my spare time to get some practice with pointers, structs, and programming in C in general. Overall, the program has been going great, no major issues. However, there is one area I am having a bit of confusion.

    When I started this project, at the beginning of the summer, I had only a small understanding of C, with my first CS class being in C++. As such, I was not too familiar with how to read input from stdin, so I ended up using getchar(). Now, I am trying to change my input to use scanf() instead. At one point in particular, this is causing some small issues. Reference the code block below:

    Code:
    int main(int argc, char **argv)
    {
            if (system("cls"))
                    system("clear");
    
            printf("TicTacToe v1.0\nBy Daniel Bauer\n\n");
    
            printf("Number of games to play: ");
    
            int games, i;
            if(scanf("%d", &games) == 0)
            {
                    printf("Input error, expected integer\n");
                    return 0;
            }
    
            for(i = 0; i < games; i++)
            {
                    if (system("cls"))
                            system("clear");
                    board *gameBoard = initializeBoard( );
                    while(gameBoard->human != 'X' && gameBoard->human != 'O')
                    {
                            printf("Pick X or O: ");
                            scanf("%c", &gameBoard->human);
                            gameBoard->human = toupper(gameBoard->human);
                    }
    
                    if(gameBoard->human == 'X')
                            gameBoard->computer = 'O';
                    else
                            gameBoard->computer = 'X';
    
                    ...
    This part of code is supposed to read in the number of games the user wants to play and then whether the player wants to be X or O. The problem is that when the program gets to asking the player X or O, it will print "Pick X or O: Pick X or O: ", when I would prefer it only print that once.

    Does anyone here know what might be causing this? Any help with fixing my code or a workaround would be appreciated.

    And if for whatever reason you need to see more of the code, just let me know!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Hmmm, okay. I guess I'll try implementation with another method of reading in from stdin... Didn't realize scanf() could be so flawed... Oh well, onward to rewriting
    Last edited by DanV2; 10-11-2010 at 10:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. unexpected exception handler
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2008, 04:49 AM
  3. Unexpected results from function
    By ajdspud in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2005, 03:19 PM
  4. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

Tags for this Thread


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