I am trying to read input from a file one by one instead of giving it from standard input.
What I have, which is currently working!
for(int i = 0; i <CLASS_SIZE; i++)
{
for(int j = 0; j <10 ; j++)
{
scanf("%d", &grade);
studentsInClass[i].setGrade(j,grade);
}
}
current input from console is this:
91 92 85 58 87 75 89 97 79 65
88 72 81 94 90 61 72 75 68 77
75 49 87 79 65 64 62 51 44 70
I want this input to be read directly from a file! So, I tried reading it as a file stream, line by line and then tried to save that line into a vector of strings, so that I can extract the single number per line in an inner loop.
std::ifstream inputfile("input.txt");
std::string line;
std::vector<std::string> v;
for(int i = 0; i <CLASS_SIZE; i++)
{
for(int j = 0; j <10 ; j++)
{
if(inputfile.is_open()){
while( std::getline(inputfile, line) ){
std::cout << line << '\n';
std::istringstream iss(line);
iss>>v;
for(std::vector<std::string>::iterator it = v.begin(); it != v.end(); ++it) {
studentsInClass[i].setGrade(j, std::stoi(it, nullptr, 2));
}
}
inputfile.close();
}
}
}
but getting following error! Because I don't know the right way to do it!
error: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’
iss>>v;
and below error, because I am trying to convert the string to an integer as my program needs to consume as integer and again I am doing something wrong with the syntax.
error: no matching function for call to ‘stoi(std::vector<std::__cxx11::basic_string<char> >::iterator&, std::nullptr_t
, int)’
studentsInClass[i].setGrade(j, std::stoi(it, nullptr, 2));
can anyone help to fix this issue?