473,398 Members | 2,088 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,398 software developers and data experts.

Incorrect Input Format PLEASE HELP

Hey guys what im trying to do is write a yatzee game with C.

And im stuck already and its the start?!?!

I want the user to type there 5 numbers.

i.e My program so far does this


Please enter dice Values:
> 1 2 3 4 5

it will then print the numbers
1 2 3 4 5

now thats cool.

But i also want error messages i.e if there are too many numbers entered, or if its in the incorrect input format or incorrect values for the numbers. I.e lower then 1 or higher then 6.

so this is the program ive written so far :
#include <stdio.h>
#include <ctype.h>

int main (int argc, char **argv) {

int dice[5];
int loop;
int next;
int flag2;
int flag1;

printf("Please enter dice values:\n");
for(loop=0;loop < 5; loop++){
scanf("%d",&dice[loop]);
}


for (loop=0,flag2=0,flag1=0;loop<5 && flag2!=5 && flag1<1;loop++){

if(dice[0]>6||dice[1]>6||dice[2]>6||dice[3]>6||dice[4]>6||dice[0]<1||dice[1]<1||dice[2]<1||dice[3]<1||dice[4]<1){
flag1++;
}
else if (dice[0]<6||dice[1]<6||dice[2]<6||dice[3]<6||dice[4]<6||dice[0]>1||dice[1]>1||dice[2]>1||dice[3]>1||dice[4]>1){
flag2++;
printf("%d ",dice[loop]);
}

}
if(dice[0]>6||dice[1]>6||dice[2]>6||dice[3]>6||dice[4]>6||dice[0]<1||dice[1]<1||dice[2]<1||dice[3]<1||dice[4]<1){
printf("Values out of output range.\n");
}
next=getchar();
if(next==' '){
next=getchar();
if(isdigit(next)){
printf("Incorrect number of values.\n");
}
}
return (0);
}

There are two things wrong with this. It prints the Value out of range Error message fine.

but for the incorrect number of values i.e sum1 enters 7 values instead of 5 like below
Please enter numbers:
1 2 3 4 5 6 7
Output:
1 2 3 4 5 Incorrect number of values?

How do i fix that????

also if its a wrong input like
1, 3 +4 6,5 2 (instead of 1 3 4 6 5 2) how would i print an error message for that Please help me.

And thanks in advance
Apr 6 '07 #1
1 2363
ilikepython
844 Expert 512MB
Hey guys what im trying to do is write a yatzee game with C.

And im stuck already and its the start?!?!

I want the user to type there 5 numbers.

i.e My program so far does this


Please enter dice Values:
> 1 2 3 4 5

it will then print the numbers
1 2 3 4 5

now thats cool.

But i also want error messages i.e if there are too many numbers entered, or if its in the incorrect input format or incorrect values for the numbers. I.e lower then 1 or higher then 6.

so this is the program ive written so far :
#include <stdio.h>
#include <ctype.h>

int main (int argc, char **argv) {

int dice[5];
int loop;
int next;
int flag2;
int flag1;

printf("Please enter dice values:\n");
for(loop=0;loop < 5; loop++){
scanf("%d",&dice[loop]);
}


for (loop=0,flag2=0,flag1=0;loop<5 && flag2!=5 && flag1<1;loop++){

if(dice[0]>6||dice[1]>6||dice[2]>6||dice[3]>6||dice[4]>6||dice[0]<1||dice[1]<1||dice[2]<1||dice[3]<1||dice[4]<1){
flag1++;
}
else if (dice[0]<6||dice[1]<6||dice[2]<6||dice[3]<6||dice[4]<6||dice[0]>1||dice[1]>1||dice[2]>1||dice[3]>1||dice[4]>1){
flag2++;
printf("%d ",dice[loop]);
}

}
if(dice[0]>6||dice[1]>6||dice[2]>6||dice[3]>6||dice[4]>6||dice[0]<1||dice[1]<1||dice[2]<1||dice[3]<1||dice[4]<1){
printf("Values out of output range.\n");
}
next=getchar();
if(next==' '){
next=getchar();
if(isdigit(next)){
printf("Incorrect number of values.\n");
}
}
return (0);
}

There are two things wrong with this. It prints the Value out of range Error message fine.

but for the incorrect number of values i.e sum1 enters 7 values instead of 5 like below
Please enter numbers:
1 2 3 4 5 6 7
Output:
1 2 3 4 5 Incorrect number of values?

How do i fix that????

also if its a wrong input like
1, 3 +4 6,5 2 (instead of 1 3 4 6 5 2) how would i print an error message for that Please help me.

And thanks in advance
First I suggest you use code tags([code'][/code'], w/o the asteriks). It makes code easier to read.
Second, in your second for loop, I don't understand why you have the flag1 and flag2 variables. Instead of the flag1 variable you can try the break keyword which breaks out of the current loop. Still, there's no point in testing in every iteration of the loop if the values are out the range. And instead of haveing all those "||"s, you can just do that in a separate for loop like this:
Expand|Select|Wrap|Line Numbers
  1. for (int x = 0; x < 5; x++){
  2.     if (dice[x] > 6 || dice[x] < 1){
  3.         printf("Values are out of range\n");
  4.         break;}}    // break out of the for loop
  5.  
after that you shouldnt need to check that again.
Also, to check if the user entered more than 5 values use the sizeof() function:
Expand|Select|Wrap|Line Numbers
  1. if (sizeof(dice) / 4 > 5){                           // the reason I divided by four is
  2. printf("Too many numbers entered\n")}   // because ints are 4 bytes long and
  3.                                                          // sizeof returns the actual byte size
  4.  
You could set a variable in each of those tests if everything went correctly and then print the actual numbers. If not then ask the user to enter again.
Does that help?
Apr 6 '07 #2

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

Similar topics

1
by: Alfonso Chavez | last post by:
Hi Everybody, I hope this is the correct newsgroup for this, if not please let me know where should i send this question which is driving me crazy. The problem is the FormatDateTime function...
13
by: NM | last post by:
Sometimes ago I was having a problem in linking between C++ and Fortran program. That was solved (using input from this newsgroup) using the Fortran keyword "sequence" with the derived types (to...
8
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
11
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect...
5
by: blackg | last post by:
Input string not in correct format -------------------------------------------------------------------------------- I am trying to view a picture from a table. I am getting this error Input string...
1
by: tshad | last post by:
I have a field that I read from my database that is a bit field field into a textbox. The Trace on the variable (MyInfoCreated) shows it as "False" - as you can see from the error. I tried it...
0
by: hudhuhandhu | last post by:
have got an error which says Input string was not in a correct format. as follows.. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
11
by: RipperT | last post by:
Don't know if this group covers web apps, but here goes. In VS 2005, I am trying to get variables to hold thier values during postback from the server. I convert a text box's user-keyed value to an...
6
by: Lilith | last post by:
I'm trying to build a string for eventual output to a file. Input comes from a number of input boxes but I've only test on one text box content when I get an exception at runtime. The program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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 project—planning, 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.