-
October 4th, 2009, 02:57 PM
#1
Please Help me with this program.
I am trying to ask the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again". The program should also keep a count of the number of guesses that the user makes. The program should use a loop that repeats until the user correctly guesses the random number. The program should display the random number as well as the number of guesses. This is what I have so far but my loop is not working. Help!!!!
Code:
//This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
cout << "Please try to guess a number from 1-100: ";
cin >> number;
}
while (number < 1 || number > 100)
cout << "Try again. Please try to guess a number from 1-100: ";
system("pause");
return 0;
}
-
October 4th, 2009, 03:20 PM
#2
Re: Please Help me with this program.
Code:
//This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
cout << "Please try to guess a number from 1-100: ";
}
while(number != seed)
{
cin >> number;
if(numb > seed)
cout << "Number is too high!\n";
else if(numb < seed)
cout << "Number is too low!\n";
}
cout << "Congratulation! You won the game!\n";
system("pause");
return 0;
}
I'll give you the code, but this is a very easy task that you had to figure on your own.
-
October 4th, 2009, 04:12 PM
#3
Re: Please Help me with this program.
Originally Posted by salehhamadeh
Code:
//This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
cout << "Please try to guess a number from 1-100: ";
//}
while(number != seed)
{
cin >> number;
if(numb > seed)
cout << "Number is too high!\n";
else if(numb < seed)
cout << "Number is too low!\n";
}
cout << "Congratulation! You won the game!\n";
system("pause");
return 0;
}
I'll give you the code, but this is a very easy task that you had to figure on your own.
That and your "{" and "}" were not well balanced. Your "main" was ending at my quote (where it was closed). Once the main closed, the rest of the code could not compile.
Also,
Code:
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
It is a very bad idea to use the same variable for things things that have nothing in common. You should have a "seed" variable, and a "numToGuess" variable.
Last edited by monarch_dodra; October 4th, 2009 at 04:14 PM.
-
October 4th, 2009, 04:29 PM
#4
Re: Please Help me with this program.
Originally Posted by salehhamadeh
Code:
//This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
cout << "Please try to guess a number from 1-100: ";
}
while(number != seed)
{
cin >> number;
if(numb > seed)
cout << "Number is too high!\n";
else if(numb < seed)
cout << "Number is too low!\n";
}
cout << "Congratulation! You won the game!\n";
system("pause");
return 0;
}
I'll give you the code, but this is a very easy task that you had to figure on your own.
Thank you so much for your help.
Originally Posted by monarch_dodra
That and your "{" and "}" were not well balanced. Your "main" was ending at my quote (where it was closed). Once the main closed, the rest of the code could not compile.
Also,
Code:
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
It is a very bad idea to use the same variable for things things that have nothing in common. You should have a "seed" variable, and a "numToGuess" variable.
Can you please explain why this was a bad idea?? Which variables have nothing in common?
-
October 4th, 2009, 05:41 PM
#5
Re: Please Help me with this program.
You're re-using the seed variable for something totally unrelated (the generated number). That's what he means.
-
October 4th, 2009, 07:19 PM
#6
Re: Please Help me with this program.
Thank you all for the input. This is how the code looks
Code:
//This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
cout << "Please try to guess a number from 1-100: ";
while(number != seed)
{
cin >> number;
if(number > seed)
cout << "\nNumber is too high!\n";
if(number < seed)
cout << "\nNumber is too low!\n";
else if(number < 1 || number > 100)
cout << "Number is not between 1-100. Try again.\n";
}
cout << "\nCongratulations! You won the game!\n";
system("pause");
return 0;
}
else if(number < 1 || number > 100)
cout << "Number is not between 1-100. Try again.\n";
I am having problems executing this. It couts but it also couts that number is to high ( if(number > seed)
cout << "\nNumber is too high!\n"; )
I don't want this I only want to cout that number is not between 1-100. What am I missing? Also which formula or function can I use to cout how many time user tried to guess the number?? cin.get??
-
October 4th, 2009, 10:14 PM
#7
Re: Please Help me with this program.
Code:
if(number > seed)
cout << "\nNumber is too high!\n";
if(number < seed)
cout << "\nNumber is too low!\n";
else if(number < 1 || number > 100)
This is wrong. The very first thing you should test for is to see if the number is in range (between 1 and 100). Then you test whether it is > seed and then test for < seed.
Also, take a look at the code I have above closely. You only do a test for out of range if the number is < seed? That doesn't sound right. What happens if the number is too high? First, the number is > seed, so you output "Number is too high", regardless of whether it is in range or not.
Make it simple -- test for the out of range first and get that out of the way. Once it is established that the number is in range, then you test if the number is < seed or > seed.
Regards,
Paul McKenzie
-
October 4th, 2009, 10:20 PM
#8
Re: Please Help me with this program.
Originally Posted by Kl2eativ
Also which formula or function can I use to cout how many time user tried to guess the number?? cin.get??
You don't need a formula or function. All you need is a counter starting at 0 and increment it each time the loop is executed to do the guess.
Regards,
Paul McKenzie
-
October 4th, 2009, 10:24 PM
#9
Re: Please Help me with this program.
Originally Posted by Paul McKenzie
Code:
if(number > seed)
cout << "\nNumber is too high!\n";
if(number < seed)
cout << "\nNumber is too low!\n";
else if(number < 1 || number > 100)
This is wrong. The very first thing you should test for is to see if the number is in range (between 1 and 100). Then you test whether it is > seed and then test for < seed.
Also, take a look at the code I have above closely. You only do a test for out of range if the number is < seed? That doesn't sound right. What happens if the number is too high? First, the number is > seed, so you output "Number is too high", regardless of whether it is in range or not.
Make it simple -- test for the out of range first and get that out of the way. Once it is established that the number is in range, then you test if the number is < seed or > seed.
Regards,
Paul McKenzie
Awsome Thank you... The only thing I need is how do I get how many time the user tried to guess the number?
-
October 4th, 2009, 10:39 PM
#10
Re: Please Help me with this program.
Originally Posted by Kl2eativ
Awsome Thank you... The only thing I need is how do I get how many time the user tried to guess the number?
I mentioned it in my last post.
Regards,
Paul McKenzie
-
October 4th, 2009, 10:45 PM
#11
Re: Please Help me with this program.
Originally Posted by Paul McKenzie
I mentioned it in my last post.
Regards,
Paul McKenzie
I did what you recommended and it worked great. Please take a look.
Code:
/This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
unsigned seed = time(0);
srand(seed);
seed = 1 + rand() % 100;
cout << "Please try to guess a number from 1-100: ";
while(number != seed)
{
cin >> number;
if(number < 1 || number > 100)
cout << "\nNumber is not between 1-100. Try again: ";
else if(number > seed)
cout << "\nNumber is too high! Try again: ";
else if(number < seed)
cout << "\nNumber is too low! Try again: ";
}
cout << "\nThe number was: " << number << endl;
cout << "\nCongratulations! You won the game!\n\n";
system("pause");
return 0;
}
The only thing I need is at the end after the cout statement The number was, I need it also to Say You have made "x" amount of attempts!!
Last edited by Kl2eativ; October 4th, 2009 at 11:07 PM.
-
October 4th, 2009, 11:48 PM
#12
Re: Please Help me with this program.
Originally Posted by Kl2eativ
The only thing I need is at the end after the cout statement The number was, I need it also to Say You have made "x" amount of attempts!!
Well, this is going to be the third time I've mentioned it:
You need another variable to keep count of how many times the loop is executed. Output the value of that counter at the end.
Regards,
Paul McKenzie
-
October 4th, 2009, 11:56 PM
#13
Re: Please Help me with this program.
Originally Posted by Paul McKenzie
Well, this is going to be the third time I've mentioned it:
You need another variable to keep count of how many times the loop is executed. Output the value of that counter at the end.
Regards,
Paul McKenzie
I did not see where u mentioned to create another value for the loop. Or is it me that just can't read?? Thank you for your help and time!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|