cannot read certain lines of a file with getline
Apr 27, 2011 at 3:14pm UTC
Alright, because the names and phone number lines of the file i'm reading
include spaces, i need to use getline() to read them in. I'm not really
sure what I need to include in the () ,or if it needs to be in the loop with the
"ifile >> ... here's what i have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
/***********************
structure definitions
************************/
struct Records
{
char ID[8];
char name[30];
char deptID[4];
char phoneNum[14];
};
/**********prototypes**********/
void getFile(Records label[], const int SIZE);
void output(Records label[], const int SIZE);
/***********mainline***********/
int main()
{
const int SIZE = 26;
Records label[SIZE];
getFile(label, SIZE);
output(label, SIZE);
system("pause" );
return 0;
}
void getFile(Records label[], const int SIZE)
{
ifstream ifile("directory.txt" );
if (ifile)
{
for (int i=0; i<SIZE && !ifile.eof(); i++)
{
ifile >> label[i].ID;
//ifile >> label[i].name;
ifile.getline(label[i].name);
ifile >> label[i].deptID;
//ifile >> label[i].phoneNum;
ifile.getline(label[i].phoneNum);
}
ifile.close();
}
else
{
cout << "File failed to open\n" ;
system("pause" );
exit(2);
}
}
void output(Records label[], const int SIZE)
{
for (int i=0; i<SIZE; i++)
{
cout << "Employee ID: " << setw(22) << label[i].ID << endl;
cout << "Employee name: " << setw(22) << label[i].name << endl;
cout << "Employee department: " << setw(22) << label[i].deptID << endl;
cout << "Employee telephone: " << setw(22) << label[i].phoneNum << endl;
}
}
Apr 27, 2011 at 4:36pm UTC
Try to replace:
ifile >> label[i].name;
With:
cin >> label[i].name;
See if that works?
Topic archived. No new replies allowed.