C++ CIN cin 随机跳过

发布于 2024-08-27 16:59:33 字数 2346 浏览 14 评论 0原文

我有这个程序,但 cin 会随机跳过。我的意思是有时会跳过,有时不会。有什么想法如何解决这个问题吗?

    int main(){ 


        /** get course name, number of students, and assignment name **/
        string course_name;
        int numb_students;
        string assignment_name;
        Assignment* assignment;

        cout << "Enter the name of the course" << endl;
        cin >> course_name;

        cout << "Enter the number of students" << endl;
        cin >> numb_students;   

        cout << "Enter the name of the assignment" << endl;
        cin >> assignment_name;

        assignment = new Assignment(assignment_name);

        /** iterate asking for student name and score **/
        int i = 0;
        string student_name;
        double student_score = 0.0;
        while( i < numb_students ){

            cout << "Enter the name for student #" << i << endl;
            cin >> student_name;
            cout << "Enter the score for student #" << i << endl;
            cin >> student_score;
            assignment->addScore( Student( student_name, student_score ));
            i++;
        }
}

好吧,我想通了。对于任何想知道这里是更新后的代码的人:

int main(){ 

    /** get course name, number of students, and assignment name **/
    string course_name;
    int numb_students;
    string assignment_name;

    cout << "Enter the name of the course" << endl;
    getline(cin, course_name);

    cout << "Enter the number of students" << endl;
    string temp;
    getline(cin, temp);
    numb_students = atoi(temp.c_str());

    cout << "Enter the name of the assignment" << endl;
    getline(cin, assignment_name);

    Assignment assignment(assignment_name);

    /** iterate asking for student name and score **/
    int i = 0;
    string student_name;
    double student_score = 0.0;
    while( i < numb_students ){

        cout << "Enter the name for student #" << i+1 << endl;
        getline(cin, student_name);     
        cout << "Enter the score for student #" << i+1 << endl;
        getline(cin, temp);
        student_score = atof(temp.c_str());
        assignment.addScore( Student( student_name, student_score ));
        i++;
    }

I have this program, but cin in randomly skips.. I mean sometimes it does, and sometimes it doesn't. Any ideas how to fix this?

    int main(){ 


        /** get course name, number of students, and assignment name **/
        string course_name;
        int numb_students;
        string assignment_name;
        Assignment* assignment;

        cout << "Enter the name of the course" << endl;
        cin >> course_name;

        cout << "Enter the number of students" << endl;
        cin >> numb_students;   

        cout << "Enter the name of the assignment" << endl;
        cin >> assignment_name;

        assignment = new Assignment(assignment_name);

        /** iterate asking for student name and score **/
        int i = 0;
        string student_name;
        double student_score = 0.0;
        while( i < numb_students ){

            cout << "Enter the name for student #" << i << endl;
            cin >> student_name;
            cout << "Enter the score for student #" << i << endl;
            cin >> student_score;
            assignment->addScore( Student( student_name, student_score ));
            i++;
        }
}

OK I figured it out. For anyone that would like to know here's the updated code:

int main(){ 

    /** get course name, number of students, and assignment name **/
    string course_name;
    int numb_students;
    string assignment_name;

    cout << "Enter the name of the course" << endl;
    getline(cin, course_name);

    cout << "Enter the number of students" << endl;
    string temp;
    getline(cin, temp);
    numb_students = atoi(temp.c_str());

    cout << "Enter the name of the assignment" << endl;
    getline(cin, assignment_name);

    Assignment assignment(assignment_name);

    /** iterate asking for student name and score **/
    int i = 0;
    string student_name;
    double student_score = 0.0;
    while( i < numb_students ){

        cout << "Enter the name for student #" << i+1 << endl;
        getline(cin, student_name);     
        cout << "Enter the score for student #" << i+1 << endl;
        getline(cin, temp);
        student_score = atof(temp.c_str());
        assignment.addScore( Student( student_name, student_score ));
        i++;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-09-03 16:59:33

我猜你的一些输入中有空格,其中 >>运算符视为特定输入项的结尾。 iostreams>>运算符实际上并不是为交互式输入而设计的,特别是对于字符串 - 您应该考虑使用 getline() 代替。

另外,您不必要地使用动态分配:

assignment = new Assignment(assignment_name);

最好写为:

Assignment assignment(assignment_name);

您应该尽可能避免在代码中使用“new”,而是让编译器为您处理对象生命周期。

I would guess that some of your inputs have spaces in them, which the >> operator treats as the end of a particular input item. The iostreams >> operator is really not designed for interactive input, particularly for strings - you should consider using getline() instead.

Also, you are needlessly using dynamic allocation:

assignment = new Assignment(assignment_name);

would much better be written as:

Assignment assignment(assignment_name);

you should avoid the use of 'new' in your code wherever possible, and instead let the compiler take care of object lifetimes for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文