So, I'm completely new to C++ and I chose it as my first programming language. Now I've decided on this one and I realize how powerful and hard this language might be to learn but none the less i'd like to get good at how to program in it, so I want to stick to C++. So I've been writing beginning programs with nano and compiling with g++, and I'm reading out of a book which is programming principles and practices by Bjarne Stroustrup. So I wrote this little code but what I want it to do is recognize the answer and give me a different option.
Would you like to play a game? y/n
so if you where to answer no it would give you a different option like:
What would you like to do?
or if you answered yes it would give something like:
What game would you like to play?
I'd also like to say in advance that i'm sorry if this post is on somewhere else but I haven't been able to find the answer for this particular question, here is the code I have written, I got the idea from a 1980's movie called war games lol.
#include<iostream>
using namespace std;
int main()
{
cout<<"hello, please enter your first and last name and age\n";
string name;
string name2;
int age;
cin>>name>>name2;
cin>>age;
cout<<"Hello,"<<name<<name2<<" (age"<<age<<")\n";
cout<<"Would you like to play a game?\n";
string yes;
cin>>yes;
cout<<"What game would you like to play?" " name"<<name<<"?\n";
string Chess;
cin>>Chess;
cout<<"Would you like to go first?\n";
return 0;
Try to name your variables so that you know what they're for by reading them. For example, in your code, the variable yes would be much better named userInput.
1 2 3 4 5 6 7 8 9 10 11
cout<<"Would you like to play a game? (y or n)\n";
string userInput;
cin >> userInput;
if (userInput == "y")
{
cout << "You said yes.";
}
elseif (userInput == "n")
{
cout << "You said no.";
}
Thank you, I do understand what that means an I'm going to try it as soon as I get a chance, I appreciate the feed back knowing that this is probably such a beginner question.