Hey I'm working on this program that tests you on a few questions. I enter the answer to one and it comes back and displays correct. For some reason though when the next question comes on it already comes back with a response saying the answer was incorrect. Here's the code.

string metals = "metals";
    string userresponse221;
    cout << "\n22.  Ionic compounds are usually formed between which two groups of elements?\n1:\t";
    cin >> userresponse221;
    
    if (userresponse221 == metals)
    {
                cout << "Correct" << endl;
    }
    else
    {
        cout << "Incorrect" << endl;
    }
    
    cout << "2: \t";
    string nonmetals = "nonmetals";
    string userresponse222;
    cin >> userresponse222;
    if (userresponse222 == nonmetals)
    {
                cout << "Correct" << endl;
    }
    else
    {
        cout << "Incorrect" << endl;
    }
    
    
    cout << "\nName the following compounds:\n";
    
    
    
    // Question 23
    string answer23 = "lithium nitride";
    string userresponse23;
    cout << "\n23.  Li3N:\n\t";
    getline(cin,userresponse23);
    if (userresponse23 == answer23)
    {
                 cout << "Correct" << endl;

    }
    else
    {
                 cout << "Incorrect" << endl;
    }

If anyone sees anything wrong I'd truly appreciate it.

Best guess is that you have a newline remaining in the input stream. getline sees that, takes it, and thus doesn't pause for input. You need to flush the input stream so there's nothing in it when the program reaches the getline statement.

http://www.daniweb.com/forums/thread90228.html

thank you so much. that article cleared everything up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.