State Capital Identification

I'm not sure why this program isn't working. I'm fairly new to this, so some explanation would be very helpful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<string>
    using namespace std;
int main()
{
    string stateOne, stateTwo, stateThree, stateFour, stateFive;
    
    cout << "What is the capital of Arkansas?" << endl;
    cin >> stateOne;
   if(stateOne == "Little Rock")
   {
        cout << "That is correct!" << endl;
   }
    else
        {
        cout << "That's wrong... The capital of Arkansas is Little Rock" << endl;
        }
    cout << "What is the capital of Illinois?" << endl;
    cin >> stateTwo;
    if(stateTwo == "Springfield")
    {
        cout << "That is correct!" << endl;
    }
    else
        {
        cout << "That's wrong... The capital of Illinois is Springfield" << endl;
        }
    cout << "What is the capital of Michigan?" << endl;
    cin >> stateThree;
    if(stateThree == "Lansing")
    {
        cout << "That is correct!" << endl;
    }
    else
        {
        cout << "That's wrong... The capital of Michigan is Lansing" << endl;
        }
    cout << "What is the capital of Oregon?" << endl;
    cin >> stateFour;
    if(stateFour == "Salem")
    {
        cout << "That is correct!" << endl;
    }
    else
        {
        cout << "That's wrong... the capital of Oregon is Salem" << endl;
        }
    cout << "What is the capital of California?" << endl;
    cin >> stateFive;
    if(stateFive == "Sacramento")
    {
        cout << "That is correct!" << endl;
    }
    else
        {
        cout << "That's wrong... The capital of California is Sacramento" << endl;
        }
}
The >> operator stops reading as soon as it sees a whitespace character (e.g. space, tab, newline).

If you want to read the whole line you can use std::getline. I recommend using it in conjunction with std::ws because it avoids many problems that you can get when using both >> and std::getline in the same program.

1
2
cin >> stateOne;
getline(cin >> ws, stateOne);

http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/istream/ws/
Topic archived. No new replies allowed.