trying to read from a file

i dont know what is going wrong with this code but it has chosen not to do what i want it to do

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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: tolulekes
 *
 * Created on July 2, 2016, 6:24 PM
 */

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>




using namespace std;

class General{
    
    string name;
    string location;
    string destination;
    string seatno;
    string depdate;
    string redate;
    
    string line1;
    
    ofstream myfile;
    
    
public:
    
    void acceptInfo(){
        
        
        cout << "Please what is your full name? " << endl;
        getline (cin, name);
        cout << "Please where are you flying from? " << endl;
        getline (cin, location);
        cout << "Please where are you flying to? " << endl;
        getline (cin, destination);
        cout << "Please what is your preferred seat?" << endl;
        getline (cin, seatno);
        cout << "Please what date do you depart?" << endl;
        getline (cin, depdate);
        cout << "Please what day do you return?" << endl;
        getline (cin, redate);
        
        myfile.open ("Aisosa.txt", ios::in|ios::ate);
        myfile << name << "  " << location << "  " << seatno << "  " << destination << "  " << redate  << endl;
        
        myfile.close();
            }
    
    
    void checkSeats() {
        string something;
        cout << "Who's seat are you checking for?";
        cin >> something ;
        ifstream myfile ("Aisosa.txt");
        
        if (myfile){
            while (getline(myfile, line1)){
                
                if (line1 == something){
                    cout << something << endl;                                        
                }
            }
        }
        else {
            cout << "File could not be opened";
        }
        
        
    }
    
    void editEntry(){
    
    
    }


};
/*
 * 
 */
int main(int argc, char** argv) {
  
    General aise;
    int choice;
    
    while (choice <= 4){
        cout << "Menu:" << endl;
        cout << "1. Enter new entry" << endl;
        cout << "2. Edit old entry" << endl;
        cout << "3. Check how many seats are available" << endl;
        cout << "4. Display all entries" << endl;
        cout << "5. Exit" << endl;
        cout << endl << "Enter choice:" ;
        cin >> choice;
        
        switch (choice)
        { case 1:
            aise.acceptInfo();
            break;
            case 2:
                aise.checkSeats();
                break;
            case 3:
                
                break;
            case 4:
                
                break;
            case 5:
                
                continue;
            default:
                cout << "Please enter only between 1 - 6 only" << endl;                
        }
    }
    
    return 0;
}
It would help to know what you want it to do.
it is just basically supposed to give options to add data to a file and also check the seats in the same file but its running but it isn't giving me the options to select from and it used to.
Line 97 defines variable choice and doesn't assign a value to it. That means it contains who-know-what. Line 99 then checks if who-knows-what is less than or equal to 4. If it isn't, then the while loop is skipped and the program returns. So initialize choice to something (zero?).

Also, you want the loop to execute at least once, which is what a do-while loop is for, so to express this better, change lines 99 and 128 from
1
2
3
while (choice <= 4) {
...
}
to
1
2
3
do {
...
while (choice <= 4);
Tolulekes,

On line 56 myfile.open ("Aisosa.txt", ios::in|ios::ate); opens for input when it needs to be for output. Also before line 56 or on line 56 you need to define myfile as an ofstream otherwise you will have problem. You should also check to make sure the file is open.

In checkSeats() line 66 would work better as getline(cin, something); this way you can type a first name space last name. cin will just read until the first white space so, you only get the first name. For some reason I also had to add the code
1
2
cin.clear();
cin.ignore(CIN_MAX_LIMIT, '\n');

To make the getline() work. I defined CIN_MAX_LIMIT as being 32700 something or just put some large number in that place.

Line 70 your while is reading an entire line from the file. Yet in the if statement you are trying to compare an entire line of data to a small portion and that will never work. I added the variable lname to pick up the last name then added the line name = name + “ “ + lname; so that name would contain first and last name. At the end of the function I added:
1
2
Sleep(5000);  //  to wait 5 seconds to read what was output
Myfile.close();


dhayden mentioned to initialize the variable on line 97. Then in the case statements set choice to zero before the break.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.