我正在尝试学习如何使用 cin
和 getline
编写我可以在学校使用的论文评分程序。对于初学者来说,这是一个棘手的项目,但它让我知道我需要学习什么,这是我需要做的第一件事。
int main()
{
string grader;
int x;
cout << "Who will I be assisting today? ";
getline (cin, grader);
cout << "Hello " << grader << ".\n";
cout << "How manny questions are on the test you will be grading? ";
getline (cin, x);
cout << "this is a " << x << "question test graded by" << grader << ".\n";
}
假设我对第一个问题回答了 John Doe,然后对第二个问题回答了 20。我希望它打印“这是由 John Doe 评分的 20 道题”
我哪里错了?
我确定这是一个愚蠢的错误,但它困扰着我。我是一个新手,很抱歉我的无知。关于这个程序,我会有更多与用户输入无关的问题。可以在这里发布这些问题,还是开始新的话题?
谢谢
最佳答案
因为你没有说明你的错误是什么,唉,它也可能是缺少包含/命名空间。 完整的可运行/可编译程序将是:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string grader;
int x;
cout << "Who will I be assisting today? ";
getline (cin, grader);
cout << "Hello " << grader << ".\n";
cout << "How manny questions are on the test you will be grading? ";
cin >> x;
cout << "this is a " << x << "question test graded by" << grader << ".\n";
}
无论如何,这将在您输入问题数量后立即关闭(或者您从 shell/cmd 调用您的 exe)- 所以如果您看不到结果,请不要怀疑。
关于c++ - 将 cin 的输出分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15131772/