-
problem with c
im a 1st year uni student and im kinda stuck with this c assignment. Its supposed to be a yahtzee scoring program. read 5 values from the user and display the scoring options.
We're supposed to error check the input in 3 different ways
1)
correct format, ie the values should be separated by spaces and the number of spaces between each value doesnt matter
so 12345 is not the correct format but 1 2 3 4 5 is.
2)
range.needs to be between 1 and 6
3) frequency
there needs to be 5 dice values entered
ive quite easily been able to code it to check for number two but am having a few problems with 1 and 3
1) im using getchar to get the values off the user and then use an if statement with the isdigit() to filter out the digits. Porblem: 12345 is read the same way as 1 2 3 4 5 which isint in accordance to error checking method number 1
3) i tried creating a frequency arry whose components i could add and see if they were <or>5. for some reason when i try to print that array i get weird values like the following and ive got no idea why
Code:
Freq[1]= 134518840
Freq[2]= -1075437672
Freq[3]= 134513481
Freq[4]= -1208152076
Freq[5]= -1208250368
Freq[6]= -1075437640
following is my code
Code:
#define VALUES 5
#define FREQ 7
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
int dice[VALUES];
int freq[FREQ];
int x = 0;
int p;
int c;
printf ("Please enter dice values: \n");
c = getchar();
while(c !=10)
{
if (isdigit(c))
{
dice[x] = c-48;
x = x + 1;
}
c = getchar();
}
for(x=0;x<VALUES;x++)
{
if (dice[x]<1 || dice[x]>6)
{ printf("Value Out of Range.");
exit(1);
}
}
for(x=0;x<VALUES;x++)
{
dice[x]=p;
freq[p]++;
}
for (x = 0; x <VALUES; x++)
{
printf ("Dice [%d] = %d\n", x+1, dice[x]);
}
for(x=1; x<FREQ;x++)
{
printf("Freq[%d]= %d\n",x,freq[x]);
}
return (0);
}
nb: i no the formatting is wrong.:(
a big thankyou to anyone who can help!
piyush
-
Re: problem with c
Well I noticed that your variable p is never #1 initialized and never #2 set anywhere.
If your using old MS compilers, this can lead to undefined behavior.
Additionally if your not, p will alwayz be 0.
The problem with C is that you didnt create it?
-
Re: problem with c
so if i interpret you right you are saying is in the beginning i should set p to a value like ive done with x?
-
Re: problem with c
Read my posting again.
Where is the value of p set anywhere in your program? :D
-
Re: problem with c
Code:
for(x=0;x<VALUES;x++)
{
dice[x]=p;
freq[p]++;
}
no?
-
Re: problem with c
incorrect. to initialize the value of p you have to:
Code:
int p = 0;//initialize variable to zero
and later to set the value of p do something like:
Code:
p=10;//set the value of p to 10 with an assignment statement
//or
p++; //increment the value of p
//or
p+=100;//add 100 to the current value
in some compilers p is an undefined value....as i said.
Think about what p should be.
-
Re: problem with c
I think this is "cant see the forest for the trees..."
should be
As for the frequencies, you never init the elements to 0.
-
Re: problem with c
But that is his job...not ours...he has to think about his HW right?
-
Re: problem with c
Code:
for(x=0;x<FREQ;x++)
{
freq[x]=0;
}
for(x=0; x<VALUES;x++)
{
p=0;
p=dice[x];
freq[p]+=1;
this seems to have solved the problem.
anyone got a clue asto how i can get around the problem in error checking step one?
thanks ahoodin and TheCPUWizard for your help! (Y) :D
-
Re: problem with c
For your first problem, you could read in ints instead of chars. This would force the user to space them out, it would also make it so you don't have deal with all that c-48 inconvenience. The downside is that you need to do some error checking to make sure that they don't input a character.
instead of c = getchar(), you would use
cin >> c;
-
Re: problem with c
cin is for 'C++'. The poster said originally that it's a 'C' assignment.
-
Re: problem with c
for #3, just check x. make sure it does not get too large inside the loop, and make sure it is not too small when you leave the loop.
for #1, just do value checking on the ch.
-
Re: problem with c
by checking values of ch ill be able to tell if its a space or a number but what if the user enters 12345, both those conditions are true and itll accept it. how can i differentiate between no space between 2 numbers and a space between them.
this is what i could come up with??:(
it can identify all the illegal delimiters the user might use except when the input is 12 33 5 etc ie 2 values not separated by gaps
Code:
if ( (isdigit(c)) || isspace(c)) {
blah, continues with adding values to array
}
else
{
printf("Incorrect input format");
exit(1);
}