reading from a file help

I need help getting the output from my file.

what is in the file:
Car CN 819481 maintenance false None
Car SLSF 46871 business true Memphis
Car AOK 156 tender true McAlester

my result I get is:

reportingMark:CN
carNumber: 819481
kind: maintenance
loaded: false
destination:

Process returned 0 (0x0) execution time : 0.038 s
Press any key to continue.

The output I should be getting is:

reportingMark:CN
carNumber: 819481
make: maintenance
loaded: true
destination: None

reportingMark:SLSF
carNumber: 46871
make: business
loaded: true
destination: Memphis

reportingMark:AOK
carNumber: 156
make: tender
loaded: true
destination: McAlester

Process returned 0 (0x0) execution time : 0.022 s
Press any key to continue.


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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>


using namespace std;

/* ******************** class Car ********************
create class Car
*/

class Car
{
private:
    string reportingMark;
    int carNumber;
    string kind;
    bool loaded;
    string destination;

public:
    Car(); // default constructor
    Car(const Car &oldObject); // coy constructor
    Car(string reportingMark,int carNumber, string kind, bool loaded, string destination){setUp(reportingMark,carNumber,kind, loaded, destination);}
    ~Car(){}; // display nothing
    void output();
    void setUp(string, int, string, bool, string);
    friend bool operator==(const Car &, const Car &); //friend
};

void input();

int main()
{
    input();
    return 0;
}
/* ******************** Car::Car ********************
default constructor
*/

Car::Car()
{
    reportingMark = "";
    carNumber = 0;
    kind = "other";
    loaded = false;
    destination = "NONE";
}

/* ******************** Car::Car ********************
copy constructor
*/

Car::Car(const Car &oldObject)
{
    reportingMark = oldObject.reportingMark;
    carNumber = oldObject.carNumber;
    kind = oldObject.kind;
    loaded = oldObject.loaded;
    destination = oldObject.destination;
}

/* ******************** Car::showInput ********************
uses the input from user and displays it onto the screen
*/

void Car::output()
{
    cout << endl;
    cout << setw(14)<< left<< "reportingMark:" << setw(5)<< left << reportingMark << endl;
    cout << setw(14)<< left<<"carNumber:" << setw(5)<< left << carNumber << endl;
    cout << setw(14)<< left<<"kind:" << setw(5)<< left << kind << endl;
    switch(loaded)
    {
    case 1:
        cout << setw(14)<< left<<"loaded:" << setw(5)<< left << "true" << endl;
        break;
    default:
        cout << setw(14)<< left<<"loaded:" << setw(5)<< left << "false" << endl;
        break;
    }
    cout <<setw(14)<< left<< "destination:" << setw(5)<< left  << destination << endl;
}

/* ******************** Car::setUp ********************
puts everything from user input into the class
*/

void Car::setUp(string ReportingMark, int CarNumber, string Kind, bool Loaded, string Destination)
{
    reportingMark = ReportingMark;
    carNumber = CarNumber;
    kind = Kind;
    loaded = Loaded;
    destination = Destination;
}

/* ******************** bool operator== ********************
checks if car1.reportingMark and car2..reportingMark are same
checks if car1.carNumber and car2.carNumber are the same
*/

bool operator==(const Car &car1, const Car &car2)
{
    if((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))
        return true;
}

/* ******************** Input ********************
ask for user input on reporting mark, car number, kind, loaded, and destination
*/

void input()
{
    string type;
    string ReportingMark;
    int CarNumber;
    string Kind;
    bool Loaded;
    string Destination;

    ifstream inputFile;
    inputFile.open("car.txt", ios::in);
    if(inputFile.fail())
    {
        cerr << "File has failed to open." << endl;
        exit(1);
    }

    while(inputFile.peek() != EOF)
    {
        while(inputFile.peek() == ' ')
        {
            inputFile.get();
        }
        inputFile >> type;
        inputFile >> ReportingMark;
        inputFile >> CarNumber;
        inputFile >> Kind;
        inputFile >> Loaded;

        getline(inputFile, Destination);

        Car temp(ReportingMark, CarNumber, Kind, Loaded, Destination);
        temp.output();
    }

    inputFile.close();
}


Please and thank you
The problem is that the stream operators are not very sophisticated when it comes to bool values. It uses 0 as false and 1 as true. In your case it finds an 'f' character which is neither '0' or '1' so the read operation fails. You will have to change your text file to use 0 and 1 instead of true and false, or read the value as a string and check in your program if it's equal to "true" or "false".
thanks I got it now
Topic archived. No new replies allowed.