Page 1 of 2
Help on a game.
Posted: Tue May 19, 2009 7:42 am
by Rhys
Hi,
I haven't been programming for very long, and this is my first project that has a goal, and isn't copied from a tutorial. The game I'm trying to make is simple, you couldn't get it wrong if you tried, but it's just me teaching myself. What I want to happen is for the user to input a value for x, then output a question, like "is x > 5?", then the user inputs Y or N for confirmation.
I know how to do "if x > 5" then output this or that, but I don't know how to get input.
Here's the source;
Code: Select all
#include <string>
#include <iostream>
using namespace std;
int main()
{
// Variables.
string input;
float x = 0.0;
float y = 0.0;
float z = 0.0;
// Main Function.
system("TITLE The Number Game!");
cout << "The Number Game!" << endl;
cout << "To play the number game, enter a number between 1 and 10." << endl << endl;
cin >> x;
cout << endl << endl;
cout << "Number entered: " << x << endl;
cout << "Let's begin the game. You need to answer a series or questions using Y and N. These resemble Yes and NO."
" You must use capitals. Here comes the first question." << endl << endl;
cout << "Is " << x << "larger than five(5)?" << endl;
if (x > 5){
cout << "Input your answer." << endl;
} else {
cout << "Input your answer." << endl;
}
cin >> input;
if (input == "Y"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
system("pause");
return 0;
}
Sorry if this is in the wrong place or not allowed, I'm no regular, but if it's wrong just say, if not then your help is appreciated.
Edit: Was also wondering how to remove that "Press any key to continue" at the end of console applications.
Re: Help on a game.
Posted: Tue May 19, 2009 7:55 am
by gordon
I would help you with the program but I'm a little busy so I'll just answer one of your questions.
Edit: Was also wondering how to remove that "Press any key to continue" at the end of console applications.
The message comes with the:
Remove that and your program will end without requiring the user to press a key and without a message.
Re: Help on a game.
Posted: Tue May 19, 2009 7:56 am
by Falco Girgis
You "don't know how" to get input? It looks like you do. You're using cin...
Re: Help on a game.
Posted: Tue May 19, 2009 8:09 am
by gordon
Code: Select all
cout << "Is " << x << "larger than five(5)?" << endl;
if (x > 5){
cout << "Input your answer." << endl;
} else {
cout << "Input your answer." << endl;
}
cin >> input;
if (input == "Y"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
}
Since gyrovorbis has concluded that you do know how to use input I'll suggest how you could achieve your goal. I have taken the section above as I think it is what needs changed, checking the input for Y or N should be done within an if statement that checked whether the number was above or below 5. Right now it outputs yes or no , regardless of the number entered.
Something like this should be done to that section:
Code: Select all
cout << "Is " << x << "larger than five(5)?" << endl;
cout << "Input your answer." << endl;
cin >> input;
if (x > 5){
if (input == "Y"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
} else {
if (input == "N"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
}
}
Re: Help on a game.
Posted: Tue May 19, 2009 8:16 am
by MadPumpkin
looks they're helping you just fine :D
by the way...
you could have named your thread anything you want
and a lot of people would assume it was for a game haha
Re: Help on a game.
Posted: Tue May 19, 2009 8:27 am
by Kros
gordon wrote:I would help you with the program but I'm a little busy so I'll just answer one of your questions.
Edit: Was also wondering how to remove that "Press any key to continue" at the end of console applications.
The message comes with the:
Remove that and your program will end without requiring the user to press a key and without a message.
To further elaborate, removing the pause command isn't going to immediately fix things. Without that there, it'll likely exit as soon as your final cout occurs. If this is what you want, then by all means, just remove the system("pause");. Otherwise, you need to replace it with something like:
Code: Select all
cin.ignore();
cin.get();
return 0;
This will stop the program from exiting until a key is pressed but, no message will show.
gordon wrote:
Something like this should be done to that section:
Code: Select all
cout << "Is " << x << "larger than five(5)?" << endl;
cout << "Input your answer." << endl;
cin >> input;
if (x > 5){
if (input == "Y"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
} else {
if (input == "N"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
}
}
That is some crazy weird indentation.
Re: Help on a game.
Posted: Tue May 19, 2009 8:42 am
by gordon
That is some crazy weird indentation.
Yeah I apologise for that. TAB , SPACE etc can do unexpected things within a webbrowser.....
Re: Help on a game.
Posted: Tue May 19, 2009 11:14 am
by MarauderIIC
This
Code: Select all
if (x > 5){
if (input == "Y"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
} else {
if (input == "N"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
}
could be condensed to
Code: Select all
//"Correct" if x is greater than 5 and the user answers 'yes'
//"Correct" if x is less than 5 and the user answers 'no'
if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
cout << "Correct!";
} else {
cout << "Wrong.";
}
Re: Help on a game.
Posted: Tue May 19, 2009 12:59 pm
by gordon
MarauderIIC wrote:This
Code: Select all
if (x > 5){
if (input == "Y"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
} else {
if (input == "N"){
cout << "Correct!";
} else {
cout << "Wrong.";
}
}
could be condensed to
Code: Select all
//"Correct" if x is greater than 5 and the user answers 'yes'
//"Correct" if x is less than 5 and the user answers 'no'
if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
cout << "Correct!";
} else {
cout << "Wrong.";
}
good call.
Re: Help on a game.
Posted: Tue May 19, 2009 2:42 pm
by Ginto8
I would HIGHLY frown upon this, but for the sake of conversation I'd like to point out that the DOS command "pause" can take the parameter ">nul", which stops "press any key to continue..." from showing, so it is possible to do system("pause >nul") to solve your problem. However, getchar(), getch(), and cin.sync() with cin.get() are all MUCH better choices.
Re: Help on a game.
Posted: Tue May 19, 2009 6:18 pm
by Rhys
MarauderIIC wrote: Code: Select all
//"Correct" if x is greater than 5 and the user answers 'yes'
//"Correct" if x is less than 5 and the user answers 'no'
if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
cout << "Correct!";
} else {
cout << "Wrong.";
}
When I implement this, (whole code);
Code: Select all
#include <string>
#include <iostream>
using namespace std;
int main()
{
// Variables.
string input;
float x = 0.0;
float y = 0.0;
float z = 0.0;
// Main Function.
system("TITLE The Number Game!");
cout << "The Number Game!" << endl;
cout << "To play the number game, enter a number between 1 and 10." << endl << endl;
cin >> x;
cout << endl << endl;
cout << "Number entered: " << x << endl;
cout << "Let's begin the game. You need to answer a series or questions using Y and N. These resemble Yes and NO."
" You must use capitals. Here comes the first question." << endl << endl;
cout << "Is " << x << "larger than five(5)?" << endl;
if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
cout << "Correct!";
} else {
cout << "Wrong.";
}
cin.ignore();
cin.get();
return 0;
I don't get to answer the question is x > 5. It simply states "Wrong", even when x is larger than 5.
Re: Help on a game.
Posted: Tue May 19, 2009 6:29 pm
by herby490
Here try this code I changed a few things about it.
First i created a char that is used for input of the choice. I also made it so it will see if the input is greater than or equal the the choice so if the user selects five he won't be wrong every time.
Code: Select all
#include <string>
#include <iostream>
using namespace std;
int main()
{
// Variables.
string input;
double x = 0.0;
float y = 0.0;
float z = 0.0;
char a;
// Main Function.
system("TITLE The Number Game!");
cout << "The Number Game!" << endl;
cout << "To play the number game, enter a number between 1 and 10." << endl << endl;
cin >> x;
cout << endl << endl;
cout << "Number entered: " << x << endl;
cout << "Let's begin the game. You need to answer a series or questions using Y and N. These resemble Yes and NO."
<< " You must use capitals. Here comes the first question." << endl << endl;
cin >> a;
cout << "Is " << x << " larger than five(5)?" << endl;
if ( ( a == 'Y' && x >= 5) || (a == 'N' && x < 5) ){
cout << "Correct!";
} else {
cout << "Wrong.";
}
cin.ignore();
cin.get();
return 0;
}
Re: Help on a game.
Posted: Tue May 19, 2009 7:57 pm
by dandymcgee
Did you fix the "Press any key to continue..." message?
If it still does it without the System("pause") and you are using Code::Blocks, try unchecking "Pause when execution ends" in the "Build Targets" tab of the Project->Properties window.
Re: Help on a game.
Posted: Tue May 19, 2009 9:28 pm
by MarauderIIC
Rhys: probably because it's caps-sensitive.... and it looks like you never getline(cin, input), so input is never assigned. Oh yeah, and < 5 should be <= 5.
Re: Help on a game.
Posted: Thu May 21, 2009 2:04 am
by Rhys
Thanks for the help, the programming book I ordered just arrived, that should help.