I am trying to read data from a file in a loop. The file looks like this:

X Joe Scholtz 437-4798
A Tim Wade 768-7658
X Sara Jobs 326-7857
! Jaynce Bee 354-8678
(The X in front is for preferred or not preferred.)

I am getting the first and last name of the last person listed and their phone number, I can't figure out how to make it read the first line and then the second line, etc. Any help would be greatly appreciated! Thanks!

Here is what I have:

int main(int argc, char *argv[])
{   // Declare variables.
    string name1, name2;
    char preferred, dash;
    double phone, phone2;
    ifstream inputFile;     
    int number=0;


//Open text file.
inputFile.open("potentials.txt");
cout << "Reading data from the file." << endl << endl;


//Read all required information from the file.

    for (number =0; inputFile; number++)
      {       
       inputFile >> preferred;
       inputFile >> name1;
       inputFile >> name2;
       inputFile >> phone;
       inputFile >> dash;
       inputFile >> phone2;
       } 


//Display name.
cout << "Name: " << name1 << " " << name2 << endl;
cout << "Phone Number: " << phone << dash << phone2 << endl << endl;


There is a lot more to the program, I just can't get the right names to read in from the file.

Reading looks to be ok, what you need to do is move lines 29 and 30 inside the loop so that it prints each of the lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.