inputing strings to vector

The program should read an unknown number of names and phone numbers from a file “data.txt” and store them in a vector of strings. The program should then ask the user for a transaction file name to load and process.

Im having a hard time putting the inputFile into the vector string. Please help. This is what i have so far:

Here is an example of "Data.txt":
Joe Garcia 858-343-2009
Amelia Perez 617-255-0987
Bob Haynes 858-765-1122
Tim Ducrest 760-877-1654
Kevin Garcia 760-543-5622
Suzy Walter 858-190-2307
Fang Yi 619-677-1212
Robert James 619-909-3476
Mary Palmer 760-435-2086

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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

void Display(vector<string> v, string, string, string);

int main()
{
  string fileName, fname, lname,phone;
  vector <string> v;
  ifstream inputFile;

  cout << "Enter the name of the file: ";
  cin >> fileName;

  inputFile.open(fileName.c_str());

  if (!inputFile)
    {
      cout << "Error opening the file!"<< endl;
    }
  else
    {
      while (inputFile>> fname>> lname >>phone)
        {
          v.push_back(fileName.c_str());
        }
      inputFile.close();
    }
  Display(v,fname,lname,phone);
  return 0;
}
void Display(vector<string> v, string fname, string lname, string phone)
{
  for(int i=0; i<v.size(); i++)
    {
      cout << v[i];
    }
}

Last edited on
Wrap a struct, Person, around the variables fname, lname and phone and overload the Person ctor so that it is easier to construct Person objects as the file is being read into the program. Store these Person objects in std::vector<Person> and overload the insertion operator << to print the vector directly:
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

//using namespace std;
//http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

struct Person
{
    std::string m_fname;
    std::string m_lname;
    std::string m_phone;

    Person(const std::string& fname, const std::string& lname, const std::string& phone)
    : m_fname(fname), m_lname(lname), m_phone(phone){}
};

std::ostream& operator << (std::ostream& os, const Person& p)
{
    os << p.m_fname << " " << p.m_lname << " " << p.m_phone << '\n';
    return os;
}

int main()
{
    std::string fname{}, lname{}, phone{};
    std::vector <Person> persons;
    std::ifstream inputFile("D:\\input2.txt");

    if(inputFile)
    {
        while(inputFile >> fname >> lname >> phone)
        {
            Person temp(fname, lname, phone);
            if(inputFile)
            {
                persons.push_back(temp);
            }
        }
    }
    for(const auto& elem : persons)
    {
        std::cout << elem;
    }
}


Your variable fileName is already a string

You don't need to put it in quotes when using inputFile.open() on line 19
Topic archived. No new replies allowed.