# Thread: Having trouble creating an arithmitec game, please take a look if you can very short

1. ## Having trouble creating an arithmitec game, please take a look if you can very short

So this is part of my code to create and arithmetic game, it will ask for the maximum number you'd like for an addition game. Every time you get the answer correct you get a point. I have the code spitting out teh random numbers but it wont output if the answer is correct it only prints out if its incorrect. Please help if you can any help is appreciated.....
Code:
```switch(choice){
case 1:
printf("What is the maximum number you'd like?\n");
scanf("%d", &maxnum);
for (i=0; i<10; i++){
srand(time(0));
x = rand() % maxnum;
y = rand() % maxnum;
printf("What is %d+%d\n", x, y);
correct = x+y;
if (answer == correct){
printf("Correct, great job!\n");
count1++;
score += count1;
}
else
printf("Sorry, that's incorrect, the answer is %d\n", correct);
}//end case1 for
break;```

2. Are 'answer' and 'correct' both ints? ('answer' appears to be judging by the scanf, but confirmation would be nice)

Also, what type should the second parameter of the scanf() be? Hint: it shouldn't be an int

3. For your scanf, you need an ampersand in front of answer, just like maxnum.
And srand should only be called once at the very beginning of your program, not every time you want another random number. It's just used to "initialize" the random number generator (RNG).
Also, you don't seem to be calculating score correctly !?

4. Originally Posted by oogabooga
Also, you don't seem to be calculating score correctly !?
You're a hard boss. It's an arithmetic game, surely you don't expect the programmer to get it correct as well.

5. I've placed the ampersand in there but it still wont work correctly :/

6. Post it....

7. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define MULT 2

int main() {
int choice, roundscore, totalscore, count1, count2, x, y, maxnum, answer, correct, i, z, guess, guessnum;
srand(time(0));

while (choice!=4){
printf("Please make a selection from the following:\n");
printf("1. Play Arithmetic Game.\n");
printf("2. Play Guessing Game.\n");
printf("3. Print Score.\n");
printf("4. Quit.\n");
scanf("%d", &choice);

switch(choice){
case 1:
printf("What is the maximum number you'd like?\n");
scanf("%d", &maxnum);
for (i=0; i<10; i++){
x = rand() % maxnum;
y = rand() % maxnum;
printf("What is %d+%d\n", x, y);
correct = x+y;
if (answer == correct){
printf("Correct, great job!\n");
count1++;
roundscore += count1;
}
else
printf("Sorry, that's incorrect, the answer is %d\n", correct);
}//end case1 for
printf("Your score for the round is %d.\n", roundscore);
totalscore += roundscore;
break;

case 2:
guessnum = rand() % 100;
for(z=0; z<17; z++){
printf("Enter a guess!\n");
scanf("%d\n", &guess);
if (guess != guessnum && guess > guessnum){
printf("Your guess is to high, guess again.\n");
count2++;
}
if (guess != guessnum && guess < guessnum){
printf("Your guess is to low, guess again\n");
count2++;
}
if (guess == guessnum){
printf("Great, you guessed the correct number %d in %d guesses.\n", guessnum, count2);
count2++;
break;
}
}// end case 3 for loop
break;

case 3:
printf("Your total score is %d.\n", totalscore);
break;

case 4:
exit(0);

default:
printf("You have made an invalid choice.\n");
}

}//END WHILE LOOP

return 0;
}```

8. \n is useless in your scanf (and on some terminals/OS probably requires you to type in another number just so it can stop skipping whitespace).

Additionally: you are giving them an undefined number of points: roundscore starts out as an undefined number; count1 starts out as an undefined number, to which you are successively adding one to get further undefined numbers; those undefined numbers are then being added to roundscore to get another undefined number.

You should strongly consider initializing your variables to meaningful values when you start the game.

9. Originally Posted by tabstop
[\n at the end of scanf format] ... on some terminals/OS probably requires you to type in another number just so it can stop skipping whitespace
Incorrect!!! (Did you just pull that out of your butt?)

10. Originally Posted by oogabooga
Incorrect!!! (Did you just pull that out of your butt?)
Have you ever used Windows? I have, and I know it requires it. You can test it with this program (assuming you have Windows installed somewhere ... I'm using MinGW but I believe it works the same in VS but I don't have it to hand right this minute):
Code:
```#include <stdio.h>

int main(void) {

int i;
printf("Type in a number:");
scanf("%d\n", &i);
printf("Your number was %d.\n", i);
return 0;
}```
You can wait as long as you like, but unless you type in another non-whitespace character your input will not be processed. (EDIT: I should add, it can be on the same "input line" -- typing "4 7<enter>" will do fine, but you can type "4<enter><enter><space><tab><enter><enter><tab><en ter>" and the program will still be waiting to finish input.)

11. Originally Posted by oogabooga
Incorrect!!! (Did you just pull that out of your butt?)
I doubt it... I'd say it was pulled out of the hat!

Code:
```                         _.---,_
.'       `'.
\    __..-'\
}-"`       \
/__,,..---.._|
\            |
|---..__     |
/        ``"-./
.'---...__      |
.'          ``"-./
,--./...,,,__        /
'--.'__    __```.-. /._
/ `  `  '  `=/.-.|-._)
| .-.  .-.  "\\  /
|| O| | O|  ""=='_\
.-' '-'o '-'    ""=\`
`''--/-              ""=-,\--._
.---|-            ( ""=-. \`
\             /`)"=."=|'-.
'.       _.-' '  "=|\|
(`----`    '="=|/
`-.       "=/`
'.     =/
\   =|
.-.        |` "=|
(   ~._     | "==|        _.-~`\
\     ~.   |'"="|    _.-~      )
;      ~-.|.-._|_.-~         /
/        _-(  /-.__          (
'._..--~~`/`/-'\-._ `~~-       ;
jgs  /"=|  |" =\~-...___.-~
/=" /  | "==\
/ = (_  \  "==\
;="=   `\_)  =="\```

12. Originally Posted by tabstop
Have you ever used Windows? I have, and I know it requires it.
Wow ... sorry. I don't know what I was thinking. My butt hurts.

13. I can now confirm that the same happens on *nix as well as Windows, and I can also confirm because that's what the standard says it has to do (admittedly I haven't bought the new standard yet, because I'm a bad person, but):
Originally Posted by C99 standard, section 7.19.6.2, para 5
A directive composed of white-space character(s) is executed by reading input up to the
first non-white-space character (which remains unread), or until no more characters can