[try Beta version]
Not logged in

 
Yes or No question, if "No" return to previous question

Sep 27, 2019 at 12:56am
Hello, I am brand new to C++ programming. Sorry if this is simple, I looked it up a lot.

I am wondering if there is a way to press Y or N to a question, and if No return to the question answered. For example, my code asks for the users name. After they type their name, it asks if it is correct. If they made a mistake, and press N, it will return to the question. Is this possible? I will also be adding more questions later and wondering if I will be able to return to those questions in the event of No answer. Thank you!

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

int main()
{
    char response;

    std::string charName;
    std::cout << "What is your name?" << std::endl;
    std::getline(std::cin, charName);
    std::cout << "So your name is " << charName << "? (Y/N)" << std::endl;
    std::cin >> response;
    if(response=='Y'||response=='Y')
    {
        std::cout << "Hello, " << charName << ", and welcome to Honeywood!" << std::endl;
    }
    else if(response=='N'||response=='N')

    //not sure what to do here

    return 0;
}
Sep 27, 2019 at 1:23am
Also I don't expect anyone to write the code for me, but to just point me in the right direction. :D Thank you very much!
Sep 27, 2019 at 2:00am
Both of your if comparisons are checking upper case characters.

Don't forget #include <string> when using std::string.

Wrap lines 8 - 18/19 in a while(true) loop and use break or continue in the body of your if blocks.

https://en.cppreference.com/w/cpp/language/break

https://en.cppreference.com/w/cpp/language/continue

Last edited on Sep 27, 2019 at 2:02am
Topic archived. No new replies allowed.