I'm having trouble reading from a file with the following format:
Code:
John 51021400
Mary 51021411
Steve 51021406
Aaron 51021403
Kim 51021408
For some reason the getline functions do not work. Here's the full code:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
string name;
string *id;
string *num;
int i, n = 0;
ifstream fin("phone.txt");
//get number of lines
if (fin.is_open()) {
while (fin.good()) {
getline(fin,name);
n++; //line counter up
}
}
else { //file access verification
cout << "File access error.";
return 0;
}
//create dynamic array
id = new string[n];
num = new string[n];
fin.seekg(ios::beg); //reset file position
//assign names and numbers in file
for (i=0; i<n; i++) {
getline(fin, id[i], '\t');
cout << id[i];
while (fin.get() == '\t');
fin.unget();
getline(fin, num[i]);
cout << num[i];
}
fin.close();
return 0;
}
EDIT: Got it! I just had to clear the buffer halfway.