cin 在没有有效输入(字符串)的情况下被跳过

cin gets skipped without valid input (string)

我正在编写一个需要输入的程序。但不知何故,控制台中的输入参数似乎被阻止了。因为 cin 被跳过了。 我试图强制输入以查看是否有任何输入被采用:

while (!(cin >> exit_theloop)){
            if (exit_theloop == "EXIT" || exit_theloop == "exit"){
                break;
            }
            cout << exit_theloop;
    };

但变量从未打印出来。我还在没有 while 循环的情况下进行了检查,并且 cin 的值不包含 anything.It 似乎 cin 没有输入但仍然继续。 我尝试了 cin.ignore() 和 getline() 但没有任何效果。 之后每一个下一个 cin 行都会被忽略。

这是我的完整代码:


#include <algorithm>
#include <iomanip>
#ifndef __GNUC__
#include <ios>
#endif
#include <iostream>
#include <string>
#include <vector>


using std::cin;             using std::sort;
using std::cout;            using std::streamsize;
using std::endl;            using std::string;
using std::setprecision;    using std::vector;

int main()
{
    // Grades and students
    vector<string> students;
    vector<int> student_grades;
    vector<double> homework;

#ifdef _MSC_VER
    typedef std::vector<double>::size_type vec_sz;
#else
    typedef vector<double>::size_type vec_sz;
#endif
    
    vec_sz mid;
    vec_sz size;
    double median;
    double x;
    int counter = 0;
    
    while (true){
        // ask for and read the student's name
        cout << "Please enter the first name of student " << counter + 1 << ":";
        string name;
        cin >> name;
        cout  << endl << name << " Has been accepted!" << endl;
        students.push_back(name);
        // ask for and read the midterm and final grades
        cout << "Please enter the midterm and final exam grades: ";
        double midterm, final;
        cin >> midterm >> final;

        // ask for and read the homework grades
        cout << "Enter all your homework grades, "
                "followed by end-of-file: ";
        
        
        
        // invariant: `homework' contains all the homework grades read so far
        while (cin >> x)
            homework.push_back(x);

        // check that the student entered some homework grades
    
        size = homework.size();
        if (size == 0) {
            cout << endl << "You must enter your grades.  "
                            "Please try again." << endl;
            return 1;
        }

        // sort the grades
        sort(homework.begin(), homework.end());

        // compute the median homework grade
        mid = size/2;
        
        median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2
                               : homework[mid];

        // compute and write the final grade
        streamsize prec = cout.precision();
        
        student_grades.push_back(0.2 * midterm + 0.4 * final + 0.4 * median);
        
       
        
        // increment counter for next student
        counter++;
        
        // ask the user if hes is done
        string exit_theloop;
        
        cout << "\ndo you wish to continue? Type in EXIT to exit the Programm!: " << endl;
        
        // clearing homework 
        homework.clear();
        
        cin >> exit_theloop; //<------------------------ not working
            
    
        cout << exit_theloop << endl; // <--------------------returns nothing 
        
        if (exit_theloop == "EXIT" || exit_theloop == "exit"){
            break;
        }
        
    }
  
    

    return 0;
}

当用户输入 EOF 时,std::cin 将进入 fail 状态。这意味着不能也不会从 std::cin 读取任何输入,等等。您需要做的是使用 cin.clear() 重置状态,如下所示:

#include <algorithm>
#include <iomanip>
#ifndef __GNUC__
#include <ios>
#endif
#include <iostream>
#include <string>
#include <vector>


using std::cin;             using std::sort;
using std::cout;            using std::streamsize;
using std::endl;            using std::string;
using std::setprecision;    using std::vector;

int main()
{
    // Grades and students
    vector<string> students;
    vector<int> student_grades;
    vector<double> homework;

#ifdef _MSC_VER
    typedef std::vector<double>::size_type vec_sz;
#else
    typedef vector<double>::size_type vec_sz;
#endif

    vec_sz mid;
    vec_sz size;
    double median;
    double x;
    int counter = 0;

    while (true) {
        // ask for and read the student's name
        cout << "Please enter the first name of student " << counter + 1 << ":";
        string name;
        cin >> name;
        cout << endl << name << " Has been accepted!" << endl;
        students.push_back(name);
        // ask for and read the midterm and final grades
        cout << "Please enter the midterm and final exam grades: ";
        double midterm, final;
        cin >> midterm >> final;

        // ask for and read the homework grades
        cout << "Enter all your homework grades, "
            "followed by end-of-file: ";



        // invariant: `homework' contains all the homework grades read so far
        while (cin >> x)
            homework.push_back(x);


        // check that the student entered some homework grades

        size = homework.size();
        if (size == 0) {
            cout << endl << "You must enter your grades.  "
                "Please try again." << endl;
            return 1;
        }

        cin.clear(); //reset the state, so we can read more input

        // sort the grades
        sort(homework.begin(), homework.end());

        // compute the median homework grade
        mid = size / 2;

        median = size % 2 == 0 ? (homework[mid] + homework[mid - 1]) / 2
            : homework[mid];

        // compute and write the final grade
        streamsize prec = cout.precision();

        student_grades.push_back(0.2 * midterm + 0.4 * final + 0.4 * median);



        // increment counter for next student
        counter++;

        // ask the user if hes is done
        string exit_theloop;

        cout << "\ndo you wish to continue? Type in EXIT to exit the Programm!: " << endl;

        // clearing homework 
        homework.clear();

        cin >> exit_theloop; //<------------------------ not working


        cout << exit_theloop << endl; // <--------------------returns nothing 

        if (exit_theloop == "EXIT" || exit_theloop == "exit") {
            break;
        }

    }



    return 0;
}

输出: