So, i'm doing a little "virtual friend" kind of project:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::cout << "Hey!\n";
std::cout << "My name is Bob! Hope I can be your friend! :D\n";
std::cout << "But before all of my questions... would you like to be my friend?\n\n";
std::cout << "Select your answer: YES or NO? ";
string yes;
string no;
if (std::cin >> yes){
std::cout << "\n";
std::cout << "GOOD! Thank you for letting me know more of you! :D\n";
std::cout << "Then, I'll start with some simple questions!\n";
std::cout << "So tell me, what is your name?\n\n";
std::cout << "My name is: ";
string name;
std::cin >> name;
std::cout << "\n";
std::cout << "GOOD! So your name is " << name << ", such a nice name! :)\n";
std::cout << "So, " << name << "... I feel like I can only talk to someone once they tell me their age...\n";
std::cout << "I would love to know, how old are u " << name << "?\n\n";
std::cout << "I am: ";
int age;
std::cin >> age;
std::cout << "\n";
std::cout << "Hehe! Nice so you are " << age << " years old! :D\n";
std::cout << "Very good!\n";
} else if (std::cin >> no){
std::cout << "Oh... I'm sorry if I bothered you...\n";
std::cout << "See you one day then! :D\n";
}
return 0;
}
But I'm having a problem with my code, and I can't find what is wrong.., Supposedly, "Bob" starts asking questions after the user inputs a "yes": but in the case of a "no" he has to answer the following:
std::cout << "Oh... I'm sorry if I bothered you...\n";
std::cout << "See you one day then! :D\n";
I don't know why, but whenever I choose "yes" or "no", "Bob" starts making the questions, as you can see here:
Can someone help me with this..? I would really love that. (also some tips to improve the code).
I assume you wanted something closer to
string user_input;
std::cin >> user_input;
if (user_input == "yes")
{
// yes case
}
else if (user_input == "no")
{
// no case
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.