reading file and storing info

Jun 11, 2020 at 6:25pm
So I am reading a file and trying to store the information into vectors and having trouble with it, I know I'm messing up somewhere but don't know how to fix it separating the spaces and storing the right information into the right vectors, please any help will be appreciated. The text file is arranged like this......

file.txt

Name Order-number Phone-number Date
----------------------------------------------------------------------------
Billy Chamber 1763829 555-7863 1/23
Bob Hurt 2143567 555-7234 3/23
Nurt Kille 7283495 555-8273 2/12
...............
...............


1
2
3
4
5
6
7
8
9

struct company{

        vector<string> Name;
        vector<int> Order_num;
        vector<string> phone;

    }; 



the part of the code that reads the file is here along with the struct of the


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

....
....
.....
 int choice;  // choice for the user to enter
    string line, garbage1, garbage2;
    const string space = " ";
    
    ifstream file;  // reading the file

    company info;  // called for use of struct

    file.open("file.txt");  // file to be read

    cin >> choice;  // choice for user to enter

    cout << "0: exit prog." << endl;
    cout << "1: read from file " << endl;
    cout << "2: add to file " << endl;
    cout << "3: delete from file " << endl;

    if (choice == 0) {
        cout << "exiting the prog" << endl;
        system("exit");
    } 


cout << "reading from the file" << endl;

        if (file.is_open()) {

            garbage1 = line; // tosses the first line no use
            garbage2 = line; // tossess the ------ line

            string contain;


// problem occurring here.....

            while (getline(file, line)) {
                if (line != space) {
                    line.substr(0, line.find(" "));
                    for (int i = 0; i < file.eof(); i++) {
                        info.Name[i] = line;
                        info.Order_num = line;
                        info.phone = line;
                    }
               }
            }

            file.close(); // closing the file
        }
        else {
            cout << "file cant be open" << endl;
        }


......
......
......
Last edited on Jun 11, 2020 at 7:14pm
Jun 11, 2020 at 7:45pm
If you have something like this:
1
2
3
4
5
struct company {
    vector<string> Name;
    vector<int> Order_num;
    vector<string> phone;
};

You probably should have something like this instead:
1
2
3
4
5
6
struct Company {
    string name;
    int order_num;
    string phone;
};
using companies = std::vector<Company>;


You'd read it with something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    Companies companies;

    std::ifstream file("file.txt");
    std::string line;
    while (std::getline(file, line)) {
        // line contains something like: Billy Chamber 1763829 555-7863
        std::istringstream is(line);
        Company company;
        string first_name, last_name;
        is >> first_name >> last_name >> company.order_num >> company.phone;
        company.name = first_name + " " + last_name;

        companies.push_back(company);
    }
Last edited on Jun 11, 2020 at 7:46pm
Jun 11, 2020 at 8:14pm
thanks for the fast response but i think i got it a little bit but a new error came up this is the changes with the ones you made

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
while (getline(file, line)) {
                if (line != " ") {

                    istringstream is(line);


                    for (int i = 0; i < file.eof(); i++) {

                        std::istringstream is(line);

                        string files_name, files_phone, files_date;
                        int files_order;

                        is >> files_name >> files_phone >> files_order >> files_date;

                        info.Name = files_name; 
                        info.phone = files_phone;
                        info.Order_num = files_order;
                        info.date = files_date;
                        
                        companies.push_back(company); // here is the error  i have is expected //identifier  why is that

                    }
               }
            }





the error i have is expected identifier doesnt it not get stored in the vector i thought puch back adds at the end of the vector
Last edited on Jun 11, 2020 at 8:15pm
Topic archived. No new replies allowed.