473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1.  
  2.   char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
  3.     printf("Enter 1 for X and 2 for O: ");
  4.     scanf("%d",&move);
  5.  
  6.        if(scanf("%d",&move)!=1){
  7.             printf("Invalid Input exiting game\n");
  8.             exit(1);
  9.         }
  10.  
  11.     while(move!=1 && move !=2){
  12.         printf("Enter a valid number 1 or 2 to continue\n");
  13.         scanf("%d",&move);
  14.     }
  15.  
  16.     if(move==1 || move== 2){
  17.  
  18.     printf("\n\n");
  19.     printf("%c |%c| %c \n",board[0][0],board[0][1],board[0][2]);
  20.     printf("----------\n");
  21.     printf("%c |%c| %c \n",board[1][0],board[1][1],board[1][2]);
  22.     printf("----------\n");
  23.     printf("%c |%c| %c \n\n",board[2][0],board[2][1],board[2][2]);
  24.  
  25.     }
  26.  
  27.       return;
  28.  
  29.  

What am I doing wrong?
Oct 21 '08 #1
2 1330
oler1s
671 Expert 512MB
On line 4, you have a call to scanf. On line 6, you have another call to scanf.
Oct 21 '08 #2
Wow I knew I was doing something silly! Thanks for the help.
Oct 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian Shade | last post by:
Hi all. I am having a bit of a problem with my Perl script and the creation of a chart. I can create the chart and create a new SeriesCollection. I then specify the XValues and the Values parameter...
0
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that sound strange. "Exception are divided into logic errors and...
5
by: tiger79 | last post by:
Hello, I'd like to know what the C# counterpart is for the VB On Error statement ? My VB code looks like this : On Error GoTo ErrGetItem plain simple ud say, but I need to "translate" it into...
5
by: rajavel | last post by:
Hi, Runtime Error occurs after occurs after you execute a program. Example is StackOverFlow, Invalid Pointer reference Compile Time Error - If this Error occurs you will not be able to...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
8
by: Taras_96 | last post by:
Hi everyone, We' ve come to the conclusion that we wish the user to be directed to an error page if javascript is disabled <enter comment about how a webpage shouldn't rely on javascript here :)...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
14
by: yeah | last post by:
hi all I am working with linux environment and i compiled device driver programs using native compiler "gcc".In one of my macro i have these lines #if (1 << HARDIRQ_BITS) < MAX_HARDIRQS_PER_CPU...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.