cycy1234
cycy1234

Reputation: 15

cin gets skipped without valid input (string)

Im writing a program requiring input. But somehow the input argument in the console seems to be blocked. Because cin just get's skipped. I tried to force input with to see if any input gets taken with:

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

but the variable never get's printed. I also checked without the while loop and the value from cin doesn't contain anything.It seems like cin is getting no input but continues nevertheless. I tried cin.ignore() and getline() but nothing worked. Every next cin line gets ignored afterwards.

Here is my full code:


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

Upvotes: 1

Views: 374

Answers (1)

user15401571
user15401571

Reputation:

When the user puts an EOF, std::cin will be put in a fail state. This means no input can nor will be read from std::cin, among other things. What you need to do is reset the state using cin.clear(), like so:

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

Output: enter image description here

Upvotes: 2

Related Questions