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;
#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.
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:
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
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
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:
//"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.";
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
//"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.";
}
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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
//"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.";
}
#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.
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.
#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;
}
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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.