Why does my program crash?

Pages: 12
Hello!
I am building a hotel program.Which is still not finished.
but I encountered a problem in which the program crashes and I was wondering if someone can explain to me why?And help me please!

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
  #include <iostream>
#include <string>
//#include "hotel.h"


using namespace std;

class hotel
{
    string name,egn,residence;
    int stay;
    float price;
    char type;
public:
    void setguest(string,string,string,int,float,char);
    string getname();
    string getegn ();
    string getresidence();
    int getstay ();
    float getprice();
    char gettype ();


};
void hotel::setguest(string ime,string ssn,string mestopolojenie,int prestoi,float cena,char polojenie)
{
    name=ime;
    egn=ssn;
    residence=mestopolojenie;
    stay=prestoi;
    price=cena;
    type=polojenie;
}

string hotel::getname(){return name;}


string hotel::getegn(){return egn;}


string hotel::getresidence(){return residence;}


int hotel::getstay() {return stay;}

float hotel::getprice(){return price;}

char hotel::gettype(){return type;}


int main()
{
    string imenagost,egnnagost,naselenomqsto;
    int n=0,i=0,j=0,k=0,dalgotraene;
    float noshtuvka;
    char martial,operation;
    hotel *vaas=new hotel [n],*vaas2=new hotel [j];

    while(operation!='e')
    {
        cout<<"To add a guest type in the letter a \n";
        cout<<"To output all data entered type in the letter o \n";
        cout<<"To output how much have family guests paid who have stayed for more than 5 days type in the letter n \n";
        cout<<"To output information for the single guests type in the letter s \n";
        cout<<"To exit program type in the the letter e \n";

        cin>>operation;

        switch (operation)
        {
        case 'a':
            {
                n++;
                for(i;i<n;i++)
                {
                    cout<<"Enter the name of the guest \n";
                    cin>>imenagost;
                    cout<<"Enter ID number \n";
                    cin>>egnnagost;
                    cout<<"Enter where does the guest reside";
                    cin>>naselenomqsto;
                    cout<<"Enter how long will the guest stay \n";
                    cin>>dalgotraene;
                    cout<<"Enter how much does it cost stay per night \n";
                    cin>>noshtuvka;
                    cout<<"Enter martial status \n";
                    cin>>martial;
                    vaas[i].setguest(imenagost,egnnagost,naselenomqsto,dalgotraene,noshtuvka,martial);

                }
                break;

            }
        }
    }
    delete [] vaas2;
    delete [] vaas;


}
Please include the error messages
Hello stonedviper,

Do I have to guess at where it is caching? My guess would be line 57. On line 54 you define variables and initialize the to zero. This is good. Then on like 57 while "n" and "j" are still zero you define an array of size zero. If the program does not crash here then it is most likely crashing when you try to access the array.

My initial thought without testing the code.

Hope that helps,

Andy
Well it does compile but it crashes after I initialize the martial char variable.
it basically stops working and crashes.
I will edit the code when I get back from work.
Still thank you!
Well the n and j are basically a way of adding single or married guests using the dynamic memory so that's why they are 0 after that if the user wants to enter a guest it increments itself so that it stores one guest and so on.
Hello stonedviper,

As I said earlier on line 54 you initialize your variables. This is good, bur you missed the last one and all the variables after that. "operation" became a problem when you tried to use it in the while condition.

This:
hotel *vaas=new hotel [n],*vaas2=new hotel [j]; which is the same as:
hotel *vaas=new hotel [0],*vaas2=new hotel [0];

When I set "n" and "j" to 5 the program worked. Before you create an array like this "n" and "j" need a value greater than zero.

Creates a zero length array which points to nothing. Soon on line 88 vaas[i] has no element zero to access. This is where the program crashes because the array has no size.

The for loop in case 'a' is problem:

1
2
 n++;  // <--- Does not need to be here.
 for(i;i<n;i++)


line 1 makes the for loop larger than it needs to be. Thus you will try to access the array past the end of the array.

Line 2 "i" should be set to zero or you could end up with a for loop the will not work. Most time it is just better to set up the for loop as for (int i = 0; i < N; i++).

The {} braces after "case 2:" are not needed, but OK if you leave them.

Just some thoughts.

Hope that helps,

Andy
ok thank you i will try it when i get home but untill then i want to know does it add a guest one at a time whenever i chose to?
Hello stonedviper,

No it adds one less than "n" until the value of "i" exceeds "n" when you choose "a". Th second time you choose "a" the value of "i" could start at a value greater than "n". I have not tested that use of the program yet.

BTW changing the value of "n" after line 57 does not change what is done on line 57. Once line 57 is processed/executed the program does not change wht happened there just because the the values of "n" or "j" change later.

Hope that helps,

Andy
I'm home now and thank you for your quick response I really appreciate it.
Anyway I'm back home fed and ready I will take your advice and report on it.
Thank you again :)
Well I know how to make my program.
and I did what I could but sadly i can't bypass the fixed size arrays vaas and vaas 2

this is my program

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
#include <iostream>
#include <string>
//#include "hotel.h"


using namespace std;

class hotel
{
    string name,egn,residence;
    int stay;
    float price;
    char type;
public:
    void setguest(string,string,string,int,float,char);
    string getname();
    string getegn ();
    string getresidence();
    int getstay ();
    float getprice();
    char gettype ();


};
void hotel::setguest(string ime,string ssn,string mestopolojenie,int prestoi,float cena,char polojenie)
{
    name=ime;
    egn=ssn;
    residence=mestopolojenie;
    stay=prestoi;
    price=cena;
    type=polojenie;
}

string hotel::getname(){return name;}


string hotel::getegn(){return egn;}


string hotel::getresidence(){return residence;}


int hotel::getstay() {return stay;}

float hotel::getprice(){return price;}

char hotel::gettype(){return type;}

float totalprice (float sleepprice,int staytime)
{
    float caine;
    caine=sleepprice*staytime;
    return caine;
}


int main()
{
    string imenagost,egnnagost,naselenomqsto;
    int n=0,i=0,j=0,k=0,dalgotraene;
    float noshtuvka;
    char martial,operation;
    hotel vaas[50],vaas2[50];

    while(true)
    {
        cout<<"A:To add a guest \n";//<<izpalneno
        cout<<"O:Output all guests \n";//<<izpalneno
        cout<<"N:To output how much did family guests paid to stay for 5 or more days \n";//<<izpalneno
        cout<<"S:To output single guests \n";
        cout<<"E:To exit program \n";//<<izpalneno

        cin>>operation;

        switch (operation)
        {
        case 'a':
            {
                n++;

                while(i<n)
                {
                    cout<<"Enter the name of the guest \n";
                    cin>>imenagost;
                    cout<<"Enter ID number \n";
                    cin>>egnnagost;
                    cout<<"Enter where does the guest reside \n";
                    cin>>naselenomqsto;
                    cout<<"Enter how long will the guest stay \n";
                    cin>>dalgotraene;
                    cout<<"Enter room price \n";
                    cin>>noshtuvka;
                    cout<<"Enter martial status \n";
                    cin>>martial;
                    if(martial=='s')
                    {
                        j++;
                        while(k<j)
                        {
                            vaas2[k].setguest(imenagost,egnnagost,naselenomqsto,dalgotraene,noshtuvka,martial);
                            k++;
                        }

                    }
                    vaas[i].setguest(imenagost,egnnagost,naselenomqsto,dalgotraene,noshtuvka,martial);
                    i++;

                }

                break;
            }

        case 'o':
            {
                for(i=0;i<n;i++)
                {
                    cout<<"Name of guest "<<vaas[i].getname()<<"\nId number of guest "<<vaas[i].getegn()<<"\nResidence "<<vaas[i].getresidence()
                    <<"\nStay "<<vaas[i].getstay()<<"\nRoom price "<<vaas[i].getprice()<<"\nMartial status "<<vaas[i].gettype()<<"\n\n";
                }
                break;
            }
        case 's':
            {
                for(k=0;k<j;k++)
                {
                    cout<<"Name of guest "<<vaas2[k].getname()<<"\nId number of guest "<<vaas2[k].getegn()<<"\nResidence "<<vaas2[k].getresidence()
                    <<"\nStay "<<vaas2[k].getstay()<<"\nRoom price "<<vaas2[k].getprice()<<"\nMartial status "<<vaas2[k].gettype()<<"\n\n";
                }
                break;
            }
        case 'n':
            {
                for(i=0;i<n;i++)
                {
                    if(vaas[i].gettype()!='s')
                    {
                    if(vaas[i].getstay()>=5)
                    {
                        cout<<"Family guest "<<vaas[i].getname()<<" has payed a total of "<<totalprice(vaas[i].getprice(),vaas[i].getstay())
                        <<" for his stay\n";
                    }
                    }
                }
                break;
            }
        case 'e':
            {
                return 0;
            }


    }
    }


}


thanks for the help though :)
Last edited on
Hello stonedviper,

I will get this this out of the way first. Change your "float"s to "doubles"s. "doubles"s are the preferred type these days. Also have a look at this, lines 3 and 4,: http://www.cplusplus.com/forum/beginner/221554/#msg1017350

I see you added a function. At first look I do not see anything wrong with the function. Although you could just as easily say: return sleepprice*staytime;

Lines 60 - 63 always initialize your variables. Some you have and some you have not.

Line 64 was fine with your original code you just needed a different variable set to a number greater than zero. This works though.

line 74 I would follow with operation=tolower(operation);, so the switch has the proper case for the letters.

Line 66 is an endless loop with no way out. Your original code here is better,

Lines 79 and I think line 153, hard to tell the way the code is indented, do not have to be there, but it is OK if they are.

Line 80 should not be needed or be there, but it does work with line 82 now.

Line 82 along with line 80 I believe this while loop will execute only once. So, that makes the while loop mostly unnecessary.

Lines 84 to 95 re OK, but you might want to consider this:

1
2
3
4
5
6
7
8
9
10
11
12
std::cout << "Enter the name of the guest: ";
std::cin >> imenagost;
std::cout << "Enter ID number: ";
std::cin >> egnnagost;
std::cout << "Enter where does the guest reside: ";
std::cin >> naselenomqsto;
std::cout << "Enter how long will the guest stay: ";
std::cin >> dalgotraene;
std::cout << "Enter how much does it cost stay per night: ";
std::cin >> noshtuvka;
std::cout << "Enter martial status: ";
std::cin >> martial;

This just makes the output and input look better.

Line 96 is a good addition, but the while loop is not really needed. The point is to store the information in one array or the other. You just need to keep track of the value of the variable used in the subscript for "vaas" and "vaas2", "i" and "k" respectfully, since they are likely to be of different values.
Lines 11 on the additional case statements. They look OK for now I will have to test them out and see what happens.

Line 147 Not the way I would have done it, but the return will either exit the while loop or the program, not sure which yet.

My last note for now is to watch your indenting. Proper indenting makes the code easier to read. As n example lines 153 and 154 it is hard to tell which } lines up with the opening {. And the case statements would look better indented from the switch {}s.

Hope that helps,

Andy
I see thank you!
It's not actually the best but this is the way i thought it works best a couple of others came to mind but this one satisfies me the most.
thank you again though.
the return 0 statement is just so the user can exit the program;
I will take your advice.
A question though can I use a more dynamic arrays? instead of a fixed size?
Thanks again :)
Last edited on
Hello stonedviper,

You can always use the code that works best for you, but this may not always be the best way. You should always strive to find a better simpler way of preforming a task.

When I loaded the new code I did see how the "return 0" works. I was not sure about it when I first saw it.

A question though can I use a more dynamic arrays? instead of a fixed size?

Yes. Your original code will work fine as long as "n" and "j" have a size greater than zero. Usually people who use a dynamic array will either get the size from the user or do some kind of math to figure the size. And they do not normally do this at the beginning of a function, but later in the function.

My thought would be not to use an array, but to use a vector. Similar to an array, but much easier to us. Create one instance/object of the class where you can store your information and when its filled put it into the vector. Later you can use "vectorName.size()" to get the number of elements in the vector and use a for loop with a subscript on the vector to print out each element. I will have to revise your code to use a vector and show you later.

In the if statement in "case 2" I changed the condition from "!=" to "=="., as I see you did to, and it worked like it should. I also added "else" to go with the "if" because it was storing the same information in both arrays. The while loop is unnecessary because at this point you just need to store the information in one array or the other. My version looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (martial == 's')
{
	//j++; // <--- Not needed without the while loop.
	//while (k<j)  // <--- While loop not needed.
	//{
		vaas2[k].setguest(imenagost, egnnagost, naselenomqsto, dalgotraene, noshtuvka, martial);
		k++;
	//}

}
else  // <--- These lines need to be in an else.
{
	vaas[i].setguest(imenagost, egnnagost, naselenomqsto, dalgotraene, noshtuvka, martial);
	i++;
}


This works s long as "vaas2" is for "single". Otherwise the arrays need to be reversed or the if condition would need changed.

When I tested option "o" it only printed one array, so you are missing some information.

I still need to check out the other options today.

This is the beginning of main with the changes I made. See what you think.

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
	// All variables should be initialized.
	std::string imenagost{}, egnnagost{}, naselenomqsto{};
	int n = 0, i = 0, j = 0, k = 0, dalgotraene{};
	double noshtuvka{};
	char martial{ ' ' }, operation{ ' ' };
	hotel vaas[50], vaas2[50];  // <--- Could use your original code here.
        //constexpr int size{ 50 };  // <--- Or just "const". Just needs to be an actual number to use in the next line.
        //hotel *vaas=new hotel [size],*vaas2=new hotel [size];


	while (true)
	{
		std::cout << "\nA:To add a guest \n";//<<izpalneno
		std::cout << "O:Output all guests \n";//<<izpalneno
		std::cout << "N:To output how much did family guests paid to stay for 5 or more days \n";//<<izpalneno
		std::cout << "S:To output single guests \n";
		std::cout << "E:To exit program \n";//<<izpalneno
		std::cout << "\nEnter operation: ";
		std::cin >> operation;

		operation = tolower(operation);  // <--- Added.

		std::cout << std::endl;

		switch (operation)
		{
		case 'a':
		{  // <--- Not necessary, but OK.
			n++;

			//while (i<n)  // <--- This while loop may work, but not really needed. Same goes for a or loop.
			//{
				std::cout << "Enter the name of the guest: ";
				std::cin >> imenagost;
				std::cout << "Enter ID number: ";
				std::cin >> egnnagost;
				std::cout << "Enter where does the guest reside: ";
				std::cin >> naselenomqsto;
				std::cout << "Enter how long will the guest stay: ";
				std::cin >> dalgotraene;
				std::cout << "Enter how much does it cost stay per night: ";
				std::cin >> noshtuvka;
				std::cout << "Enter martial status: ";
				std::cin >> martial;
				if (martial == 's')
				{
					//j++; // <--- Not needed without the while loop.
					//while (k<j)  // <--- While loop not needed.
					//{
						vaas2[k].setguest(imenagost, egnnagost, naselenomqsto, dalgotraene, noshtuvka, martial);
						k++;
					//}

				}
				else  // <--- These lines need to be in an else.
				{
					vaas[i].setguest(imenagost, egnnagost, naselenomqsto, dalgotraene, noshtuvka, martial);
					i++;
				}

			//}

			break;
		}  // <--- Not necessary, but OK. 

Read the comments in the code and see how I changed the "cout" statements. This presentation looks better. Just something you pick up with experience.

That is as far as I naged to get last night. I will work on it more today.

Hope that helps,

Andy
I see it's good thank you!

I will try it after work it certanly looks more organized than mine.
I will try it after work.
Hello stonedviper,

This took me longer to finish than I first thought. The code went quickly, but the comments took the most time. I realize that some of the code could be improved on, but I feel that the concepts and ideas are more important for learning. I have tried to put as many comments into the program as I could think of. If there is anything that needs better explained let me know.


I got tired of typing each time I ran the program, so I added the code to read a file to get started with. This also demonstrates how you can read from a file and still add to the vectors. The code for opening may be a bit long, but I think it is good when first learning using a file for input. I have similar code for opening an output file, just a little extra code to deal with the output file, if you are interested.

This morning I had the revelation to make the "Add" function a member function of the class to avoid passing all the variables and vectors needed to make it work. As member function of the class it gives direct access to the private variables, so you can change directly and avoid the "set" function. Then back in "case 2:" use the if/else to check marital status and put the information into the correct vector.

I put the code in the next message because it is to long for here.


Hope this helps you learn something,

Andy
Had to break this up to fit,

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
#include <chrono>
#include <thread>

// Normally I would put this class in a header file and just include it here.
class hotel
{
	std::string name, egn, residence;
	int stay;
	double price;
	char type;
public:
	void setguest(std::string, std::string, std::string, int, double, char);
	std::string getname();
	std::string getegn();
	std::string getresidence();
	int getstay();
	double getprice();
	char gettype();
	void Add();


};

//  I would put these member functions in a separate .cpp file.
void hotel::setguest(std::string ime, std::string ssn, std::string mestopolojenie, int prestoi, double cena, char polojenie)
{
	name = ime;
	egn = ssn;
	residence = mestopolojenie;
	stay = prestoi;
	price = cena;
	type = polojenie;
}

std::string hotel::getname() { return name; }


std::string hotel::getegn() { return egn; }


std::string hotel::getresidence() { return residence; }


int hotel::getstay() { return stay; }

double hotel::getprice() { return price; }

char hotel::gettype() { return type; }

void hotel::Add()
{
	CLS;

	std::cout << "\n Enter the name of the guest: ";
	std::getline(std::cin, name);  // <--- Used for a name like "Bob Smith" otherwise "cin" would just get "Bob".
	std::cout << "\n Enter ID number: ";
	std::getline(std::cin, egn);  // <--- Does not need the "getline" just easier to use here.
	std::cout << "\n Enter where does the guest reside: ";
	std::getline(std::cin, residence);  // <--- Same basic reason as name.
	std::cout << "\n Enter how long will the guest stay: ";
	std::cin >> stay;
	std::cout << "\n Enter how much does it cost stay per night: ";
	std::cin >> price;
	std::cout << "\n Enter martial status: ";
	std::cin >> type;
	// <--- Used after a "cin" or a file read and before a getline to clear the input buffer of the "\n",
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
}

// I would most likely put this in a different .cpp file either by its self or with other functions.
// Or put after main like I did  with other functions and put a prototype here.
double totalprice(double sleepprice, int staytime)
{
	double caine;
	caine = sleepprice*staytime;
	return caine;
}

// Normally I put the prototypes in a separate header file.// ************************ Prototypes ****************************************
char Menu();

void ReadFile(std::ifstream& infile, std::vector<hotel>& single, std::vector<hotel>& maried, hotel& cHotel);
Part 2

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
int main()
{
	// All variables should be initialized.
	std::string imenagost{}, egnnagost{}, naselenomqsto{};
	int n = 0, i = 0, j = 0, k = 0, dalgotraene{};
	double noshtuvka{};
	char martial{ ' ' }, operation{ ' ' };
	hotel vaas[50], vaas2[50];  // <--- Could use your original code here.
	std::vector<hotel> single, maried;
	hotel cHotel;

	// I set this up to eliminate some typing. Works with the "ReadFile" function. to load the vectors.
	std::string iFileName{ "Data.txt" };

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		// <--- I comment out these lines when I know the file opens properly. Uncommen when you first start.
		//std::cout << "\n File " << iFileName << " is open" << std::endl;
		//std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}

	ReadFile(inFile, single, maried, cHotel);

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Put here to change the cout format.

	while (true)
	{
		//  Something here to clear the screen is helpful.

		operation = Menu();

		switch (operation)
		{
		case 'a':
			cHotel.Add();

			if (cHotel.gettype() == 's')
			{
				single.emplace_back(cHotel);
			}
			else  // <--- These lines need to be in an else.
			{
				maried.emplace_back(cHotel);
			}

			break;

		case 'o':  // <--- Only prints 1 array not both. Based on the value of "n" does not work right.
		{
			// Prints marital status single.
			for (auto&& lc : single)
				std::cout << std::setw(19) << "Name: " << lc.getname() << "\n"
				<< std::setw(19) << "Id: " << lc.getegn() << "\n"
				<< std::setw(19) << "Guest resides at: " << lc.getresidence() << "\n"
				<< std::setw(19) << "Length of stay: " << lc.getstay() << " Days" << "\n"
				<< std::setw(19) << "Cost: " << "$" << lc.getprice() << "\n"
				<< std::setw(19) << "Marital status: " << lc.gettype() << "\n" << std::endl;

			// Prints marital status maried.
			for (auto&& lc : maried)
				std::cout << std::setw(19) << "Name: " << lc.getname() << "\n"
				<< std::setw(19) << "Id: " << lc.getegn() << "\n"
				<< std::setw(19) << "Guest resides at: " << lc.getresidence() << "\n"
				<< std::setw(19) << "Length of stay: " << lc.getstay() << " Days" << "\n"
				<< std::setw(19) << "Cost: " << "$" << lc.getprice() << "\n"
				<< std::setw(19) << "Marital status: " << lc.gettype() << "\n" << std::endl;

			mstd::SetColor(14);
			std::cout << "\n\n\n\n Press anykey to continue";
			_getch();
			mstd::SetColor();

			break;
		}
		case 's':  // may work for single, but what about maried?
		{
			std::string idSearch{};

			std::cout << "\n Enter ID nuber of guest: ";
			std::cin >> idSearch;

			// Prints marital status single.
			for (auto&& lc : single)
			{
				if (idSearch == lc.getegn())
					std::cout << '\n' << std::setw(19) << "Name: " << lc.getname() << "\n"
					<< std::setw(19) << "Id: " << lc.getegn() << "\n"
					<< std::setw(19) << "Guest resides at: " << lc.getresidence() << "\n"
					<< std::setw(19) << "Length of stay: " << lc.getstay() << " Days" << "\n"
					<< std::setw(19) << "Cost: " << "$" << lc.getprice() << "\n"
					<< std::setw(19) << "Marital status: " << lc.gettype() << "\n" << std::endl;
			}

			// Prints marital status maried.
			for (auto&& lc : maried)
			{
				if (idSearch == lc.getegn())
					std::cout << '\n' << std::setw(19) << "Name: " << lc.getname() << "\n"
					<< std::setw(19) << "Id: " << lc.getegn() << "\n"
					<< std::setw(19) << "Guest resides at: " << lc.getresidence() << "\n"
					<< std::setw(19) << "Length of stay: " << lc.getstay() << " Days" << "\n"
					<< std::setw(19) << "Cost: " << "$" << lc.getprice() << "\n"
					<< std::setw(19) << "Marital status: " << lc.gettype() << "\n" << std::endl;
			}
			std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
			break;
		}
		case 'n':
		{
			for (auto&& lc : single)
				if (lc.gettype() == 's')
					if (lc.getstay() >= 5)
						std::cout << "Family guest " << lc.getname() << " has payed a total of " << totalprice(lc.getprice(), lc.getstay()) << " for his/her stay.\n";
					else
						std::cout << "\n Nothing matches.\n" << std::endl;

			for (auto&& lc : maried)
				if (lc.gettype() == 'm')
					if (lc.getstay() >= 5)
						std::cout << "Family guest " << lc.getname() << " has payed a total of " << totalprice(lc.getprice(), lc.getstay()) << " for his/her stay.\n";
					else
						std::cout << "\n Nothing matches.\n" << std::endl;

			std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
			break;
		}
		case 'e':
		{
			return 0;
		}


		}
	}  // End while. <--- Good lable when you can not see the opening {.

	return 0;
}  // End main

char Menu()
{
	char operation{ ' ' };

	do
	{
                // Something here to clear the screen is helpful.

		std::cout << "\nA:To add a guest \n";//<<izpalneno
		std::cout << "O:Output all guests \n";//<<izpalneno
		std::cout << "N:To output how much did family guests paid to stay for 5 or more days \n";//<<izpalneno
		std::cout << "S:To output single guests \n";
		std::cout << "E:To exit program \n";//<<izpalneno
		mstd::SetColor(14);
		std::cout << "\nEnter operation: ";
		mstd::SetColor(11);
		std::cin >> operation;
		mstd::SetColor();

		operation = tolower(operation);  // <--- Added.

		if (operation == 'a' || operation == 'o' || operation == 'n' || operation == 's' || operation == 'e')
		{
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			return operation;
		}
		else
		{
			std::cout << "\n Invalid entry try again.Choices are \"A\", \"O\", \"N\", \"S\" or \"E\"" << std::endl;
			std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		}
	} while (true);
}

void ReadFile(std::ifstream& inFile, std::vector<hotel>& single, std::vector<hotel>& maried, hotel& cHotel)
{
	std::string imenagost{}, egnnagost{}, naselenomqsto{};
	int dalgotraene{};
	double noshtuvka{};
	char martial{ ' ' };

	while (std::getline(inFile, imenagost))
	{
		std::getline(inFile, egnnagost);
		std::getline(inFile, naselenomqsto);
		inFile >> dalgotraene;
		inFile >> noshtuvka;
		inFile >> martial;
		inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

		cHotel.setguest(imenagost, egnnagost, naselenomqsto, dalgotraene, noshtuvka, martial);

		if (martial == 'm')
			maried.emplace_back(cHotel);
		else
			single.emplace_back(cHotel);

	}
}
And the "data.txt" file I used.

Andy
101
Marysville, OH
6
50
s
Bob Smith
202
Columbus, OH
4
60
m
Sally Ride
303
Delware, OH
2
50
s
Isabeau
404
Keyton, OH
7
70
m
Navarre
505
Columbus, IN
7
50
m
Saphira Dragon
606
Navarre, OH
2
70
m
Imperius
707
Navarre, OH
4
50
m
I copied the code in my compiler.
but it gives me compilation error why?
Also doest it matter what kind of a compiler i use?
and what is your compiler?
Hello stonedviper,

I use Visual Studio 2015 with the C++11 standard. Some of the code might new than your compiler or I may have missed removing some lines that you would not be able to use.

Let me know what errors you are getting and what you are using as a compiler and I will do what I can to fix it,

Andy

Edit: Anywhere it says "CLS;" you can comment out or delete.
Last edited on
Pages: 12