Code:How do I input a string to compare it to a character?
Like I want to input '}' and input '*/' and compare them.
Printable View
Code:How do I input a string to compare it to a character?
Like I want to input '}' and input '*/' and compare them.
Code:char input[SIZE];
fgets(input, SIZE, filePointer);
// now input contains a string given by the user
You input a string consisting of a single word (no whitespace) like this
std::string str;
std::cin >> str;
If you want a whole line (everything typed before return, including whitespace)
std::getline(std::cin, str);
If you want to see if the first character matches 'a' then you can test (str[0] == 'a')
If you want to see if the string contains 'a' anywhere (mathing "contains" but not "string") then if (str.find('a') == std::string::npos) is true, 'a' was not found (find is equal to the position of the first 'a' found, npos is string's opinion of infinity)