You should use doubleQuotes for string "Yes" "No"
How do you use Yes/No to input your answer C++?
-
31-05-2022 - |
سؤال
I have my program here.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout << "say yes or no" << endl;
cin >> input;
if(input == Yes)
{
cout << "test" << endl;
}
else if(input == No)
{
cout << "test123" << endl;
}
system("pause");
}
It says that Yes and No are undefined? Please help I am new to c++
المحلول
نصائح أخرى
First, Yes and No without double quote are read as variable by the compilator, and you didn't declare any Yes or No variable.
Second, I don't think you can compare String like that in C++ : you have to use compare ( http://www.cplusplus.com/reference/string/string/compare/ ) so the prog will be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout << "say yes or no" << endl;
cin >> input;
if(input.compare("Yes"))
{
cout << "test" << endl;
}
else if(input.compare("No"))
{
cout << "test123" << endl;
}
system("pause");
}
لا تنتمي إلى StackOverflow