I'm writing an small program in C++ and have an input file and need to read line by line, there're 2 columns in file, string name and integer number. for example:
abad 34
alex 44
chris 12
I wrote such code:
ifstream input("file.txt");
int num;
string str;
while( getline( input, line ) ){
istringstream sline( line );
if( !(sline>>str>>num) ){
//throw error
}
...
}
I need to throw errors in cases:
if there's no number - only name is written e.g. abad
(actually I'm getting error with my code),
if there's a name and no number, e.g.: abad 34a
(letter a in 34a
is ignored and transferred to just 34 in my code while error should be triggered),
or if there're more than 2 columns e.g. abad 34 45
(2nd number is ignored).
How to read correctly input data ( and without iterators )?