0

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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 3
    Are you aware that `cin >> a1` would read only `"United"` if you type `"United States"`? See e.g. https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt – Bob__ Mar 30 '22 at 23:26

1 Answers1

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.

yo1nk
  • 57
  • 7