I am a beginner in C++. I am trying to write a simple program that creates records of student_info. I create an array of structs with member variables name and a vector of homework grades. I wish to read from terminal input cin into this array of structs. Please find below my attempt to do this. What I am confused about is how to terminate/ exit the read loop in the program while running the program. I need to continue reading name and a bunch of homework grades that forms a single record. If I delete is.clear() then it only gets one record, when I type in the name of the next student the program exits.
I would greatly appreciate any suggestions.
#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct student_info{
string name;
vector<double> hw_grades;
};
istream& read_single_record (istream& is, student_info& s){
is>>s.name;
double x;
while(is>>x)
{
s.hw_grades.push_back(x);
}
is.clear();
return is;}
int main() {
//read data into an array of student info
vector<student_info> vec_st_info;
student_info x;
while(read_single_record(cin,x))
{
vec_st_info.push_back(x);
}
return 0;
}
A sample input for the program will be
John
88
98
89
67
Sam
78
90
Tom
89
90
76
The name followed by a sequence of homework grades each entered with a 'return' key. The number of homework grades is also not fixed.
std::getlineand then read from a string usingstd::istringstream. If all values are to be on separate lines, and separated by a blank line,std::getlinewill help again in this case. If there's a sentinel value (such as-1) for a grade that ends the record, make sure you are handling that. Otherwise, you may need to read one character at a time, and putback the character if it doesn't look like a number. - paddyistream&instead of changing tobool. - paddy