'Why doesn't work correctly when I put a space between United and States words? [duplicate]
If statement isn't working when I change "UnitedStates" as "United States". What is the reason of that? Why space character cannot be read correctly? What should I do? If you could help me to understand, I'd be grateful.
int main()
{
string u1 = "UnitedStates";
string a1;
cout << "Country: ";
cin >> a1;
if (a1 == u1)
cout << "Congrats!";
return 0;
}
int main()
{
string u1 = "United States";
string a1;
cout << "Country: ";
cin >> a1;
if (a1 == u1)
cout << "Congrats!";
return 0;
}
I tried so many times.
Solution 1:[1]
The input operator works by reading in one string a time and a space character indicates the string ends. So a1 will be "United" in this case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | yo1nk |
