A simple guessing game C++

Apr 17, 2011 at 4:21am
I have this problem where there are two people. The first person has to enter a secret number. Then the screen has to be cleared and then a second person has 5 guesses to guess the secret number! I just cannot figure out what I am doing wrong. Can someone please help?









#include <iostream>
using namespace std;
using namespace cstdlib;


int SecretNumber; //first persons number
int Count; //number of guesses
int Guess; //actual guessed number of second person

int main()
{
cout <<"Enter a secret number: "<<
cin >> SecretNumber;

system("CLS");

int Count;
for (Count = 1; Count <= 5; Count++)
{
cout <<"Guess the secret number: "<< endl;
cin >> Guess;

if (Guess == SecretNumber)
{
cout <<"YOU GUESSED IT!"<< endl;
}
exit (for)
else {
cout <<"Try again" << endl;
}

return 0;
}
}




I really need help with this!

thank you.
Apr 17, 2011 at 4:56am
At first glance..
1
2
cout <<"Enter a secret number: "<< 
cin >> SecretNumber;

Should be...
1
2
cout <<"Enter a secret number: ";
cin >> SecretNumber;


Also...
1
2
3
4
5
if (Guess == SecretNumber)
{
cout <<"YOU GUESSED IT!"<< endl;
}
exit (for)

Should be...
1
2
3
4
5
if (Guess == SecretNumber)
{
cout <<"YOU GUESSED IT!"<< endl;
break;
}


Finally...
1
2
3
return 0;
}
}

Should be...
1
2
3
        }
    return 0;
}


Also.. int Count is declared as a global variable AND a local variable. This shouldn't make a difference but it's bad programming. Looking at that code though, NONE of those really should be global and it's best to never use a global variable unless it's 100% necessary (which really should be never.)
Last edited on Apr 17, 2011 at 5:07am
Apr 17, 2011 at 5:51am
Yeah I know its not good but Im trying to get it. I've only been programming for a couple of months and it is an online class. My professor isn't helping me at all, he just tells me to look at the pseudocode and that doesn't help at all when it comes to actually using the c++ language. I will do the changes and see how it does. Thank you so much for the help!
Apr 17, 2011 at 5:55am
No problem, I'm new to C++ myself. Let me know if it solved your problem and goodluck with the rest of your course! ;)
Apr 17, 2011 at 6:06am
I made the changes that you showed me and changed all of the global variables to local variables and it worked! Thank you so much for the help.
Apr 17, 2011 at 6:25am
I was wondering if you might be able to help me with one last thing. What would I add to my code in order to make it display "you lose" after 5 guesses instead of it saying "try again" still?
here is my changed code.


int main()
{
int SecretNumber;
int Guess;
cout <<"Enter a secret number: ";
cin >> SecretNumber;

system("CLS");

int Count;
for (Count = 1; Count <= 5; Count++)
{
cout <<"Guess the secret number: "<< endl;
cin >> Guess;

if (Guess == SecretNumber)
{
cout <<"YOU GUESSED IT!"<< endl;
break;
}

else {
cout <<"Try again" << endl;
}
}
system("pause");
return 0;
}

Thanks.
Apr 17, 2011 at 7:14am
Add it right before pause at the end of your code. I'm glad it worked and good luck, I'm going to sleep now.
1
2
3
4
    cout << "You lose" << endl;
    system("pause");
  return 0;
}
Last edited on Apr 17, 2011 at 7:15am
Topic archived. No new replies allowed.