Error in my code!!! Need help please!!

Hi
I have error in my code (Reservation.h). I try to search forum and many thing but always have error. Is there someone who can help me a debugg my program please.

The program should display the following menu:

Press 1 for wifi

Press 2 to offline

If the answer is 1, the program assigns a seat in a bus offline (1 to 40). Otherwise, it allocates a seat in a bus wifi (1 to 40). In addition, the program should display on the screen a travel document that indicates whether the passenger in a bus or a bus wifi offline and how many passengers there on the bus.

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
//Bus.h
#ifndef BUS_H
#define BUS_H

#include <iostream>

using namespace std;

const int wifi = 1;

const int offline = 2;

class Bus
{
    public:
        Bus(int statusf):status (statusf),
        nbSeat (40),
        nbSeatAllocated (0){
        }
        ~Bus(){
        }
        int isWifi(){
        return status;
        }
        int seatAllocated(){
        return nbSeatAllocated;
        }
        int reservedSeat(){
        return (nbSeat < nbSeatAllocated ? nbSeatAllocated++:0);
        }

    private:
        int nbSeat;
        int nbSeatAllocated;
        int status;
};

#endif // BUS_H

//Reservation.h
#ifndef RESERVATION_H
#define RESERVATION_H
#include "Bus.h"
#include <iostream>

using namespace std;

class Reservation
{
    public:
        Reservation(){
        busWifi = new bus (wifi);
        busOffline = new bus (offline);
}
~Reservation(){
delete busWifi;
delete busOffline;
}
int reservedWifi(){
return busWifi -> reservedSeat();
}
int reservedOffline(){
return busOffline -> reservedSeat();
}
int seatAllocatedWifi(){
return busWifi -> seatAllocated();
}
    private:
        bus*busWifi;
        bus*busOffline;
};

#endif // RESERVATION_H


//main .ccp
#include <iostream>
#include "Reservation.h"

using namespace std;

int main(int argc, char** argv)
{
Reservation reserv;int reply = 0;
int nbSeat;


cout <<"Menu for reservation:" << endl;

do {
cout << "1:\tType 1 for offline:"<< endl;
cout << "2:\tType 2 for Wifi:"<< endl;
cout << "3:\tType 3 to quit..."<< endl;
cin >> reply;

if( reply == 1 )
{
nbSeat = reserv.reservedOffline();
if( nbSiege > 0 )
cout << "offline " << nbSeat << endl;
else
cout << "Bus offline is full..." << endl;
}
if( reply == 2 )
{
nbSeat = reserv.reservedWifi();
if( nb_siege > 0 )
cout << "Wifi " << nbSeat << endl;
else
cout << "Bus wifi is full..." << endl;
}

cout << "Press a button..." << endl;
cin >> reply;
}while( reply != 3 );
return 0;
}
Try this, see if it runs correctly, and if you have any questions feel free to ask, I will be happy to help. Your biggest problem was just forgetting to capitalize the 'B' in Bus.

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
//Bus.h
#ifndef BUS_H
#define BUS_H

#include <iostream>

using namespace std;

const int wifi = 1;

const int offline = 2;

class Bus
{
    public:
        Bus(int statusf):status (statusf),
        nbSeat (40),
        nbSeatAllocated (0){
        }
        ~Bus(){
        }
        int isWifi(){
        return status;
        }
        int seatAllocated(){
        return nbSeatAllocated;
        }
        int reservedSeat(){
        return (nbSeat < nbSeatAllocated ? nbSeatAllocated++:0);
        }

    private:
        int nbSeat;
        int nbSeatAllocated;
        int status;
}*busWifi,*busOffline;

#endif // BUS_H

//Reservation.h
#ifndef RESERVATION_H
#define RESERVATION_H
#include "Bus.h"
#include <iostream>

using namespace std;


class Reservation
{
    public:
        Reservation(){
}
~Reservation(){
delete busWifi;
delete busOffline;
}

int reservedWifi(){
return busWifi -> reservedSeat();
}
int reservedOffline(){
return busOffline -> reservedSeat();
}
int seatAllocatedWifi(){
return busWifi -> seatAllocated();
}
};

//main.cpp
#include <iostream>
#include "Reservation.h"

using namespace std;

int main(int argc, char** argv)
{
Reservation reserv;int reply = 0;
busWifi = new Bus (wifi);
busOffline = new Bus (offline);

int nbSeat;


cout <<"Menu for reservation:" << endl;

do {
cout << "1:\tType 1 for offline:"<< endl;
cout << "2:\tType 2 for Wifi:"<< endl;
cout << "3:\tType 3 to quit..."<< endl;
cin >> reply;

if( reply == 1 )
{
nbSeat = reserv.reservedOffline();
if( nbSeat > 0 )
cout << "offline " << nbSeat << endl;
else
cout << "Bus offline is full..." << endl;
}
if( reply == 2 )
{
nbSeat = reserv.reservedWifi();
if( nbSeat > 0 )
cout << "Wifi " << nbSeat << endl;
else
cout << "Bus wifi is full..." << endl;
}

cout << "Press a button..." << endl;
cin >> reply;// would suggest you change this,
             // because it is easy to make an endless loop
             // use a char or string instead
}while( reply != 3 );
return 0;
}
Last edited on
Hi, thank for response

I have this error with this code in Code block

fatal error: Reservation.h: No such file or directory
Well do you have a file named Reservation.h?
I have file named Reservation.h in the Headers folder

This error appear on line 72 with correct code of guatemala007 (33)
Last edited on
Are you sure the name of the file is identical to the name you're using in the include statement?

Do you have the Headers folder in your include path?
Hi, thank for your help

Reservation.h is ok now but the program does not work as I want.

The menu appears correctly but do not book a seat Assigns a bus.
It immediately wrote
When I press 1 the program writes: Bus is full offline
When I press 2 program writtes: Wifi Bus is full

I want:

If the answer is 1, the program Assigns a seat in a bus offline (1 to 40). Otherwise, it allocates a seat in a bus wifi (1 to 40). In addition, the program display on the screen shoulds a travel document That Indicates Whether the passenger in a bus or a bus wifi and offline How Many passengers on the bus there.

Here my correct 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
//Bush.h
#ifndef BUS_H
#define BUS_H

#include <iostream>

using namespace std;

const int wifi = 1;

const int offline = 2;

class Bus
{
    public:
        Bus(int statusf):status (statusf),
        nbSeat (40),
        nbSeatAllocated (0){
        }
        ~Bus(){
        }
        int isWifi(){
        return status;
        }
        int seatAllocated(){
        return nbSeatAllocated;
        }
        int reservedSeat(){
        return (nbSeat < nbSeatAllocated ? nbSeatAllocated++:0);
        }

    private:
        int nbSeat;
        int nbSeatAllocated;
        int status;
}*busWifi,*busOffline;

#endif // BUS_H

//Reservation.h
#ifndef RESERVATION_H
#define RESERVATION_H
#include "Bus.h"
#include <iostream>

using namespace std;


class Reservation
{
    public:
        Reservation(){
}
~Reservation(){
delete busWifi;
delete busOffline;
}

int reservedWifi(){
return busWifi -> reservedSeat();
}
int reservedOffline(){
return busOffline -> reservedSeat();
}
int seatAllocatedWifi(){
return busWifi -> seatAllocated();
}
};
#endif // RESERVATION_H


//main.ccp
#include <iostream>
#include "Reservation.h"

using namespace std;

int main(int argc, char** argv)
{
Reservation reserv;char reply = 0;
busWifi = new Bus (wifi);
busOffline = new Bus (offline);

int nbSeat;


cout <<"\n\t\t\tMenu for reservation\n" << endl;

while(reply != '3' ) {
cout << "\n\tOffline bus :  1";
cout << "\n\tWifi bus    :  2";
cout << "\n\tExit...     :  3";
cout << "\n\n\tEnter your option:   ";
cin >> reply;

if( reply == '1' )
{
nbSeat = reserv.reservedOffline();
if( nbSeat > 0 )
cout << "offline " << nbSeat << endl;
else
cout << "Bus offline is full...";
}
if( reply == '2' )
{
nbSeat = reserv.reservedWifi();
if( nbSeat > 0 )
cout << "Wifi " << nbSeat << endl;
else
cout << "Bus wifi is full..." << endl;
}


cin >> reply;
}
return 0;
}

Last edited on
Try giving bus a default constructor.
Your code will be much easier to read and understand - both for you, and for anyone else trying to read it - if you use consistent indentation. It will make it much easier to see at a glance which code is inside which method, and inside which class.

I can see why it's doing what it's doing. In the constructor for Bus, you initialise nbseat to 40, and nbSeatAllocated to 0. So when you call Reservation::reservedOffline(), which calls through to Bus::reservedSeat(), nbSeat < nbSeatAllocated is always false. The method will always return 0.
yelnatz, I don't see anywhere in the code where a default constructor would be called. Where are you seeing that?
Topic archived. No new replies allowed.