Why is my string array not getting input from the txt file?

Why is my string array not getting input from the txt file and why are the next cars not getting input either? The only things I get data put into are all the variables before the first destination, and then it doesn't take any data from the txt file anymore. I've been stuck on this for 4 days and I'm getting exceedingly frustrated.

Text file:
1
2
3
Car    CN    819481   maintenance   false    NONE
Car    SLSF   46871   tank	    true     Memphis
Car    AOK      156   tender        true     McAlester


Code:
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
165
166
167
168
169
170
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#define NUM_CAR 3

using namespace std;

void input();

/*
 ###################################
 Car class to store data about a car.
 ####################################
 */
class Car
{
private:
    int carNumber;
    bool loaded;
    string reportingMark, kind, destination;
public:
    void output();
    void setUp( int,  bool,  string,  string,  string);

    Car();
    Car(Car&);
    Car(const int&, const bool&, const string&, const string&, const string&);
    ~Car();

    friend bool operator==(const Car& car1, const Car& car2);
};

/*
 ###################################
 Main function
 ####################################
 */
int main()
{
    input();
}

/*
 ###################################
 Default constructor
 ####################################
 */
Car::Car()
{
    reportingMark = " ";
    carNumber = 0;
    kind = "other";
    loaded = false;
    destination = "NONE";
}

/*
 ###################################
 Copy Constructor
 ####################################
 */
Car::Car(Car& toBeCopiedFrom)
{
    carNumber = toBeCopiedFrom.carNumber;
    loaded = toBeCopiedFrom.loaded;
    reportingMark = toBeCopiedFrom.reportingMark;
    kind = toBeCopiedFrom.kind;
    destination = toBeCopiedFrom.destination;
}

/*
 ###################################
 Constructor to call setup member function
 ####################################
 */
Car::Car(const int& tempCarNumber, const bool& tempLoaded, const string& tempReportingMark, const string& tempKind, const string& tempDestination)
{
    setUp(tempCarNumber, tempLoaded, tempReportingMark, tempKind, tempDestination);
}

/*
 ###################################
 Destructor that does nothing
 ####################################
 */
Car::~Car() {;}

/*
 ###################################
 Output function used to output the data
 ####################################
 */
void Car::output()
{
    static int counter;
    cout << "\nContents of car " << counter + 1 << ":";
    cout << setw(18) << left;
    cout << "\nreportingMark:" << reportingMark;
    cout << setw(18) << left;
    cout << "\ncarNumber:" << carNumber;
    cout << setw(18) << left;
    cout << "\nkind:" << kind;
    cout << setw(18) << left;
    cout << "\nloaded:" << boolalpha << loaded;
    cout << setw(18) << left;
    cout << "\ndestination:" << destination << endl;
    counter++;
}

/*
 ###################################
 setUp member function takes temporary variables and store them in the object
 ####################################
 */
void Car::setUp(int tempCarNumber,  bool tempLoaded,  string tempReportingMark,  string tempKind,  string tempDestination)
{
    reportingMark = tempReportingMark;
    kind = tempKind;
    destination = tempDestination;
    carNumber = tempCarNumber;
    loaded = tempLoaded;
}

/*
 ###################################
 Overloaded bool operator to compare objects reportingMark and carNumber.
 ####################################
 */
bool operator== (const Car& car1, const Car& car2)
{
    if((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))
        return 1;
    else
        return 0;
}

/*
 ###################################
 Input function used to take input from user and then call a function to store it
 ####################################
 */
void input()
{
    bool loaded[3];
    int number[3];
    string type[3], arr[3], kind[3], destination[3];
    ifstream inputFile("cardata.txt");
    Car car[NUM_CAR];
    
    if(!inputFile)
    {
        cerr << "Error: cardata.txt not opened.\n";
        exit(EXIT_FAILURE);
    }

    for(int index = 0; index < NUM_CAR; index++)
    {
        inputFile >> type[index] >> arr[index] >> number[index] >> kind[index] >> loaded[index];
        
        while(inputFile.peek() == ' ')
            inputFile.get();
        getline(inputFile, destination[index]);
        car[index].setUp(number[index], loaded[index], arr[index], kind[index], destination[index]);
        car[index].output();
    }
    inputFile.close();
    inputFile.clear();
}
Last edited on
Your basic problem is trying to read "true" or "false" without setting boolalpha mode on inputFile. Add this before the for loop of input:
 
inputFile >> boolalpha;

And you don't need 3-element arrays for the input variables. One of each would do since they can be reused.
And inputFile.clear() after closing the file isn't going to do anything useful. You don't even need to close the file since that will happen automatically when input() returns since inputFile is a local variable (i.e., it's destructor will close the file).
Last edited on
In your input() function you're trying to insert a string ("false") into a bool variable, which causes the input stream to fail so no other operations on that stream are possible until the error is cleared. You will need to first insert this value into a string variable then convert that string into a bool.


Also there is no reason for those arrays for the data entry, you can just use non-array variables instead.

And remember that car will go out of scope at the end of this function.


jlb wrote:
You will need to first insert this value into a string variable then convert that string into a bool.

Nope. :-)
Topic archived. No new replies allowed.