1

我正在尝试学习如何使用 cingetline编写我可以在学校使用的纸质评分程序。对于初学者来说这是一个棘手的项目,但它让我知道我需要学习什么,这是我需要做的第一件事。

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 题测试”
我哪里出错了?我确定这是一个愚蠢的错误,但它困扰着我。我是新手,对我的无知感到抱歉。我会有更多关于这个程序的问题,这些问题与用户输入无关。可以在这里发布这些问题,还是开始新的话题?谢谢

4

1 回答 1

1

由于您没有说明您的错误是什么,唉,它也可能是缺少包含/命名空间。完整的可运行/可编译程序将是:

#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) - 所以不要怀疑您是否看不到结果。

于 2013-02-28T09:33:58.657 回答