Everything seems to be working but read from file part

Oct 16, 2014 at 7:27am
If someone could please help me grasp reading from a file i would appreciate it.

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;

struct automobile
{
short int year;
string category, make, model, color, engine;
};

const short int amount_vehicles = 3;

struct garage
{
automobile vehicles[amount_vehicles];
double len, width;
};

void initialize_garage(garage &g);
garage populategarage();
void displaygarage(const garage &g);
void swap_car(garage &mg, garage &ng);

void main()
{
garage mine, neigbors;

cout << "Populate My Garage:";
initialize_garage(mine);
mine = populategarage();
cout << setw(50) << right << "My Populated Garage";
displaygarage(mine);
cout << "\n\n\n";

cout << "Populate Neigbor's Garage:";
initialize_garage(neigbors);
neigbors = populategarage();
cout << setw(50) << right << "Neighbor's Populated Garage";
displaygarage(neigbors);
cout << "\n\n\n";

swap_car(mine, neigbors);
cout << "\nAfter swapping car with neighbor, my garage is as follows:";
displaygarage(mine);
cout << "\nAfter swapping car with neighbor, neighbor's garage is as follows:";
displaygarage(neigbors);

cout << "\n\n\n";
system("pause");
}

void initialize_garage(garage &g)
{
for (int i = 0; i < amount_vehicles; i++)
{
g.vehicles[i].category = "\0";
g.vehicles[i].make = "\0";
g.vehicles[i].model = "\0";
g.vehicles[i].color = "\0";
g.vehicles[i].engine = "\0";
g.vehicles[i].year = 0;
}
g.len = 0.0; g.width = 0.0;
}

garage populategarage()
{
garage tg;
string instruction_prompt;

initialize_garage(tg);

for (int i = 0; i < amount_vehicles; i++)
{
cout << i+1;
getline(cin, tg.vehicles[i].category);

if (tg.vehicles[i].category == "\0")
i = amount_vehicles;
else
{

ifstream ReadFromFile("garage-input-file.txt");
if (ReadFromFile. is_open())
{
while (!ReadFromFile.eof())
{
getline(ReadFromFile, tg.vehicles[i].make);
getline(ReadFromFile, tg.vehicles[i].model);
getline(ReadFromFile, tg.vehicles[i].color);
cin >> (ReadFromFile, tg.vehicles[i].year);
cin.ignore();
getline(ReadFromFile, tg.vehicles[i].engine);
cin.ignore();
}
cin >> (ReadFromFile, tg.len);
cin >> (ReadFromFile, tg.width);
cin.ignore();
ReadFromFile.close();
}
else
cout << "Failed to open the file\n";
}
return tg;
}
}

void displaygarage(const garage &g)
{
for(int i = 0; i < amount_vehicles; i++)
{
cout << "\nVehicle " << i+1 << ":\n";
cout << setw(12) << right << "category: ";
cout << setw(15) << left << g.vehicles[i].category << "\n";
cout << setw(12) << right << "make: ";
cout << setw(20) << left << g.vehicles[i].make << "\n";
cout << setw(12) << right << "model: ";
cout << setw(20) << left << g.vehicles[i].model << "\n";
cout << setw(12) << right << "color: ";
cout << setw(20) << left << g.vehicles[i].color << "\n";
cout << setw(12) << right << "year: ";
cout << setw(20) << left << g.vehicles[i].year << "\n";
}
cout << "\nGarage size: " << g.len * g. width << " square ft. ";
}
void swap_car(garage &mg, garage &ng)
{
int choice = 0;
garage tg;

while (choice < 1 || choice > amount_vehicles)
{
cout << "\nEnter the car you want to swap (1/2/3) or quit with 0: ";
cin >> choice;

if (choice == 0 )
break;
}

if (choice != 0 )
{
int i = --choice;
tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;

tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;

tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;
}
}
Oct 16, 2014 at 7:44am
Please use code tags.
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;

struct automobile
{
short int year;
string category, make, model, color, engine;
};

const short int amount_vehicles = 3;

struct garage
{
automobile vehicles[amount_vehicles];
double len, width;
};

void initialize_garage(garage &g);
garage populategarage();
void displaygarage(const garage &g);
void swap_car(garage &mg, garage &ng);

void main()
{
garage mine, neigbors;

cout << "Populate My Garage:";
initialize_garage(mine);
mine = populategarage();
cout << setw(50) << right << "My Populated Garage";
displaygarage(mine);
cout << "\n\n\n";

cout << "Populate Neigbor's Garage:";
initialize_garage(neigbors);
neigbors = populategarage();
cout << setw(50) << right << "Neighbor's Populated Garage";
displaygarage(neigbors);
cout << "\n\n\n";

swap_car(mine, neigbors);
cout << "\nAfter swapping car with neighbor, my garage is as follows:";
displaygarage(mine);
cout << "\nAfter swapping car with neighbor, neighbor's garage is as follows:";
displaygarage(neigbors);

cout << "\n\n\n";
system("pause");
}

void initialize_garage(garage &g)
{
for (int i = 0; i < amount_vehicles; i++)
{
g.vehicles[i].category = "\0";
g.vehicles[i].make = "\0";
g.vehicles[i].model = "\0";
g.vehicles[i].color = "\0";
g.vehicles[i].engine = "\0";
g.vehicles[i].year = 0;
}
g.len = 0.0; g.width = 0.0;
}

garage populategarage()
{
garage tg; 
string instruction_prompt;

initialize_garage(tg);

for (int i = 0; i < amount_vehicles; i++)
{
cout << i+1;
getline(cin, tg.vehicles[i].category);

if (tg.vehicles[i].category == "\0")
i = amount_vehicles;
else
{

ifstream ReadFromFile("garage-input-file.txt");
if (ReadFromFile. is_open())
{
while (!ReadFromFile.eof())
{
getline(ReadFromFile, tg.vehicles[i].make);
getline(ReadFromFile, tg.vehicles[i].model);
getline(ReadFromFile, tg.vehicles[i].color);
cin >> (ReadFromFile, tg.vehicles[i].year);
cin.ignore();
getline(ReadFromFile, tg.vehicles[i].engine);
cin.ignore();
}
cin >> (ReadFromFile, tg.len);
cin >> (ReadFromFile, tg.width);	
cin.ignore();
ReadFromFile.close();
}
else
cout << "Failed to open the file\n";
}
return tg;
}
}

void displaygarage(const garage &g)
{
for(int i = 0; i < amount_vehicles; i++)
{
cout << "\nVehicle " << i+1 << ":\n";
cout << setw(12) << right << "category: ";
cout << setw(15) << left << g.vehicles[i].category << "\n";
cout << setw(12) << right << "make: ";
cout << setw(20) << left << g.vehicles[i].make << "\n";
cout << setw(12) << right << "model: ";
cout << setw(20) << left << g.vehicles[i].model << "\n";
cout << setw(12) << right << "color: ";
cout << setw(20) << left << g.vehicles[i].color << "\n";
cout << setw(12) << right << "year: ";
cout << setw(20) << left << g.vehicles[i].year << "\n";
}
cout << "\nGarage size: " << g.len * g. width << " square ft. ";
}
void swap_car(garage &mg, garage &ng)
{
int choice = 0;
garage tg;

while (choice < 1 || choice > amount_vehicles)
{
cout << "\nEnter the car you want to swap (1/2/3) or quit with 0: ";
cin >> choice;

if (choice == 0 )
break;
}

if (choice != 0 )
{
int i = --choice;
tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;

tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;

tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;
}
}
Oct 16, 2014 at 7:50am
You cant do cin >> (readFromFile,xx)
Continue using getline(readFromFile,xx)

Lines: 94,99,100

Why the cin.ignore() ? you are not reading any thing from stdin
Oct 16, 2014 at 8:03am
i made the fixes you suggested but, the problem is reading the file... keeps telling me it cannot be read or found
Oct 16, 2014 at 8:22am
'garage-finished.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'garage-finished.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'garage-finished.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
Oct 16, 2014 at 9:22am
The PDB file is a Visual Studio specific file that has the debugging symbols for your project. You can ignore those messages, unless you're hoping to step into the code for those dlls with the debugger.

That has nothing to do with whether your program can open the file or not.

In line 104: you have
1
2
else
cout << "Failed to open the file\n";

Is this what you get?
Oct 16, 2014 at 9:28am
it is not calling the garage populategarage(); function, and i don't know why
Oct 16, 2014 at 9:34am
yeah i know i am just spacing out at this point becuase of how long iv'e been trying to figure this out. finally took code out of functions and did the input file read... worked fine all i can think of is tha populategarage() is not being called. any idea why
Oct 16, 2014 at 9:45am
To check if indeed poulategarage is not called, insert
1
2
 cout<<"We are here"<<endl;
system("pause");
as the 1st line in the populategarage() function.
Oct 16, 2014 at 9:51am
yup, not being called. no idea why though. This code is just an alteration of a working code(i nolonger have) to change the input from user to file based.
Oct 16, 2014 at 10:47am
Tested the code on ma machine, poulategarage() works for me and is called properly.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include <cstdlib>

using namespace std;

struct automobile
{
    short int year;
    string category, make, model, color, engine;
};

const short int amount_vehicles = 3;

struct garage
{
    automobile vehicles[amount_vehicles];
    double len, width;
};

void initialize_garage(garage &g);
garage populategarage();
void displaygarage(const garage &g);
void swap_car(garage &mg, garage &ng);

int main()
{
    garage mine, neigbors;

    cout << "Populate My Garage:";
    initialize_garage(mine);
    mine = populategarage();
    cout << setw(50) << right << "My Populated Garage";
    displaygarage(mine);
    cout << "\n\n\n";

    cout << "Populate Neigbor's Garage:";
    initialize_garage(neigbors);
    neigbors = populategarage();
    cout << setw(50) << right << "Neighbor's Populated Garage";
    displaygarage(neigbors);
    cout << "\n\n\n";

    swap_car(mine, neigbors);
    cout << "\nAfter swapping car with neighbor, my garage is as follows:";
    displaygarage(mine);
    cout << "\nAfter swapping car with neighbor, neighbor's garage is as follows:";
    displaygarage(neigbors);

    cout << "\n\n\n";
    system("pause");
}

void initialize_garage(garage &g)
{
    for (int i = 0; i < amount_vehicles; i++)
    {
        g.vehicles[i].category = "\0";
        g.vehicles[i].make = "\0";
        g.vehicles[i].model = "\0";
        g.vehicles[i].color = "\0";
        g.vehicles[i].engine = "\0";
        g.vehicles[i].year = 0;
    }
    g.len = 0.0; g.width = 0.0;
}

garage populategarage()
{
    cout<<"here"<<endl;
    system("pause");
    garage tg;
    string instruction_prompt;

    initialize_garage(tg);

    for (int i = 0; i < amount_vehicles; i++)
    {
        cout << i+1;
        getline(cin, tg.vehicles[i].category);

        if (tg.vehicles[i].category == "\0")
            i = amount_vehicles;
        else
        {
            ifstream ReadFromFile("input.txt");
            if (ReadFromFile. is_open())
            {
                while (!ReadFromFile.eof())
                {
                    getline(ReadFromFile, tg.vehicles[i].make);
                    getline(ReadFromFile, tg.vehicles[i].model);
                    getline(ReadFromFile, tg.vehicles[i].color);
                    ReadFromFile>>tg.vehicles[i].year;

                    getline(ReadFromFile, tg.vehicles[i].engine);
                }
                ReadFromFile>>tg.len;
                ReadFromFile>>tg.width;
                ReadFromFile.close();
            }
            else
                cout << "Failed to open the file\n";
        }
        return tg;
    }
}

void displaygarage(const garage &g)
{
    for(int i = 0; i < amount_vehicles; i++)
    {
        cout << "\nVehicle " << i+1 << ":\n";
        cout << setw(12) << right << "category: ";
        cout << setw(15) << left << g.vehicles[i].category << "\n";
        cout << setw(12) << right << "make: ";
        cout << setw(20) << left << g.vehicles[i].make << "\n";
        cout << setw(12) << right << "model: ";
        cout << setw(20) << left << g.vehicles[i].model << "\n";
        cout << setw(12) << right << "color: ";
        cout << setw(20) << left << g.vehicles[i].color << "\n";
        cout << setw(12) << right << "year: ";
        cout << setw(20) << left << g.vehicles[i].year << "\n";
    }
    cout << "\nGarage size: " << g.len * g. width << " square ft. ";
}
void swap_car(garage &mg, garage &ng)
{
    int choice = 0;
    garage tg;

    while (choice < 1 || choice > amount_vehicles)
    {
        cout << "\nEnter the car you want to swap (1/2/3) or quit with 0: ";
        cin >> choice;

        if (choice == 0 )
            break;
    }

    if (choice != 0 )
    {
        int i = --choice;
        tg.vehicles[i].category = ng.vehicles[i].category;
        tg.vehicles[i].make = ng.vehicles[i].make;
        tg.vehicles[i].model = ng.vehicles[i].model;
        tg.vehicles[i].color = ng.vehicles[i].color;
        tg.vehicles[i].year = ng.vehicles[i].year;

        tg.vehicles[i].category = ng.vehicles[i].category;
        tg.vehicles[i].make = ng.vehicles[i].make;
        tg.vehicles[i].model = ng.vehicles[i].model;
        tg.vehicles[i].color = ng.vehicles[i].color;
        tg.vehicles[i].year = ng.vehicles[i].year;

        tg.vehicles[i].category = ng.vehicles[i].category;
        tg.vehicles[i].make = ng.vehicles[i].make;
        tg.vehicles[i].model = ng.vehicles[i].model;
        tg.vehicles[i].color = ng.vehicles[i].color;
        tg.vehicles[i].year = ng.vehicles[i].year;
    }
}
Oct 16, 2014 at 10:49am
Thank you for all the help... so much time spent and it was either my computer or my compiler.
Oct 16, 2014 at 10:52am
The input file's data still isn't being used.
Oct 16, 2014 at 11:08am
what do you mean by still isnt being used?
Oct 16, 2014 at 11:23am
This is what i have. I assume the zero's are coz i tested with an empty file.
http://picpaste.com/Capture111-2EPxl3Mr.PNG
Oct 16, 2014 at 12:35pm
that is exactly what i get but im importing the information from a file or at least i should be.
Oct 16, 2014 at 12:49pm
Oct 16, 2014 at 12:56pm
Found. So the files being read but the information isn't being input into the program.
Oct 16, 2014 at 1:13pm
it's has to be an issue with how im opening the file or something because i put a
cout << "omg";
inbetween some of the variabeles.
and removed some stuff.
garage populategarage()
{
cout<<"here"<<endl;
system("pause");
garage tg;
string instruction_prompt;

initialize(tg);

for (int i = 0; i < amount_vehicles; i++)
{
cout << i+1;
getline(cin, tg.vehicles[i].category);
ifstream inputFile;
inputFile.open("E\\garage-input-file.txt");
getline(inputFile, tg.vehicles[i].make);
cout << "omg" << tg.vehicles[i].make;
getline(inputFile, tg.vehicles[i].model);
getline(inputFile, tg.vehicles[i].color);
inputFile >> tg.vehicles[i].year;
inputFile >> tg.len;
inputFile >> tg.width;
return tg;
}
}


"omg" didn't come up until the file read check and a loop was removed.
Topic archived. No new replies allowed.