Trying to use int in getline

Scott picture Scott · Apr 30, 2011 · Viewed 67.7k times · Source
cout << "How many questions are there going to be on this exam?" << endl;
cout << ">>";
getline(cin, totalquestions);

This small piece of code comes from a function in a class that I have created and I need totalquestions to be an int so that it can run through a for loop and keep asking the total amount of questions that I have asked.

question q;
for(int i = 0; i < totalquestions; i++)
{
    q.inputdata();
    questions.push_back(q);
}

Where does this piece of code comes to play? Does anyone have any idea to make this work?

Answer

sehe picture sehe · Apr 30, 2011

Use

cin >> totalquestions;

Check the errors too

if (!(cin >> totalquestions))
{
    // handle error
}