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

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




Guess number limit guesses error

 
Reply to this topicStart new topic

Guess number limit guesses error

villalandron
25 Sep, 2008 - 07:15 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

Hello! I am writing this code for the Guess the number game. I have been trying to limit the number of guesses to 7 but can't figure it out. I thougth it was by using a while loop but it doesn't seem to work. Here is my code. Thank You!

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

int main()
{
srand(time(0));
int theNumber = rand() % 100 + 1;
int guess = 1;
char answer;
int max = 1000;
int min = 1;



cout << "Welcome to Guess The Number" << endl;

{

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

{
cout << "Please type your first guess: " << endl;
cin >> guess;


if (guess > theNumber)
{
cout << "Too high...try again" << endl;
guess++;
}
if (guess < theNumber)
{
cout << "Too low...try again" << endl;
guess++;
}
}
if (guess == theNumber);
cout << "Excellent! You guessed the number! Would you like to play again? (y or n)?" << endl;

while (guess <= 7)
cin >> answer;
system ("pause");
return 0;
}


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

red_4900
RE: Guess Number Limit Guesses Error
25 Sep, 2008 - 10:32 PM
Post #2

Code Dreamers
****

Joined: 22 Feb, 2008
Posts: 820



Thanked: 11 times
My Contributions
You are very close to the finish. This is easier explained using just code. Let the code talks laugh.gif
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void)
{
srand(time(0));
int theNumber = rand() % 10 + 1;
int guess; //no point in initializing this value
char answer;
int guessCount = 1; //initialize counter to 1. since do-while loops will already count once
//you don't need max or min value

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

do
{
answer = NULL; //set the answer to NULL so that the loop will not continue in the second round
guessCount++;
cout << "Please type your guess: " << endl;
cin >> guess;

if (guess > theNumber)
cout << "Too high...try again" << endl;
if (guess < theNumber)
cout << "Too low...try again" << endl;
if (guess == theNumber) //careful not to put semicolon here
{
cout << "Excellent! You guessed the number! Would you like to play again? (y or n)?" << endl;
cin >> answer;
if(answer == 'y')
guessCount = 1;
}
}while((answer == 'y') || (guessCount<=7));

system ("pause"); //I would advise against using system function. but, oh well..never mind
return 0;
}


I already added some comment in my code. But feel free to ask if there's anything you don't understand.

Hope that helps you. smile.gif

User is online!Profile CardPM
+Quote Post

villalandron
RE: Guess Number Limit Guesses Error
26 Sep, 2008 - 01:46 PM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

QUOTE(red_4900 @ 25 Sep, 2008 - 11:32 PM) *

You are very close to the finish. This is easier explained using just code. Let the code talks laugh.gif
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void)
{
srand(time(0));
int theNumber = rand() % 10 + 1;
int guess; //no point in initializing this value
char answer;
int guessCount = 1; //initialize counter to 1. since do-while loops will already count once
//you don't need max or min value

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

do
{
answer = NULL; //set the answer to NULL so that the loop will not continue in the second round
guessCount++;
cout << "Please type your guess: " << endl;
cin >> guess;

if (guess > theNumber)
cout << "Too high...try again" << endl;
if (guess < theNumber)
cout << "Too low...try again" << endl;
if (guess == theNumber) //careful not to put semicolon here
{
cout << "Excellent! You guessed the number! Would you like to play again? (y or n)?" << endl;
cin >> answer;
if(answer == 'y')
guessCount = 1;
}
}while((answer == 'y') || (guessCount<=7));

system ("pause"); //I would advise against using system function. but, oh well..never mind
return 0;
}


I already added some comment in my code. But feel free to ask if there's anything you don't understand.

Hope that helps you. smile.gif


Thanks for your help!

These problems seem so easy, especially after working on them for several hours and after looking at the fixed code, it makes me feel like the answer was right in my face. Thanks for your help. All I have to do is now figure out How to output a message after the seventh try and I'll be done. Thanks!

User is offlineProfile CardPM
+Quote Post

villalandron
RE: Guess Number Limit Guesses Error
26 Sep, 2008 - 02:54 PM
Post #4

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

QUOTE(villalandron @ 26 Sep, 2008 - 02:46 PM) *

QUOTE(red_4900 @ 25 Sep, 2008 - 11:32 PM) *

You are very close to the finish. This is easier explained using just code. Let the code talks laugh.gif
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void)
{
srand(time(0));
int theNumber = rand() % 10 + 1;
int guess; //no point in initializing this value
char answer;
int guessCount = 1; //initialize counter to 1. since do-while loops will already count once
//you don't need max or min value

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

do
{
answer = NULL; //set the answer to NULL so that the loop will not continue in the second round
guessCount++;
cout << "Please type your guess: " << endl;
cin >> guess;

if (guess > theNumber)
cout << "Too high...try again" << endl;
if (guess < theNumber)
cout << "Too low...try again" << endl;
if (guess == theNumber) //careful not to put semicolon here
{
cout << "Excellent! You guessed the number! Would you like to play again? (y or n)?" << endl;
cin >> answer;
if(answer == 'y')
guessCount = 1;
}
}while((answer == 'y') || (guessCount<=7));

system ("pause"); //I would advise against using system function. but, oh well..never mind
return 0;
}


I already added some comment in my code. But feel free to ask if there's anything you don't understand.

Hope that helps you. smile.gif


Thanks for your help!

These problems seem so easy, especially after working on them for several hours and after looking at the fixed code, it makes me feel like the answer was right in my face. Thanks for your help. All I have to do is now figure out How to output a message after the seventh try and I'll be done. Thanks!


I think I am not using the right symbol or something but I am trying to have the program to say "Sorry, you failed to guess the correct number...would you like to play again?(Y or N)?". I will put a comment where I tried to have the program do that. Any input or advise will greatly help me. Thank You! Here's the program
cpp

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

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 1000." << endl;
cout << "Can you guess my number?" << endl;

do
{
int theNumber = rand() % 10 + 1;
answer = NULL;
guessCount++;
cout << "Please type your guess: " << endl;
cin >> guess;

if (guess > theNumber)
cout << "Too high...try again" << endl;
if (guess < theNumber)
cout << "Too low...try again" << endl;
if (guess == theNumber)
{
cout << "Excellent! You guessed the number! Would you like to play again? (y or n)?" << endl;
cin >> answer;
if(answer == 'y')
guessCount = 1;
}
if (guessCount == 7)
cout "Sorry, you failed to guess the correct number...vould you like to play again?(Y or N)?" << endl;
// This is the way I thought this would work. Any help will be appreciatted.

}while((answer == 'Y' || answer == 'y') || (guessCount<=7));

system ("pause");
return 0;
}



And here are the erro messages:

1>.\ITS275FinalExamVilla.cpp(8) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>.\ITS275FinalExamVilla.cpp(37) : error C2143: syntax error : missing ';' before 'string'
1>.\ITS275FinalExamVilla.cpp(37) : error C2563: mismatch in formal parameter list
1>.\ITS275FinalExamVilla.cpp(37) : error C2568: '<<' : unable to resolve function overload
1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1>Build log was saved at "file://c:\Users\Julio\Documents\Visual Studio 2008\Projects\ITS275FinalExamVilla\ITS275FinalExamVilla\Debug\BuildLog.htm"
1>ITS275FinalExamVilla - 3 error(s), 1 warning(s)

**EDIT** code.gif
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Guess Number Limit Guesses Error
26 Sep, 2008 - 05:44 PM
Post #5

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
See if this helps:
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int maxNumber = 100;

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;

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!" << endl;
if (playAgain())
{
theNumber = rand() % maxNumber + 1;
guessCount = 1;
}
}

if (guessCount == 7)
{
cout << "Sorry, you failed to guess the correct number ("
<< theNumber << ")" << endl ;
if (playAgain())
{
theNumber = generateNumber();
guessCount = 1;
}
}
}
while((answer == 'Y' || answer == 'y') || (guessCount < 7));

cout << "Press Enter to continue";
cin.get(answer);
return 0;
}

User is offlineProfile CardPM
+Quote Post

villalandron
RE: Guess Number Limit Guesses Error
27 Sep, 2008 - 05:49 AM
Post #6

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 23

QUOTE(JackOfAllTrades @ 26 Sep, 2008 - 06:44 PM) *

See if this helps:
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int maxNumber = 100;

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;

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!" << endl;
if (playAgain())
{
theNumber = rand() % maxNumber + 1;
guessCount = 1;
}
}

if (guessCount == 7)
{
cout << "Sorry, you failed to guess the correct number ("
<< theNumber << ")" << endl ;
if (playAgain())
{
theNumber = generateNumber();
guessCount = 1;
}
}
}
while((answer == 'Y' || answer == 'y') || (guessCount < 7));

cout << "Press Enter to continue";
cin.get(answer);
return 0;
}



Thank you for your help. I finally see the light at the end of the tunnel.

User is offlineProfile CardPM
+Quote Post

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

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