943,570 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2877
  • C++ RSS
Aug 21st, 2004
0

incorrect output in Linux environment

Expand Post »
below is one of my function call of a program done in C:

/* asks if the players would like to have another game of Tic Tac Toe */
void prompt_user(int game[][SIZE])
{
char reply;

printf("\nPlay Again? (Y/N): ");
scanf("%c", &reply);

if(reply == 'Y' || reply == 'y')
{
print_empty_board();
initialise_board(game);
}

else if(reply == 'N' || reply == 'n')
{
printf("Hope You Have Enjoyed The Game!\n\n");
exit(0); /* quits the game */
}
}

when using microsoft visual C++ and devC++ to compile and execute, there were no errors in the output when playing the tic-tac-toe game.

however, when i used my school's lab (which is using Linux and xemacs only) pc to compile and execute, the output did not allow me to enter Y or N as an answer to 'Play Again?'. The execution exits immediately. anyone knows the reason and how to solve my problem??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonix is offline Offline
3 posts
since Aug 2004
Aug 25th, 2004
0

Re: incorrect output in Linux environment

Hello,

I think your bug has to do with the reply definition. You are making two dangerous assumptions with your code... 1) is that char reply; properly initializes the variable (instead of you making an explicit definition, such as reply='s'; so that you know what that variable holds. 2) Your code makes no provision if someone enters in something else, such as the letter 'T' If I were to enter in 'T', neither of your if statements will work, as they fail the tests, and the function will exit without the exit code.

My GUESS is that you might have an extra character coming through the function, and the computer is seeing that as valid input. Granted, the code does not bring in any data except for the one integer, but what is in the character buffer?

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked List using pointers (C++ ADT)
Next Thread in C++ Forum Timeline: merged:nesting loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC