Hey everyone Im currently fighting with C. I did C++ all last year but now I must learn C.
This is my first C program, a very simple "guessing game", what I cant figure out is, when I guess above the number generated by rand(), 2 of my if statements fire off, and when I guess lower, all 3 of them fire off.
Im guessing something is wrong with my if comparisons or something else about C I didn't know Im missing. Are my if statements done correctly?
Any advice would be great.
104 void guessing_game(int seed)
105 {
106 int num;
107 int Guesses = 0;
108 int secrete;
109
110 /* I called time instead of seed because with seed the number was always the same
111 cause you had the us give 123 as a command line argument to the print function
112 and this function. With the same 123 seed i always got the same number */
113 srand(time(NULL));
114 secrete=rand() % 100;
115
116 printf("seed is = %i\n",seed);
117 printf("Secrete is = %i\n",secrete);
118 printf("I have a number between 0-99 (Press Enter to continue)\n");
119 getchar();
120
121 while (Guesses < 10)
122 {
123 printf("Enter your guess #%i:",Guesses);
124 scanf("%i", &num);
125
126 if (secrete > num)
127 {
128 printf("Guess higher!\n");
129 };
130 if (secrete < num);
131 {
132 printf("Guess lower...\n");
133 };
134 if (secrete == num);
135 {
136 printf("Congratulations! You guessed it in %i guesses!\n", Guesses);
137 break;
138 };
139 ++Guesses;
140 };
141
142 if (Guesses <= 10)
143 printf("Sorry, you lose!");
144 }
Here is the output
host-248-213:UnixCoding peterg$ gcc intro2.c
host-248-213:UnixCoding peterg$ ./a.out
seed is = 123
Secrete is = 32
I have a number between 0-99 (Press Enter to continue)
Enter your guess #0:33
Guess lower...
Congratulations! You guessed it in 0 guesses!
Sorry, you lose!
host-248-213:UnixCoding peterg$ ./a.out
seed is = 123
Secrete is = 16
I have a number between 0-99 (Press Enter to continue)
Enter your guess #0:15
Guess higher!
Guess lower...
Congratulations! You guessed it in 0 guesses!
Sorry, you lose!
host-248-213:UnixCoding peterg$
Hey everyone Im currently fighting with C. I did C++ all last year but now I must learn C.
This is my first C program, a very simple "guessing game", what I cant figure out is, when I guess above the number generated by rand(), 2 of my if statements fire off, and when I guess lower, all 3 of them fire off.
Im guessing something is wrong with my if comparisons or something else about C I didn't know Im missing. Are my if statements done correctly?
Any advice would be great.
snip
I think the problem is that currently ALL the if statements are checked. Think about it, if you know that the guess is higher than the number, it's not going to be lower. It's a pointless thing to check.
Change from this style:
if {
...
}
if {
..
}
if {
...
}
To:
if {
...
}
else if {
// This only goes off if the statement above is false
...
}
else if {
// Same thing here
// Essentially, else statements act naturally as break statements (kind of)
...
}
So basically, the if statement will check normally, if the if statement doesn't go through, the next else or else if statement will go of like it was an if statement.
I can't verify this right now, but please reply back if I'm wrong (which is likely).
Rollback Post to RevisionRollBack
"If tyranny and oppression come to this land, it will be in the guise of fighting a foreign enemy. The loss of Liberty at home is to be charged to the provisions against danger, real or imagined, from abroad." - James Madison
I think the problem is that currently ALL the if statements are checked. Think about it, if you know that the guess is higher than the number, it's not going to be lower. It's a pointless thing to check.
Change from this style:
-snip-
So basically, the if statement will check normally, if the if statement doesn't go through, the next else or else if statement will go of like it was an if statement.
I can't verify this right now, but please reply back if I'm wrong (which is likely).
Thanks for your reply!
I want all the if statements to be checked but, I dont want them all to fire off. I want it to check each one, then depending on the number, which changes every time the program is fired up, execute that if statement.
I did try else if before but gcc doesn't like it.
host-248-213:UnixCoding peterg$ gcc intro2.c
intro2.c: In function ‘guessing_game’:
intro2.c:130: error: expected expression before ‘else’
intro2.c:134: error: expected expression before ‘else’
Right, try just adding breaks to the end of all your if statements like:
if (expression){
...
break;
}
if (expression){
...
break;
}
That should work.
Rollback Post to RevisionRollBack
"If tyranny and oppression come to this land, it will be in the guise of fighting a foreign enemy. The loss of Liberty at home is to be charged to the provisions against danger, real or imagined, from abroad." - James Madison
Right, try just adding breaks to the end of all your if statements like:
if (expression){
...
break;
}
if (expression){
...
break;
}
That should work.
Thanks for your continued effort!
Hm, well that worked for the if detection, but it breaks me out of my loop. Ill figure out how to make it work though. Im really curious as to why it detects properly with the breaks.
Here, this took me awhile to fix actually, you had a lot of syntax errors or just logic errors. I wrote this in Visual C++, so, if I accidently used any C++ functions by accident, my mistake!
I fixed your seed too.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessing_game()
{
long seed = 0;
int guess = 0;
int guessesCounter = 0;
int secretNum = 0;
const int MAX_GUESSES = 10;
/* I called time instead of seed because with seed the number was always the same
cause you had the us give 123 as a command line argument to the print function
and this function. With the same 123 seed i always got the same number */
seed = time(NULL);
srand(seed);
secretNum = rand() % 100 + 1;
printf("seed is = %i\n", seed);
printf("Secret number is = %i\n",secretNum);
printf("Press Enter to continue\n");
getchar();
while (guessesCounter <= MAX_GUESSES - 1)
{
printf("You have %i guesses remaining\n", MAX_GUESSES - guessesCounter);
printf("Please enter a number between 1-100: ");
scanf("%i", &guess);
if (guess < secretNum)
{
printf("Guess higher!\n\n");
}
else if (guess > secretNum)
{
printf("Guess lower...\n\n");
}
else if (guess == secretNum)
{
printf("Congratulations! You guessed it in %i guesses!\n\n", guessesCounter + 1);
getchar();
break;
}
else if(guessesCounter >= MAX_GUESSES)
{
printf("Sorry, but you lose!");
getchar();
break;
}
guessesCounter++;
}
}
int main( int argc, const char* argv[] )
{
guessing_game();
}
Here, this took me awhile to fix actually, you had a lot of syntax errors or just logic errors. I wrote this in Visual C++, so, if I accidently used any C++ functions by accident, my mistake!
I fixed your seed too.
-snip-
Thank you so much for your reply! Your corrected code helped me out a lot and helped me see all my stupid mistakes! And thanks for fixing my seed cause the professor was really confusing me with it and I'd never used rand() even in C++.
After taking a break and looking at it again, I felt really dumb for some of the mistakes I made (semi colons after if statements is kinda a major fail) which solved most of the problems.
I guess thats what I get for learning a new language and learning to program in vim instead of an IDE both at once.
Also thank you Ikroth for your suggestions, you were right about the if else's in the first place :smile.gif:
You've put ';' right after some if-statements. Remove them and it will work.
(You've also put them after the {...} blocks. They're harmless there, but you can also remove those.)
Its amazing how you can stare at code for 2 hours and completely miss something simple like that. I feel pretty stupid for overlooking it. Thanks though :smile.gif:
This is my first C program, a very simple "guessing game", what I cant figure out is, when I guess above the number generated by rand(), 2 of my if statements fire off, and when I guess lower, all 3 of them fire off.
Im guessing something is wrong with my if comparisons or something else about C I didn't know Im missing. Are my if statements done correctly?
Any advice would be great.
Here is the output
Totally New at running servers? Click me!
I wonder if sansavarous is online...
I think the problem is that currently ALL the if statements are checked. Think about it, if you know that the guess is higher than the number, it's not going to be lower. It's a pointless thing to check.
Change from this style:
To:
So basically, the if statement will check normally, if the if statement doesn't go through, the next else or else if statement will go of like it was an if statement.
I can't verify this right now, but please reply back if I'm wrong (which is likely).
Thanks for your reply!
I want all the if statements to be checked but, I dont want them all to fire off. I want it to check each one, then depending on the number, which changes every time the program is fired up, execute that if statement.
I did try else if before but gcc doesn't like it.
Totally New at running servers? Click me!
I wonder if sansavarous is online...
That should work.
Thanks for your continued effort!
Hm, well that worked for the if detection, but it breaks me out of my loop. Ill figure out how to make it work though. Im really curious as to why it detects properly with the breaks.
Totally New at running servers? Click me!
I wonder if sansavarous is online...
I fixed your seed too.
Thank you so much for your reply! Your corrected code helped me out a lot and helped me see all my stupid mistakes! And thanks for fixing my seed cause the professor was really confusing me with it and I'd never used rand() even in C++.
After taking a break and looking at it again, I felt really dumb for some of the mistakes I made (semi colons after if statements is kinda a major fail) which solved most of the problems.
I guess thats what I get for learning a new language and learning to program in vim instead of an IDE both at once.
Also thank you Ikroth for your suggestions, you were right about the if else's in the first place :smile.gif:
Its amazing how you can stare at code for 2 hours and completely miss something simple like that. I feel pretty stupid for overlooking it. Thanks though :smile.gif:
Totally New at running servers? Click me!
I wonder if sansavarous is online...