0

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;
}
Pivoman
  • 4,435
  • 5
  • 18
  • 30
  • This does not appear to be a simple answer. it probably depends on what the method RemoveStudent() is doing to remove the first id. There is also the question as to what the method AddStudent() is doing and how the EvidenceStudent class is implementing the various behaviors. Without being able to review that source the RemoveStudent() method is just a Black Box with inputs and outputs and mysterious magical transformations inside. – Richard Chambers Dec 30 '13 at 15:45
  • Include code here for your .h file – Digital_Reality Dec 30 '13 at 15:48
  • Richard: I think, that definitions of methods `AddStudent()` and `RemoveStudent()` aren't needed. Problem is maybe in `cin >> id;` TimDave (in answers) has propably true. id is string and that is problem – Pivoman Dec 30 '13 at 15:55

1 Answers1

0

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.

TimDave
  • 652
  • 3
  • 6
  • but first string is recieved, second too (somtimes third too). Can help overload >> operator? – Pivoman Dec 30 '13 at 16:01
  • 1
    If for any reason the conversion fails subsequent operations on std::cin will fail. It is dependent on the data. It could fail on the first iteration or the 100th iteration. This is what operator >> is doing. If the conversion to int fails, the stream is in an error state and further attempts at input from that stream will fail. The same can hold true when prompting for the student ID when removing. If a string conversion fails you will get the same result – TimDave Dec 30 '13 at 16:06
  • Ok, now (without changes) I was removing all 10 Students without infinite cycle. But it's my semestral work. Are here any other ways to get string from input without these cycles please? – Pivoman Dec 30 '13 at 16:53
  • Try calling std::cin.clear() before each attempt at using std::cin. This will clear error state. You should also check the state by calling std::cin.rdstate() to verify the input value before using it. – TimDave Dec 30 '13 at 17:01