Program Crashing?

Super beginner so please don't judge too harshly! If you select any random number (outside of the ones in the switch construct), and then select 6, whether it is right after that random number, or a few selections later, it crashes. This was a group final assignment for my Intro. to Programming Concepts class, my teammate bailed lol. I've already submitted the assignment and will very likely lose points for it crashing but it's better than a 0 lol

ALSO! I needed to count the amount of items selected and I couldn't figure out how :( Help! I know my assignment is already submitted but I'd like to learn anyways!

EDIT (Updated 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
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
/*
 * File Name:		fantastivaPOS.cpp
 * Description:		Displays an event driven menu, the subtotal and then the selected items and the total.
 */

// Preprocessor Directives
#include <iostream>
#include <iomanip>

using namespace std;

// Prototype
void displayMenu(string menu[], double price[], int size);
void displaySubTotal(double total, int menuChoice);
int getChoice();
double processMenu(string menu[], double price[], int menuChoice, double total, int selectedOptions[], int counter);

/*
 * Name:			main()
 * Parameters:		None.
 * Processes:		
 * Return Value:	An integer representing an error code; if the program ends without error, zero 
 *					will be returned to the calling program or operating system.
 */
int main()
{
	// Constants and Variables
	int menuChoice = 0, counter = 0;
	double total = 0.0;
	const int SIZE = 5;
	int selectedOptions[SIZE] = {0};
	string menu[SIZE] = {"Steak and Shake combo","Deluxe Burger combo", "Noodle and Shrimp combo", "Chicken and Rice combo", "Surf n' Turf combo"};
	double price[SIZE] = {17.99, 12.99, 11.99, 10.99, 15.99};
	
	// Welcome message
	cout << "Welcome to Fantastiva! It's great to see ya!\n";
	cout << "\nMay I take your order?\n\n";

	// Restaurant Menu
	do
	{
		// Calling functions
		displayMenu(menu, price, SIZE);
		menuChoice = getChoice();
		total = processMenu(menu, price, menuChoice, total, selectedOptions, counter);
		displaySubTotal(total, menuChoice);
		
		// Storing selections
		selectedOptions[counter] = menuChoice -1;
		if(menuChoice !=6)
		{
		   counter++; 
		}
	}
	while (menuChoice != 6);

	system("pause");
	return 0;
}

/*
 * Name:			displayMenu()
 * Parameters:		menu[] Retrieves the menu items.
 *					price[] Retrieves the menu prices.
 *					size Retrieves the constant value for the size of the array.
 *					selectedOptions Retrieves the stored input.
 * Processes:		Display the event driven menu for the restaurant.
 * Return Value:	Nothing.
 */

void displayMenu(string menu[], double price[], int size)
{
	// Output
	cout << fixed << setprecision(2);
	cout << "\n\t\tMenu\n\n";
	for (int i = 0; i < size; i++)
	{
		cout << i + 1 << ". " << menu[i] << "." << setw(15) << "\t$ " << price[i] << endl;
	}
	cout << "6. Nothing else. \n";
}

/*
 * Name:			displaySubTotal()
 * Parameters:		price[] Retrieves the menu prices.
 *					size Retrieves the constant value for the size of the array.
 *					total Retrieves the amount total.
 *					menuChoice Retrieves the input.
 * Processes:		Display the event driven menu for the restaurant.
 * Return Value:	Nothing.
 */


void displaySubTotal(double total, int menuChoice)
{
	// Output
	if (menuChoice != 6)
	{
		cout << "\n\tTotal so far: "<< setw(20) << "$ " << total << endl << endl;
	}
}


/*
 * Name:			getChoice()
 * Parameters:		None.
 * Processes:		Receives the user's menu selection.
 * Return Value:	The user's selection from the event driven  menu.
 */

int getChoice()
{
	// Constants and Variables
	int menuChoice = 0;

	// Input
	cout << "\n\nEnter item: ";
	cin >> menuChoice;
	
	return menuChoice;
}

/*
 * Name:			processMenu()
 * Parameters:		meu[] Retrieves the menu items.
 *					price[] Retrieves the menu prices.
 *					size Retrieves the constant value for the size of the array.
 *					menuChoice Retrieves the selection the user made from the menu.
 *					total Retrieves the total amount.
 *					selectedOptions Retrieves the stored input.
 *					counter Retrieves the counter to store input.
 * Processes:		Display the event driven menu for the restaurant.
 * Return Value:	sumToAdd Returns this value to be calculated in another function.
 */

double processMenu(string menu[], double price[], int menuChoice, double total, int selectedOptions[], int counter)
{
	// Constants and Variable
	int k = 0;
	double sumToAdd = 0.0;
	const int SIZE = 5;
	

	// Calculations
		switch (menuChoice)
		{
			case 1:
				sumToAdd += price[0];
				total = total + sumToAdd;
				break;
			case 2:
				sumToAdd += price[1];
				total = total + sumToAdd;
				break;
			case 3:
				sumToAdd += price[2];
				total = total + sumToAdd;
				break;
			case 4:
				sumToAdd += price[3];
				total = total + sumToAdd;
				break;
			case 5:
				sumToAdd += price[4];
				total = total + sumToAdd;
				break;
			case 6:
				// Storing menu selection
				selectedOptions[counter] = menuChoice;

				// Output
				cout << "\nThat's:\t";
				for (int k = 0; k < counter; k++)
				{
					menuChoice = selectedOptions[k];
					cout << menu[menuChoice] << "\n\t";
				}

				cout << "\n\n\tThe total is: " << setw(19) << "\t$ " << total;
				cout << "\n\nThank you for choosing Fantastiva! Have a Fantastic day!\n\n";
				break;
			default:
				cout << "\n******************ERROR*********************\n"
					<< "*         Invalid menu selection.          *\n"
					<< "********************************************\n\n";
		}

		return total;
}
Last edited on
You are getting a segmentation fault when you call displayTotal after hitting 6. A segmentation fault occurs when there is an access violation (like when you access outside the bounds of an array, in this instance).

The menu[] array has 5 elements, therefore it has indexes menu[1], menu[2], menu[3], and menu[4]. However, when you select 6, that value gets subtracted by 1 (which is 5) and is stored inside selectedOptions[1] in this case. In line 114, you index menu[menuChoice]. Notice that 5 is stored inside selectedOptions[1], therefore you try to access menu[5], which is past the bounds of the array.

That is the problem.

In my honest opinion, I would completely restructure your code because finding where that segfault occurred honestly made my eyes bleed. You have multiple functions with multiple parameters that you do not even need, and you have many unused variables. This makes it extremely difficult to see whats going on, even though what you are trying to accomplish is very very very simple.

Also, please, if you can, get rid of that god-awful counter variable that is being incremented. You will continue to run into segfaults so long as you are using it since it is being incremented unconditionally as long as the menuChoice isn't 6.

Furthermore, I would try to look into std::vector instead of using regular arrays:
https://en.cppreference.com/w/cpp/container/vector
Last edited on
Because the instructions for the assignment was that main() only calls functions and that each function only does one thing.

I don't know what segmentation fault means :c And may I ask which variables aren't I using? Other than amount, I was planning on using that after I posted and forgot to take it out before copying for this post haha
Last edited on
I removed displayTotal() and placed it inside the switch, but still getting the error since I didn't touch Line 52.
I'm not allowed to use anything that wasn't covered in class, and vectors weren't covered in class. Basically I'm only supposed to use functions and arrays, and main() should only call functions and functions should only do one thing inside them.
If I remove the counter I can't display the stored items, I'm sorry my code made you feel like your eyes would bleed. It's my first Introduction to Programming Concepts class and it's basically just been a "Learn by yourself" course :\ I don't know how to display the chosen items at the end any other way.
If you aren't allowed to use std::vector, then I would suggest you use arrays, but you need to change the selectedOptions array implementation and GET RID OF the counter variable as those are whats causing you the segmentation fault.

What I suggest you do instead is make selectedOptions store exactly SIZE elements (5), however instead of storing the actual menu selections, make each array index store the NUMBER OF ITEMS PURCHASED. For example, if the user selects Steak and Shake combo 3 times, and then Deluxe combo 2 times, the values of the selectedOptions array would look like this:

selectedOptions[] = {3, 2, 0, 0, 0};

Now, you can go into displayTotal, and instead of indexing menu[] with the values inside selectedOptions, make a nested loop (two loops inside each other) to go through selectedOptions like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int i = 0; i < 5; ++i)
{

     for(int j = 0, j < selectedOptions[i]; ++j)
     {
             switch(i)
             {
                  case 1: std::cout << "Steak and Shake\n"; //This is for Steak and Shake
                  case 2:                                                        //This is for Deluxe
                  //...and so on and so forth
             }

     }

}

I did this on inspection so I haven't tested if this works, but it should.

Just make sure you implement the selectedOptions array as an array of numbers of purchases for each item and make sure you increment that particular array index every time that particular purchase is made. You can probably figure the rest out yourself.

Of course, this is a very suboptimal solution. If I were you I would completely start over and restructure my program
Last edited on
I'll try that, thank you very much! My assignment will probably make my professor mad but I wanted to learn since I don't really get feedback from my assignments!
No problem. In the future, make sure to plan your design ahead of time.

Also, get rid of the following variables: k and sumToAdd on lines 139 and 140. You never use them, so they are unnecessary.
Last edited on
I've been working on this project for over a week :*( It wasn't a lack of planning. It was a lack of understanding lol This Introduction to Programming Concepts class really didn't help me understand Programming Concepts lol
Question, do you really need to store the menu items in an array called menu[] and the prices in price[]? Did it say to do that in the rubric? If not, then there is a better way to do it. Since you already know the number of menu items and prices, you can simply code that directly into the switch statement. Heres some pseudocode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

int main()
{
    int user_selection, total = 0;
    while(true)
    {
        display_menu()
        std::cin >> user_selection;
        switch(user_selection)
        {
            case 1:
                total += 17.99;
            //and so on...
            case 6: break;
        }
        display_subtotal();
        if(user_selection == 6) break;
    }
    display_total();
    std::cin.get(); //Avoid system("pause") at all costs and use this instead;
    return 0;
}


You could do something like that and simplify your code 100 times more.
Topic archived. No new replies allowed.