Frage

I have console application and I first generate 10 students and then I add them to the Evidence (own double linked list). Then I'm removing student from Evidence, but after second (or highter) removal of student I have infinite cycle, because cin >> id; doesn't wait on my reaction. Where is problem please? Thanks for all advices and sorry from my bad english. :-)

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include "EvidenceStudent.h"
using namespace std;

int main(int argc, char ** argv[])
{
    EvidenceStudent * evidence = new EvidenceStudent();
    int number;
    while(1){
            << "[1] Generate 10 students" << std::endl
            << "[2] Remove student" << std::endl
            << "[3] Write all students" << std::endl
            << "Number: ";
        std::cin >> number;
        cin.ignore();
        switch(number){
        case 1 : {
            Student * s0 = new Student("st00000", "Karel", "Gott");
            Student * s1 = new Student("st11111", "Marek", "Prima");
            Student * s2 = new Student("st22222", "Alois", "Jirasek");
            Student * s3 = new Student("st33333", "Josef", "Rehak");
            Student * s4 = new Student("st44444", "Zdenek", "Zlatnik");
            Student * s5 = new Student("st55555", "Monika", "Svobodova");
            Student * s6 = new Student("st66666", "Michal", "Cvik");
            Student * s7 = new Student("st77777", "Jiri", "Sadilek");
            Student * s8 = new Student("st88888", "Tomas", "Svoboda");
            Student * s9 = new Student("st99999", "Vojtech", "Hudec");

            evidence->AddStudent(*s0, Student::FIRST);
            evidence->AddStudent(*s1, Student::LAST);
            evidence->AddStudent(*s2, Student::NEXT);
            evidence->AddStudent(*s3, Student::PREVIOUS);
            evidence->AddStudent(*s4);
            evidence->AddStudent(*s5, Student::FIRST);
            evidence->AddStudent(*s6, Student::LAST);
            evidence->AddStudent(*s7, Student::NEXT);
            evidence->AddStudent(*s8, Student::PREVIOUS);
            evidence->AddStudent(*s9);
            break;
                 }
        case 2 : {
            string id;
            cout << "Enter student ID: ";
            cin >> id;       //<-- after entering second id - infinite cycle
            cin.ignore();        
            evidence->RemoveStudent(id); 
            break;
                 }
        case 3 : evidence->WriteAllStudents(); break;
        default : cout << "Bad number." << endl; break;
        }
    }
    return 0;
}
War es hilfreich?

Lösung

See the operation of operator >> of istream: http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

You are likely having the fail bit set on cin due to failed conversion to int, and after that the subsequent calls to

std::cin >> number;

will fail causing your program to never receive input from std::cin and fall into an infinite loop.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top