Guess number advanced

Started by
8 comments, last by DinGY 18 years ago
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;
}
cout<<"cout<<'' this is my siggy''";
Advertisement
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).
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.
cout<<"cout<<'' this is my siggy''";
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;.
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
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.
cout<<"cout<<'' this is my siggy''";
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!}
It only takes one mistake to wake up dead the next morning.
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.
cout<<"cout<<'' this is my siggy''";
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;}
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
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.
cout<<"cout<<'' this is my siggy''";
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.
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"

This topic is closed to new replies.

Advertisement