Why does my program crash?

Pages: 12
I copied the code in my compiler.
but it gives me compilation error why?

It depends on what the compilation error is. It would help if you pasted the actual text of the error message itself.
Hello stonedviper,

In my work to copy and paste the program into something you could use I pasted part of the code twice which is why it was so big the first time I tried to put it into a message. It appears I deleted the wrong bit of code. The beginning of main should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	// All variables should be initialized.

	//  These lines no longer needed.
	//std::string imenagost{}, egnnagost{}, naselenomqsto{};
	//int n = 0, i = 0, j = 0, k = 0, dalgotraene{};
	//double noshtuvka{};
	//hotel vaas[50], vaas2[50];  // <--- Could use your original code here.

	char martial{ ' ' }, operation{ ' ' };
	std::vector<hotel> single, maried;  // <--- Does not need initilized. Nothing to work with yet.
	hotel cHotel;  // <--- Only need one instance/object to work with. 


This might eliminate one or two errors.

Hope that helps,

Andy
Last edited on
Hello stonedviper,

I just knew I would miss removing some lines of code that would not work.

Any line that says "CLS:" can be either commented out or removed,

Any line that starts "mstd::" an be deleted.

Once I tested and removed those lines it compiled fine for me.

Also had to add the header file "conio.h" for the "_getch();" that I use. This could be replaced with something different and "conio.h" would not be needed.

Sorry about that,

Andy
Last edited on
closed account (48T7M4Gy)
I can't help wonder whether the OP data model needs a bit of a tweak. The array of guests seems to me to be located in the hotel, rather than the current arrangement. So, along those lines:

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
#include <iostream>
#include <string>

using namespace std;

class Hotel
{
    const int hotel_limit = 2; // 2 for testing
    
    string hotel_name;
    
    string* guest = new string[hotel_limit]{};
    int* stay = new int[hotel_limit]{0};
    int no_guests = 0;
    
public:
    Hotel(string aName){hotel_name = aName;}
    
    void setGuest(string aName, int aStay);
    string getGuestName(int aGuestNo);
    string getHotelName(){return hotel_name;}
    
    void listGuests();
};

void Hotel::setGuest(string aName, int aStay)
{
    if(no_guests < hotel_limit)
    {
        guest[no_guests] = aName;
        stay[no_guests] = aStay;
        no_guests++;
    }
    else
        cout << "*** No vacancies ***\n";
}

void Hotel::listGuests()
{
    for(int i = 0; i < no_guests; ++i)
        cout << i << '\t' << guest[i] << ' ' << stay[i] << '\n';
}

int main()
{
    Hotel hilton("Caesar's Palace");
    
    string name;
    int stay = 0;
    
    while(true)
    {
        cout << "Hotel " << hilton.getHotelName() << '\n';
        cout << "A:To add a guest \n";
        cout << "O: Output all guests \n";
        cout << "E: To exit program \n";
        
        char operation;
        cin >> operation;
        
        switch ( toupper(operation) )
        {
            case 'A':
                cout << "       Enter the name of the guest: ";
                cin >> name;
                cout << "Enter how long will the guest stay: ";
                cin >> stay;
                
                hilton.setGuest(name,stay);
                break;
                
            case 'O':
                hilton.listGuests();
                break;
            
            case 'E':
                break;
                
            default:
                ;
        }
    }
}




A:To add a guest
O:Output all guests
E:To exit program
a
Enter the name of the guest
Smith
Enter how long will the guest stay
89
A:To add a guest
O:Output all guests
E:To exit program
a
Enter the name of the guest
Brown
Enter how long will the guest stay
56
A:To add a guest
O:Output all guests
E:To exit program
a
Enter the name of the guest
Black
Enter how long will the guest stay
7
No vacancies
A:To add a guest
O:Output all guests
E:To exit program
o
0 Smith 89
1 Brown 56
A:To add a guest
O:Output all guests
E:To exit program





Last edited on
Well the errors are 50
and i'm using codeblocks so i guess my compiler is c++98

and this is one big error that it gave me

1
2
3
4
5
6
7
8
9
10
#ifndef _CXX0X_WARNING_H
#define _CXX0X_WARNING_H 1
#if __cplusplus < 201103L
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif

#endif



and it thank you again for enlightening me!
not just Mr.Andy all of you :)
Last edited on
Hello stonedviper,

I am not as familiar with Code::Blocks as I would like to be. If you use the command line to compile the program you will need to try -std=c++11. If you use an IDE to work with your code than you will need to edit something in the options to add -std=c++11.

You can change the range based loops to regular for loops like this:

1
2
3
4
5
6
7
for (int lc = 0; lc < single.size(); lc++)
	std::cout << std::setw(19) << "Name: " << single[lc].getname() << "\n"
	<< std::setw(19) << "Id: " << single[lc].getegn() << "\n"
	<< std::setw(19) << "Guest resides at: " << single[lc].getresidence() << "\n"
	<< std::setw(19) << "Length of stay: " << single[lc].getstay() << " Days" << "\n"
	<< std::setw(19) << "Cost: " << "$" << single[lc].getprice() << "\n"
	<< std::setw(19) << "Marital status: " << single[lc].gettype() << "\n" << std::endl;


Notice how you access the member functions differently.

It sounds like your compiler needs to be updated. It will take some time to change the for loops, but it will work. I just changed the first from option "o" and it worked. haha tested it first this time.

Hope that helps,

Andy
I am currently updating to visual studio 2015 I will report on what happend after I install it
It still gives errors.
but I will use your code as an example for a rebuild.
thank you!
Topic archived. No new replies allowed.
Pages: 12