C ++ CIN cin随机跳过 [英] C++ CIN cin skips randomly

查看:161
本文介绍了C ++ CIN cin随机跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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我想出来了。对于任何想知道这里更新的代码的人:

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++;
    }


推荐答案

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

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.

assignment = new Assignment(assignment_name);

最好写成:

Assignment assignment(assignment_name);

你应该尽可能避免在代码中使用'new',照顾对象的生命期。

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

这篇关于C ++ CIN cin随机跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆