First Project Help

Hello, I am brand new to C++. I'm making a basic little text-based adventure thing as one of my first projects, and I don't know how to make different answers to questions display different information.

For example, my code is asking if you accept the name you have entered. How do I make it so that if they say "yes" it goes onto the next bit of information or question, and if they say "no" it asks the question again?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>

using namespace std;

int main()
{
string name;
string yayornay;
    cout << "Dungeons and Dragons" << endl;
    cout << " " << endl;
    cout << "This game is by Archie Kennedy." << endl;
    cout << " " << endl;
    cout << "State your name, Traveller!" << endl;
    cin >> name;
    cout << "" << name << ", huh?" << endl;
    cout << "Do you accept the name " << name << "?" << endl;
    cin >> yayornay;
       
}
I suggest you to read this, http://www.cplusplus.com/doc/tutorial/control/ .
You could use do-while loop to get desired name.
Maybe something like this, maybe use a loop if it's a no / NO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
string name;
string yayornay;
    cout << "Dungeons and Dragons" << endl;
    cout << " " << endl;
    cout << "This game is by Archie Kennedy." << endl;
    cout << " " << endl;
    cout << "State your name, Traveller!" << endl;
    cin >> name;
    cout << "" << name << ", huh?" << endl;
    cout << "Do you accept the name " << name << "?" << endl;
    cin >> yayornay;
    if(yayornay == "yes" || yayornay == "YES")
    {
        // Continue
    }  
    
    while(yayornay == "no" || yayornay == "NO")
    {
        // Enter new name.
        // Ask again.
    }
}
Last edited on
Topic archived. No new replies allowed.