尝试在getline中使用int [英] Trying to use int in getline
本文介绍了尝试在getline中使用int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cout << "How many questions are there going to be on this exam?" << endl;
cout << ">>";
getline(cin, totalquestions);
这小段代码来自我创建的类中的一个函数,我需要totalquestions
作为一个int,以便它可以通过for循环运行并不断询问我所问的全部问题
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?
推荐答案
使用
cin >> totalquestions;
也检查错误
if (!(cin >> totalquestions))
{
// handle error
}
这篇关于尝试在getline中使用int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文