Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,578 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,941 people online right now. Registration is fast and FREE... Join Now!




guess the number unlimited

 
Reply to this topicStart new topic

guess the number unlimited

villalandron
2 Oct, 2008 - 05:07 AM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

Hello! I have this guess the number program made. The only help I need is making it to let continue to guess until you get it right. Here is what I have:

cpp
#include <iostream>  
#include <cstdlib>
#include <ctime>

using namespace std;

const int maxNumber = 1000;

int generateNumber()
{
return rand() % maxNumber + 1;
}


int main(void)
{
srand(time(0));
int guess;
char answer;
int guessCount = 1;

cout << "Welcome to Guess The Number" << endl;
cout << "I have a number between 1 and " << maxNumber << "." << endl;
cout << "Can you guess my number?" << endl;

int theNumber = generateNumber();

do
{
guessCount++;
cout << "Please type your guess: ";
cin >> guess;

if (guess > theNumber)
cout << "Too high...try again" << endl;
else if (guess < theNumber)
cout << "Too low...try again" << endl;
else
{
cout << "Excellent! You guessed the number! Would you like to play again? (Y/N)" << endl;

}
}
while((answer == 'Y' || answer == 'y'));

system("pause");
return 0;
}


Thank You for your help

MOD EDIT: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Guess The Number Unlimited
2 Oct, 2008 - 08:46 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
This is a perfect example of nested loops. Try this:
cpp
#include <iostream>
#include <ctime>
// cstdlib wasn't being used, so I removed it

using namespace std;

const int maxNumber = 1000;

int generateNumber()
{
return rand() % maxNumber + 1;
}


int main(void)
{
srand(time(0));
int guess;
char answer;
int guessCount;
int playCount = 0; // added bonus, let's count the number of times the game is played

cout << "Welcome to Guess The Number" << endl;
cout << "I have a number between 1 and " << maxNumber << "." << endl;
cout << "Can you guess my number?" << endl;

int theNumber = generateNumber();

do { // this loop handles the play again option
guessCount = 0; // initialise guessCount at the beginning of the game loop
playCount++; // increase the number of times played
do { // this loop handles the main game
cout << "Please enter your guess: ";
cin >> guess;
if (guess > theNumber)
cout << "Too high!" << endl;
else if (guess < theNumber)
cout << "Too low!" << endl;
guessCount++;
} while (guess > theNumber || guess < theNumber);

cout << "You got it in " << guessCount << " guesses! Congratulations!" << endl;
cout << "Play again? (Y/N) ";
cin >> answer;
} while((answer == 'Y' || answer == 'y'));

cout << "Awww, you're finished already?" << endl;
cout << "You only played " << playCount << " times!";

cin.get(); // pause (system("pause") is platform specific)
return EXIT_SUCCESS; // successful execution
}

I've commented all of my additions, so you can see what I did.

Hope this helps smile.gif
User is online!Profile CardPM
+Quote Post

villalandron
RE: Guess The Number Unlimited
2 Oct, 2008 - 03:10 PM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

QUOTE(gabehabe @ 2 Oct, 2008 - 09:46 AM) *

This is a perfect example of nested loops. Try this:
cpp
#include <iostream>
#include <ctime>
// cstdlib wasn't being used, so I removed it

using namespace std;

const int maxNumber = 1000;

int generateNumber()
{
return rand() % maxNumber + 1;
}


int main(void)
{
srand(time(0));
int guess;
char answer;
int guessCount;
int playCount = 0; // added bonus, let's count the number of times the game is played

cout << "Welcome to Guess The Number" << endl;
cout << "I have a number between 1 and " << maxNumber << "." << endl;
cout << "Can you guess my number?" << endl;

int theNumber = generateNumber();

do { // this loop handles the play again option
guessCount = 0; // initialise guessCount at the beginning of the game loop
playCount++; // increase the number of times played
do { // this loop handles the main game
cout << "Please enter your guess: ";
cin >> guess;
if (guess > theNumber)
cout << "Too high!" << endl;
else if (guess < theNumber)
cout << "Too low!" << endl;
guessCount++;
} while (guess > theNumber || guess < theNumber);

cout << "You got it in " << guessCount << " guesses! Congratulations!" << endl;
cout << "Play again? (Y/N) ";
cin >> answer;
} while((answer == 'Y' || answer == 'y'));

cout << "Awww, you're finished already?" << endl;
cout << "You only played " << playCount << " times!";

cin.get(); // pause (system("pause") is platform specific)
return EXIT_SUCCESS; // successful execution
}

I've commented all of my additions, so you can see what I did.

Hope this helps smile.gif


This worked wonderfully! Thanks!
User is offlineProfile CardPM
+Quote Post

villalandron
RE: Guess The Number Unlimited
4 Oct, 2008 - 05:22 AM
Post #4

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

QUOTE(villalandron @ 2 Oct, 2008 - 04:10 PM) *

QUOTE(gabehabe @ 2 Oct, 2008 - 09:46 AM) *

This is a perfect example of nested loops. Try this:
cpp
#include <iostream>
#include <ctime>
// cstdlib wasn't being used, so I removed it

using namespace std;

const int maxNumber = 1000;

int generateNumber()
{
return rand() % maxNumber + 1;
}


int main(void)
{
srand(time(0));
int guess;
char answer;
int guessCount;
int playCount = 0; // added bonus, let's count the number of times the game is played

cout << "Welcome to Guess The Number" << endl;
cout << "I have a number between 1 and " << maxNumber << "." << endl;
cout << "Can you guess my number?" << endl;

int theNumber = generateNumber();

do { // this loop handles the play again option
guessCount = 0; // initialise guessCount at the beginning of the game loop
playCount++; // increase the number of times played
do { // this loop handles the main game
cout << "Please enter your guess: ";
cin >> guess;
if (guess > theNumber)
cout << "Too high!" << endl;
else if (guess < theNumber)
cout << "Too low!" << endl;
guessCount++;
} while (guess > theNumber || guess < theNumber);

cout << "You got it in " << guessCount << " guesses! Congratulations!" << endl;
cout << "Play again? (Y/N) ";
cin >> answer;
} while((answer == 'Y' || answer == 'y'));

cout << "Awww, you're finished already?" << endl;
cout << "You only played " << playCount << " times!";

cin.get(); // pause (system("pause") is platform specific)
return EXIT_SUCCESS; // successful execution
}

I've commented all of my additions, so you can see what I did.

Hope this helps smile.gif


This worked wonderfully! Thanks!


Now, how would you put a limit on the number of guesses a person can make?

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Guess The Number Unlimited
4 Oct, 2008 - 05:52 AM
Post #5

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
Add another check in the inner do/while loop against guessCount?

Just think about it. Write it out on paper as a flow chart. It's not difficult, so don't try to make it more difficult than it needs to be!
User is offlineProfile CardPM
+Quote Post

villalandron
RE: Guess The Number Unlimited
5 Oct, 2008 - 04:34 PM
Post #6

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

QUOTE(JackOfAllTrades @ 4 Oct, 2008 - 06:52 AM) *

Add another check in the inner do/while loop against guessCount?

Just think about it. Write it out on paper as a flow chart. It's not difficult, so don't try to make it more difficult than it needs to be!


I have been trying on my own and can't get it. Here is what I got:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int maxNumber = 1000;

int generateNumber()
{
return rand() % maxNumber + 1;
}

bool playAgain()
{
char answer;
cout << "Would you like to play again(Y or N)? ";
cin >> answer;
cin.ignore();
return (answer == 'Y' || answer == 'y');
}
int main(void)
{
srand(time(0));
int guess;
char answer;
int guessCount = 1;
int playCount = 0;

cout << "I have a number between 1 and " << maxNumber << "." << endl;
cout << "Can you guess my number?" << endl;

int theNumber = generateNumber();

do {
guessCount = 0;
playCount++;

cout << "Please type your first guess: ";
cin >> guess;
if (guess > theNumber)
cout << "Too high...try again!" << endl;
else if (guess < theNumber)
cout << "Too low...try again!" << endl;
guessCount++;
} while ((guess > theNumber || guess < theNumber) || (guessCount == 7));

cout << "Excellent! You guessed the number! Would you like to play again? (Y/N) ";
cin >> answer;

if (playAgain())
{
theNumber = rand() % maxNumber + 1;
guessCount = 1;
}
if (playCount == 7)
{
cout << "Sorry, you failed to guess the correct number ("
<< theNumber << ")" << endl;

} while((answer == 'Y' || answer == 'y'));

cin.get();
return EXIT_SUCCESS;
}

Thank You for your help
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month