So I have an assignment for class, dont want to explain the whole thing but im trying to write some info to a file, then input from that file to another output file. The problem im running into is that on the 2nd output file, whenever I type in someones full name, it messes up the whole file, but if I use only the first name, works fine. Im assuming it has to do with getline but Ive been searching for the past 4 hours and cant find anything and I think im just confusing myself more. This assignment is due tomorrow so a quick help would be awesome. Heres the code.
This line cin.getline(name, SIZE); takes a whole line (incl. space)
This line ifile >> name; takes a word only without space
So whenever you enter space you'll see different things.
Means: cin.getline(name, SIZE); // <---- name = "abc def"
leads to
1 2
ifile >> name; // <---- name = "abc"
ifile >> id; // <---- id = "def"
I'd suggest that you use std::getline() instead of cin.getline(). With std::getline() you can pass a std::string without limitation.
ok thanks for the response. im going to try this right now. What I meant by it messes up the whole file was that putting in a name with a space in it (first and last name) it would mess up the output file by putting the first half in name and the second half in the id row.
so why doesnt ifile >> name; produce the whole thing? just wondering.
Coming back to the original post - Only two lines are required to make it work:
1 2 3 4 5 6 7 8 9 10 11 12
while (count < 10)
{
//ifile >> name;//get rid of this line
ifile.getline(name,21);//and use this one insted
//..........All other lines the same as before
ifile.getline(name,21); //** Add this line as the last line in the loop before count++
count++;
}
//we need the extra line ifile.getline(name,21); //** Add this line as the last line in the loop before count++ at the bootom of the loop, because each set of employee
details is followed terminated by a carriage return and we need to get rid of it before getting
the next emplyee's name