views:

33

answers:

1

Here's my code. Purpose is to input a Vector of Student class, contain name and homework grades.

istream& input(istream& is, student& s){
    is.clear();
    cout << "Enter student name: ";
    getline(is,s.name);
    grade(is,s.homework);
    return is;
}

istream& grade(istream& is, vector<double>& homework){
    if(is){
        homework.clear();
        double x;
        cout << "Enter grade of student - Ctrl-Z to stop: ";
        while(is>>x)
            homework.push_back(x);
        is.clear();
    }
    return is;
}

The problem is that the first student's name is ok, but when the program read the next student's name (Input from keyboard), it always starts with the substitute (ASCII 26) character. I guess the problem comes from input stream, when I used CTRL - Z to signal the end of the homework grades input. Can you guys suggest a solution ?

+1  A: 

Using Ctrl-Z does insert the SUB character into the stream. Extracting to a double stops just before that character. So you could eliminate it by using the istream::ignore() method. Use a count of 1 and set the delim parameter to 0x1A (the value of SUB).

is.ignore(1, 0x1A);

The other possibility is don't instruct the user to type Ctrl-Z, just press ENTER.

Amardeep