I need help please!

So i have been at it for the past couple of days, but i can not seem to find what is wrong, i have a feeling its something to do with my display function, and the EOF. i have been looking in my book, on the web, but nothing seems to work. its a project in my intro c++ class. i am trying just to record cars in a garage, for like a shop or something. it is supposed to be able to enter a car, display cars, and remove a car. any tips?
 
[code]#include <iostream> 

#include <string> #include <fstream>

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
using namespace std;
//function prototypes
void addCar(string nameOfFile);
void disCar(string nameOfFile);
bool removeCar(string);
int main()
{
   const string FILE_NAME = "cars.txt";
   int input =  0;
   bool carRemoved = false;
 string carMake= "";
do
   {
       cout << "1 enter car " << endl;
       cout << "2 display cars " << endl;
       cout << "3 Delete car form garage" << endl;
       cout <<  "4 end"  <<endl;
       cout <<"enter option" << endl;
       cin>>input;
       cin.ignore(100, '\n');
       cout <<endl;
         if (input == 1)
        addCar(FILE_NAME);
       else if (input == 2)
            disCar(FILE_NAME);
       else if (input == 3) {
            disCar(FILE_NAME);
            cout << "^^delete car from above " << endl;
            cout << "Enter car make to be removed " << endl;
            getline (cin, carMake);
            carRemoved == removeCar (carMake);
            if (carRemoved == true)
                cout << " car was not found " << endl;
            else
            {
                cout << " Car was deleted from garage " << endl;
                cout << " below are the availible cars in your garage " << endl;
                disCar(FILE_NAME);
            }
       }
 }    while(input!=4);
}
void addCar(string nameOfFile)
{
 //saves vehicles to file.
    string name = "";
    string make = "";
    int year = 0;
ofstream outFile;
    //open file for append.
    outFile.open(nameOfFile.c_str(), ios::app);
if (outFile.is_open());
    {
        cout << "Enter car manufacturer: (-1 to end)";
        getline(cin, make);
        while (make != "-1")
        {
            //get car name.
            cout << "Enter vehicle name: ";
            getline(cin, name);

            cout <<"Enter year of vehicle: ";
            cin >> year;
            cin.ignore();

            outFile << make << '#'
                    << name << '#'
                    << year << endl;
           cout << " Enter car Manufacturer: (-1 to end) " << endl;
            getline (cin, make);
        }//end while
        //close file
        outFile.close();
    }
}
void disCar(string nameOfFile)
{
    string name = "";
    string make  = "";
    int year =0;
    ifstream inFile;
    inFile.open(nameOfFile.c_str(), ios::in);

    if (inFile.is_open())
    {
        getline(inFile, make, '#');
        getline (inFile, name,'#');
        inFile >> year;
        while (!inFile.eof())
        {
            //display.
            cout << make << ", " <<
                name << ", " << year << endl;
            //read another record.
            getline(inFile, make);
            getline(inFile, name);
            inFile >> year;
        }//end while
        inFile.close();
    }
}
bool removeCar(string carMake)
{
string carInfo[100];
string name, make, year;
bool error = false;
int i = 0;

ifstream inFile;
inFile.open("cars.txt");
if (inFile.is_open())
{
    while (isalpha(inFile.peek()))
        getline(inFile, make);
        if (make != carMake)
        {
            carInfo[i] = make;
            i++;
            getline(inFile, carInfo[i]);
            i++;
            getline(inFile, carInfo[i]);
            i++;
        }
        else
        {
            getline (inFile, name);
            getline(inFile, year);
        }
}
if (name == "")
{
    error =true;
    cout << " File could not be found " << endl;
}
else
{
    cout << " file could not be found " << endl;
}
inFile.close();

ofstream outFile;
outFile.open("cars.txt");
if (outFile.is_open())
{
    for (int x=0; x < i; x++)
    outFile << carInfo[x] << endl;
    outFile.close();
}
else
{
    error = true;
    cout << " file could not be opend" << endl;
}
return error;
}



Last edited on
Please make sure your code is [code]between code tags[/code] so it has syntax highlighting and line numbers.

What specific problem are you having?
it will not save any new car added, it shows up in the file it was added, but will not display full list. so it must be in the disCar function right?
also it ends up sometimes in an endless loop when i use the disCar function. i think that is because i am entering the name as a number, i dont think the computer knows what to do then.
i have a feeling the eof is at blame. any ideas?
On lines 88 and 97, you only read in the year and don't read in the other two variables.

By the way, looping on EOF is bad practice. Although it will work the way you did it, it is better to loop on the input operation itself:
89
90
91
while(inFile >> make >> name >> year)
{
    //... 
it is able to read only one piece of information for some reason.
i have four pieces of information in the file and it only diplays one line from the file.
do you have anything that can fix it as is.
it does not seem to be looping or something.
Please post your current 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//function prototypes
void addCar();
void disCar();
bool removeCar(string);
//the main function
int main()
{
   const string FILE_NAME = "cars.txt";
   int input =  0;
   bool carRemoved = false;
   string carMake= "";

   do//begin loop
   {
       cout << "/////////Welcome to the main menu of the garage///////" << endl;
       cout << " the purpose of this program is to keep track of car in the garage" << endl;
       cout << " please select a number to pick what you wish to do" << endl << endl;

       cout << "1 enter car " << endl;
       cout << "2 display cars " << endl;
       cout << "3 Delete car from garage" << endl;
       cout <<  "4 end"  <<endl;
       cout <<"enter option" << endl;
       cin>>input;

       cin.ignore(100, '\n');
       cout <<endl;
        //function call selection
        //or display error message.
       if (input == 1)
        addCar();
       else if (input == 2)
            disCar();
       else if (input == 3) {
            disCar();
            cout << "^^delete car from above " << endl << endl;
            cout << "Enter car make to be removed: ";
            getline (cin, carMake);
            carRemoved = removeCar(carMake);
            if (carRemoved == true)
                cout << " \n car was not found " << endl;
            else
            {
                cout << " Car was deleted from garage " << endl;
                cout << " below are the availible cars in your garage " << endl;
                disCar();
            } // end if
       }


   }    while(input!=4); //end while

}



void addCar()
{

    //saves vehicles to file.
    string name = "";
    string make = "";
    string year = "";

    ofstream outFile;

    //open file for append.
    outFile.open("cars.txt", ios::app);

    if (outFile.is_open());
    {
        cout << "Enter car manufacturer: (-1 to end)";
        getline(cin, make);
        while (make != "-1")
        {
            //get car name.
            cout << "Enter vehicle name: ";
            getline(cin, name);

            cout <<"Enter year of vehicle: ";
            cin >> year;
            cin.ignore(100, '\n');

            outFile << make << '#'
                    << name << '#'
                    << year << endl;


            cout << " Enter car Manufacturer: (-1 to end) " << endl;
            getline (cin, make);
        }//end while
        //close file
        outFile.close();
    }

}
void disCar()
{
    string name = "";
    string make  = "";
    string year = "";
    ifstream inFile;
    inFile.open("cars.txt", ios::in);
    if (inFile.is_open())
    {
        while (isalpha(inFile.peek()))
        {
            getline(inFile, make);
            getline(inFile, name);
            getline (inFile,year);
            cin.ignore();

            cout << make << ", " <<
                name << ", " << year << "\n";


        }//end while
        inFile.close();
    }
    else
        cout << " !Error!- File could not be found " << endl;
}
bool removeCar(string carMake)
{
string carInfo[100];
string name, make, year;
bool error = false;
int i = 0;

ifstream inFile;
inFile.open("cars.txt");
    if (inFile.is_open())
    {
        while (isalpha(inFile.peek()))
        {
            getline(inFile, make);
                if (make != carMake)
                {
                    carInfo[i] = make;
                    i++;
                    getline(inFile, carInfo[i]);
                    i++;
                    getline(inFile, carInfo[i]);
                    i++;
                }
                else
                {
                    getline (inFile, name);
                    getline(inFile, year);
                }//end if
        }//end while
        if (name =="")
        {
        error = true;
        cout << " !ERROR! - File could not be found " << endl;
        }
    }
else
{
    error = true;
    cout << " File could not be opened " << endl;
}//end if
inFile.close();
ofstream outFile;
outFile.open("cars.txt");
    if (outFile.is_open())
    {
        for (int x=0; x < i; x++)
        outFile << carInfo[x] << endl;
        outFile.close();
    }
    else
    {
        error = true;
        cout << " file could not be opend" << endl;
    }//end if
return error;
}

On lines 90 to 92, you are shooting yourself in the foot by using a format that is annoying to parse.
Topic archived. No new replies allowed.