This question has already been answered:

void read_character(ifstream &a) { char t; a >> t; if (t == '+') { string temp; getline(a, temp); character_name = temp; a >> number; spetifications = new string[number]; spetifications_value = new int[number]; for (int i = 0; i < number; i++) { a >> spetifications[i]; } for (int i = 0; i < number; i++) { a >> spetifications_value[i]; } a >> t; if (t != '+') { throw; } } else { throw; } } int main() { ifstream k("output.txt"); b.read_character(k); } 

The essence of the problem is as follows: I want to read object b from a text file, for convenience, the block of this object is highlighted with pluses. There are no exceptions, even the last plus is fully read, but everything after getline () is read incorrectly with the left values, and other objects subsequently throw out exceptions when reading. The text file itself:

 + void 2 first_spetification second_spetification 45 100 + 

Reported as a duplicate by Harry c ++ Apr 24 at 3:30 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Use only getline everywhere to read (numbers come from the lines already read) - avp 8:31 pm

1 answer 1

If you have all the string values ​​from one word - replace

 getline(a, temp); 

on

 a >> temp; 

and live in peace. If not, then

 a >> spetifications[i]; 

replace too with

 getline(a,spetifications[i]); 

but before each getline , including the first, flush the stream buffer

 a.ignore(std::numeric_limits<std::streamsize>::max(), '\n');