+ 1

[SOLVED] How do I store string answers with "cin"?

I'm trying to make code that .. if I type "yes" something pops up and if i type "no" something pops up. I know how to do to it with number. For example: 1 is yes and 3 is no. Here is my code sorry if it is messy. #include <iostream> #include <string> using namespace std; int main() { int a; // I know this must be string. cout << "Do u want to learn code?\n"; cout << "yes or no?"; <censored the rest : profanity & char-limit> - CipherFox

21st Aug 2017, 7:47 PM
Jordi
Jordi - avatar
7 Antworten
+ 6
Jordi de Pelseneer I understand that programming can be frustrating sometimes, BELIEVE me ... but for future reference, please try to avoid posting profanity to the Q&A.
9th May 2018, 8:30 PM
Fox
Fox - avatar
+ 2
You can do this the easiest way. Just change "int a;" to "string a;" and in your "if" type "if(a == "yes")" and "if(a == "no")" . ( cin works the same way for strings)
21st Aug 2017, 8:14 PM
SKELU21
SKELU21 - avatar
+ 1
i did that but i get an error that yes isn't declared
21st Aug 2017, 8:20 PM
Jordi
Jordi - avatar
+ 1
oooh i forgot the "" stupid me
21st Aug 2017, 8:20 PM
Jordi
Jordi - avatar
+ 1
thanks man it works
21st Aug 2017, 8:21 PM
Jordi
Jordi - avatar
+ 1
i dont know c++ but looks similar to java "yes or no\n" << this is cout theres no reason why its saying yes isnt declared as its inside print method the only declaration you have is int a; which im assuming is cin a type of scanner. code looks alright to me
21st Aug 2017, 8:25 PM
D_Stark
D_Stark - avatar
+ 1
thanks i found my problem :)
21st Aug 2017, 11:06 PM
Jordi
Jordi - avatar