reading a file with name + phone number

Write your question here.
So, i got the name to come out but im having problem with phone number to display! for example: in the file i have......i tried add inputFile >> num but it have - so it give me an error.

Mike Smith 858-343-2009
John Jackson 617-255-0987


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
 

int main()
{
  string FirstName, LastName;
  double num;


  cout << FirstName;

  //  ReadFile(); //read the file.

    vector<string> V;
    ifstream inputFile;

    inputFile.open("dataAssign.txt"); //Calling the file out if it exists.

    while (!inputFile.eof())
      {

        inputFile >> FirstName >> LastName;

  cout << FirstName << " " << LastName << " " << endl;


      }
    inputFile.close();

 return 0;
}

858-343-2009


If this is the form of number, you should use strings, not doubles.
Don't loop on eof():
http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream inputFile("C:\\input.txt");
    std::string FirstName{}, LastName{}, PhoneNo{};

    while(inputFile >> FirstName >> LastName >> PhoneNo)
    {
        std::cout << FirstName << " " << LastName << " " << PhoneNo << '\n';
        //consider additional formatting for alignment, spacing etc
    }
}


Depending on further uses in the program you can also consider setting up a struct:
1
2
3
4
5
6
struct Person
{ 
    std::string m_fName;
    std::string m_lName;
    std::string m_pNum;
};

and then overload the extraction operator >> for Person to read into Person objects directly from file, store these objects in std::vector<Person>, do stuff with them, print Person objects directly by further overload of the stream insertion operator << etc
This is a pretty popular assignment did you try searching?- http://www.cplusplus.com/forum/beginner/148719/#msg778977

Pretty much what gunner said, my link will take you to the code I wrote for a similar question with the class and overloads
Topic archived. No new replies allowed.