+ 2

How to declare a string variable in c++

I have a program where I want to declare a string g variable by setting it equals "success" or "fail" based on user input. I don't want to output the string directly but want to store it in a variable so as to use for further test in the code.

29th Dec 2017, 9:40 AM
Ahmed Olanrewaju
Ahmed Olanrewaju - avatar
2 odpowiedzi
+ 5
Use the string class : string g = "Fail"; int a; cin>>a; if(a) g="Success"; // Do other stuff. You need the <string> header for this. For more information on the string type, visit: www.cplusplus.com/reference/string Edit - Glad to be able to help. ^_^
29th Dec 2017, 9:54 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
https://code.sololearn.com/c0s4Lpw397Ow/?ref=app Thanks it works for me. I tested it using above sample before implementing it to my project.
30th Dec 2017, 3:17 PM
Ahmed Olanrewaju
Ahmed Olanrewaju - avatar