My Program Crashes after input

My C++ program crashes instantly (on Visual Studio 2015) when I input a license # don't know why does it crashes but it works correctly on Dev Compiler. I've even tried taking input with cin as well, but it still crashes



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
  #include<iostream>
//#include<bits/stdc++>


using namespace std;


int main()
{

	char choice;

	string license_no;
	string knockemdead_garage[6];
	string bashemup_garage[6];
	string street[5];

	double revenue = 0;

	int check_1 = 0;
	int check_2 = 0;
	int street_place = 4;
	int bashemup_place = 5;
	int knockemdead_place = 5;


	do
	{
		cout << "Select your choice form following " << endl;
		cout << "a:Arrival		d:Departure 		%:Exit" << endl;
		//cout<<"enter a string"<<flush;
		//getline(cin, name);
		//cout<<name<<endl;
		cin >> choice;
		switch (choice)
		{
		case 'a':
		{
			cin.ignore();
			cout << "Enter your license number:" << flush;
			getline(cin, license_no);
			for (int o = 0; o <= 5; o++)
			{
				if (license_no == knockemdead_garage[o] || license_no == bashemup_garage[o])
				{
					check_2 = 1;
				}
			}
			if (check_2 == 0)
			{
				if (knockemdead_place >= 0)
				{
					knockemdead_garage[knockemdead_place] = license_no;
					knockemdead_place--;
					cout << "Your car is parked in knockemdead Parking Garage." << endl;
				}
				else
				{
					if (bashemup_place >= 0)
					{
						bashemup_garage[bashemup_place] = license_no;
						bashemup_place--;
						cout << "Your car is parked in bashemup Parking Garage." << endl;
					}
					else
					{
						/*if( street_place >= 0 )
						{
						street[street_place] = license_no;
						street_place--;
						cout<<"Your car is parked in street until the no room in garage."<<endl;
						}
						else*/
						//{
						cout << "There is no room in garage and street move to BOSTON" << endl;
						//}
					}
				}
			}
			else
			{
				cout << "The car with license_no " << license_no << " already in garage" << endl;
			}
			check_2 = 0;
		}
		break;
		case 'd':
		{
			cin.ignore();
			cout << "Enter your license number: " << flush;
			getline(cin, license_no);
			for (int i = 0; i <= 5; i++)
			{
				if (license_no == knockemdead_garage[i] || license_no == bashemup_garage[i])
				{
					check_1 = 1;
				}
				if (license_no == knockemdead_garage[i])
				{
					revenue = revenue + 10;
					cout << "Car with license number " << license_no << " departs from knockemdead Parking Garage." << endl;
					cout << "Please pay $10. Thank you...!!!";
					knockemdead_place++;
					for (int j = i; j <= i && j >= 0; j--)
					{
						knockemdead_garage[j] = knockemdead_garage[j - 1];
					}
					/*if ( street_place < 4 )
					{
					license_no = street[4];
					knockemdead_garage[knockemdead_place] = street[4];
					knockemdead_place--;
					cout<<"The car with license No: "<<license_no<<" moved from street and parked in Knockemdead Garage."<<endl;
					/*for ( int j = i; j <= i && j >= 0; j--)
					{
					street[j] = street[j-1];
					}
					street_place++;*/
					//}
				}
				/*if( license_no == street[i] )
				{
				revenue = revenue + 3;
				cout<<"Car with license number "<<license_no<<" departs from street Parking ."<<endl;
				cout<<"Please pay $3. Thank you...!!!";
				street_place++;
				for ( int j = i; j <= i && j >= 0; j--)
				{
				street[j] = street[j-1];
				}
				}*/
				if (license_no == bashemup_garage[i])
				{
					revenue = revenue + 5;
					cout << "Car with license number " << license_no << " departs from Bashemup Parking Garage." << endl;
					cout << "Please pay $5. Thank you...!!!";
					bashemup_place++;
					for (int j = i; j <= i && j >= 0; j--)
					{
						bashemup_garage[j] = bashemup_garage[j - 1];
					}
					/*if ( street_place < 4 )
					{
					license_no = street[4];
					bashemup_garage[bashemup_place] = street[4];
					bashemup_place--;
					cout<<"The car with license No: "<<license_no<<" moved from street and parked in Bashemup Garage."<<endl;
					/*for ( int j = i; j <= i && j >= 0; j--)
					{
					street[j] = street[j-1];
					}
					street_place++;*/
					//}
				}
			}
			if (check_1 == 0)
			{
				cout << "The car with License No: " << license_no << " not in the garage" << endl;
			}
			check_1 = 0;
		}
		break;
		case '%':
		{
			cout << "Total Revenue: " << revenue << endl;
			cout << "Thankyou for visiting" << endl;
			exit(0);
		}
		break;
		default:
		{
			cout << "Please select the coorect choice from given options" << endl;
		}
		break;
		}
		cout << endl << endl;
	} while (choice != '%');
	return 0;
}


write out the array index(s) you are using in the areas around the code you are testing. It seems likely that one of your code blocks is messing up an index and crashing the program.
The problem is this:
1
2
3
4
					for (int j = i; j <= i && j >= 0; j--)
					{
						knockemdead_garage[j] = knockemdead_garage[j - 1];
					}
This loop iterates only once in any case. If i is 0 j - 1 will become invalid/out of bounds.

The same applies to the loop on line 138.
Last edited on
Topic archived. No new replies allowed.