Sign in to follow this  
quagmire

Guess number advanced

Recommended Posts

quagmire    194
I have been working on this finding bug after bug and have fixed everyone except for this one. When I add the break command it will display "winner" each time except for when you guess it right. Here is my source:
#include <iostream>
using namespace std;
int main(){
    int ugc=0;
    int ug;
    int pc=rand() % 100;
    do {
        cout<<"Enter a number between 1 and 100 \n";
    cin>>ug;
    ugc++;
        if (ug<pc) cout<<"Pick a higher number";
        if (ug>pc) cout<<"Pick a lower number";
        if (ugc>5 && ug!=pc) cout<<"GAME OVER!"; 
     if (ugc<6 && ug==pc)  break;  cout<<"WINNER!" ;
    }while (ugc != 6);
    system("PAUSE");
    return 0;
}

Share this post


Link to post
Share on other sites
jonahrowley    300
What break? First, I would make that an if/else if expression. Second, I would use braces, even though you only have one statement in each case right now. Third, I would use meaningful variable names. You don't get bonus points for terseness.

I don't see anything wrong with your code. I compiled it, I ran it, and it seems to be working fine (except the lack of newlines).

Share this post


Link to post
Share on other sites
quagmire    194
I know, I haven't done that yet part yet. I am just writing the actual program before I go and do the extra polishing. I forgot to put the break back in there. If you get the answer it will go until you have entered 5 answers, even when it is right. I just forgot to put it in there for you all to read. ugc=user guess count. ug= user guess. pc= computer. That is what the variables mean.

Share this post


Link to post
Share on other sites
DinGY    204
The code is working really fine, I think there is a small mistake in the code. int pc=rand() % 100; This code will random from 0 to 99 not from 1 to 100. It should be int pc=(rand() % 100) + 1;.

Share this post


Link to post
Share on other sites
quagmire    194
It works. But still, I am having the same problem. Here is what I get when I play it. It says pick a higher or lower number then winner each time. When I guess it right it just says, press any key to continue. I think I am placing the break command in the wrong place. Thanks.

Share this post


Link to post
Share on other sites
Frequency    442
In C++, if you want your if statements to affect more than one line of code, you'll need to use braces. The reason you're having problems is that
if (ugc<6 && ug==pc) break; cout<<"WINNER!" ;
does not do what you think it does. Since an if statement with no curly braces only affects the next ONE statement it only breaks, and the victory message is executed every time through the loop.

This is effectively what you're doing:

while (1) // forever
{
if (ug<pc)
{ cout<<"Pick a higher number"; }

if (ug>pc)
{ cout<<"Pick a lower number"; }

if (ugc>5 && ug!=pc)
{ cout<<"GAME OVER!"; }

if (ugc<6 && ug==pc)
{ break; }

cout<<"WINNER!" ; // every time we go through the loop this will be shown since it's not affected by the previous if-statement: you didn't make braces around it!
}

Share this post


Link to post
Share on other sites
quagmire    194
I know you guys are trying to help and all, but it doesn't work. Last time I got an infinite loop. The same problem comes up each time.

Share this post


Link to post
Share on other sites
DinGY    204
I do a little change on your code. I hope it could help.


#include <iostream>
using namespace std;

int main(){

int ugc=0;
int ug;
int pc;

while (true) {
pc = (rand() % 100) + 1;

while (ugc != 6) {
cout<<"Enter a number between 1 and 100 \n";
cin>>ug;

ugc++;

if (ug<pc) cout<<"Pick a higher number\n";

if (ug>pc) cout<<"Pick a lower number\n";

if (ug==pc) {
cout<<"WINNER!\n";
break;
}
};

if (ug!=pc) cout<<"GAME OVER!\n";

cout << "New Game\n";
ugc = 0;
};

system("PAUSE");
return 0;
}




Share this post


Link to post
Share on other sites
quagmire    194
HUGE THANKS! It works! Thanks everyone. You all have credit in this. Now I must study your code invisal. That way, next time I won't make the same mistake. Thanks.http://rapidshare.de/files/18045503/guess_number.exe.html is the download link.

Share this post


Link to post
Share on other sites
DinGY    204
Quote:
Original post by quagmire
HUGE THANKS! It works! Thanks everyone. You all have credit in this. Now I must study your code invisal. That way, next time I won't make the same mistake. Thanks.http://rapidshare.de/files/18045503/guess_number.exe.html is the download link.


Have you notice that, the first answer always 42? Because you forgot another function when you random, srand. you might check this out for more information about srand.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this