Input Error causes infinite loop. Suggested fixes causing program to break.

Hell everyone, first time posting here. I decided to get a head start on my Final for my intro to programming class. However on the home stretch I have encountered a very annoying problem. The program prints a restaurant menu and the user inputs the numbers for each item they like, and then inputs 17 to get their total and order summary. But when a user inputs anything other than a number the stream goes crazy, because its a char instead of an int. I looked up many solutions but the annoying part is when i transfer them to my program, it ends up breaking. So I really need some help guys thanks a bunch! (its long forgive me)
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
#include <iostream> 
#include <string> 
#include <iomanip>
using namespace std;

struct itemsonMenu //structure since they are good for outputting lists of things. in this case item and price 
{					// this will output together when i call it in the main function
	string menuitem; //string type
	double itemCost; //double type since price for ex. is 11.75 rather than just 11
};

void showMenu(itemsonMenu list[], int size) //calls in structure of menu item and price together to be out put once called in main
{
	cout << "\t\t" "Input the number of the items that you would like.""\n\n";
	cout << "\t\t" "After each input, press the Enter button." "\n\n";
	cout << "\t\t" "When you are done, type the number 17 and then the Enter button to find out your total." "\n\n\n" << endl;
	for (int i = 0; i < size; i++) //goes with size of list. in this case 16. output .) and $ in front of each item. can change if more items added
	{
		cout << "\t\t" << i + 1 << " .) " << list[i].menuitem << "$ " << list[i].itemCost << endl; //structure.   i+1 to start num at 1 rather than 0
		cout << "\n";
	}
	return;
}

void getData(itemsonMenu list[], int size) //menu output. menuitem=item  menuprice=price size of 16 in this case0-15
{
	list[0].menuitem = "Three Egg Omelet.\t\t"; //array starts at 0. output at 1.
	list[0].itemCost = 8.55;
	list[1].menuitem = "Breakfast Burritto.\t";
	list[1].itemCost = 10.75;
	list[2].menuitem = "Egg Sandwich.\t\t";
	list[2].itemCost = 7.75;
	list[3].menuitem = "Traditional Pancakes.\t";
	list[3].itemCost = 7.75;
	list[4].menuitem = "Cinnamon Toast Pancakes.\t";
	list[4].itemCost = 9.25;
	list[5].menuitem = "Oatmeal.\t\t\t";
	list[5].itemCost = 1.75;
	list[6].menuitem = "Bowl of Fruit.\t\t";
	list[6].itemCost = 2.55;
	list[7].menuitem = "Bowl of Cereal.\t\t";
	list[7].itemCost = 1.75;
	list[8].menuitem = "Juice.\t\t\t";
	list[8].itemCost = 1.75;
	list[9].menuitem = "Milk.\t\t\t";
	list[9].itemCost = 1.75;
	list[10].menuitem = "Coffee.\t\t\t";
	list[10].itemCost = 1.25;
	list[11].menuitem = "Smoothie.\t\t\t";
	list[11].itemCost = 1.55;
	list[12].menuitem = "Milkshake.\t\t";
	list[12].itemCost = 2.55;
	list[13].menuitem = "Hot Cocoa.\t\t";
	list[13].itemCost = 1.25;
	list[14].menuitem = "Pure Maple Syrup.\t\t";
	list[14].itemCost = 1.15;
	list[15].menuitem = "Hashbrown Side.\t\t";
	list[15].itemCost = 1.25;

}

int main()
{
	cout << "\n""\t\t\t\t" "Welcome to Ron'n'Jack Breakfast Shack!" << endl;
	cout << "\t\t\t\t" "-------------------------------------" "\n\n" << endl;
	itemsonMenu menuList[16] = {}; // 16 items on the menu struc
	itemsonMenu itemArr[16] = {}; //16 items on the menu struc
	int itemNum = 0;
	int j = 0;
	double totalCost=0;
	bool Order = true, number;
	char answer;
	while (Order == true)
	{
		getData(menuList, 16); //pull fnc getData to show
		showMenu(menuList, 16); // pull fnc showMenu to show
		while (cin >> itemNum && itemNum != 17)//while menu item number does not equal 17(break away number), run through
		{

			if (itemNum > 17 || itemNum < 1) //Numbers greater than 17 OR less than 1 don't pass
			{
				cout << "Please input a valid item number." << endl;
				continue; //restarts loop
			}
			itemArr[j].menuitem = menuList[itemNum - 1].menuitem; //menu list option number. Since arrays start at 0, we must take the value of itemNum and subtract 1 to match up with the array index.
			//**Usually Breaks here**//
			itemArr[j].itemCost = menuList[itemNum - 1].itemCost; //menu list option price
			j++; //next index in the array
		}

		cout << "You ordered:" << endl << endl;
		for (int i = 0; i < j; i++) //Counting through the array for each item ordered
		{
			cout << itemArr[i].menuitem << "\t$" << itemArr[i].itemCost << endl; //Printing each item ordered and their cost
			totalCost += itemArr[i].itemCost;
			cout << "\n\n\n";
		}

		cout << "Your total is: " << totalCost << endl << endl;
		cout << "Input 'n' for a new order or 'Q' to quit \t";
		cin >> answer;
		if (answer == 'q' || answer == 'Q')
		{
			return 0;
		}
		else while (answer != 'n')
		{
			cout << "Please make a valid choice.\t";
			cin >> answer;
		}
		totalCost = 0;
		j = 0;
		cout << endl << endl;
	}
	system("pause");
	return 0;
}
Replace line 77 to 89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (cin >> itemNum && itemNum != 17)//while menu item number does not equal 17(break away number), run through
{
    if (itemNum > 17 || itemNum < 1) //Numbers greater than 17 OR less than 1 don't pass
        {
	    cout << "Please input a valid item number." << endl;
	     continue; //restarts loop
        }
        itemArr[j].menuitem = menuList[itemNum - 1].menuitem; //menu list option number.      
       // Since arrays start at 0, we must take the value of itemNum and subtract 1 to 
       //matchup    
       //with the array index.
			//**Usually Breaks here**//
	itemArr[j].itemCost = menuList[itemNum - 1].itemCost; //menu list option price
	j++; //next index in the array
}


with

1
2
3
4
5
6
7
8
9
10
11
12
while((cin>>itemNum).fail() || itemNum > 17 || itemNum < 1)
{
    cout<<"Please enter correct input"<<endl;
    cin.clear();     //reset status bits
    cin.ignore();   //ignore new line (if character was entered)
} //end of while
itemArr[j].menuitem = menuList[itemNum - 1].menuitem; //menu list option number. Since
  //arrays start at 0, we must take the value of itemNum and subtract 1 to match up with   
  //the array index.
	//**Usually Breaks here**//
itemArr[j].itemCost = menuList[itemNum - 1].itemCost; //menu list option price
j++; //next index in the array 
Last edited on
Hmm that's fixed the infinite looping problem, but this blasted program breaks no matter what I try

Here's the error message: Unhandled exception at 0x524C31CA (msvcr120d.dll) in FinalProject.exe: 0xC0000005: Access violation reading location 0x61D9BCAE.
I dont know why you get those errors. Maybe you are doing something wrong on your compiler.
I assume you are on Visual Studio because of your errors.
Paste this code, do a clean build and run again.

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
#include <iostream> 
#include <string> 
#include <iomanip>
using namespace std;

struct itemsonMenu //structure since they are good for outputting lists of things. in this case item and price 
{					// this will output together when i call it in the main function
	string menuitem; //string type
	double itemCost; //double type since price for ex. is 11.75 rather than just 11
};

void showMenu(itemsonMenu list[], int size) //calls in structure of menu item and price together to be out put once called in main
{
	cout << "\t\t" "Input the number of the items that you would like.""\n\n";
	cout << "\t\t" "After each input, press the Enter button." "\n\n";
	cout << "\t\t" "When you are done, type the number 17 and then the Enter button to find out your total." "\n\n\n" << endl;
	for (int i = 0; i < size; i++) //goes with size of list. in this case 16. output .) and $ in front of each item. can change if more items added
	{
		cout << "\t\t" << i + 1 << " .) " << list[i].menuitem << "$ " << list[i].itemCost << endl; //structure.   i+1 to start num at 1 rather than 0
		cout << "\n";
	}
	return;
}

void getData(itemsonMenu list[], int size) //menu output. menuitem=item  menuprice=price size of 16 in this case0-15
{
	list[0].menuitem = "Three Egg Omelet.\t\t"; //array starts at 0. output at 1.
	list[0].itemCost = 8.55;
	list[1].menuitem = "Breakfast Burritto.\t";
	list[1].itemCost = 10.75;
	list[2].menuitem = "Egg Sandwich.\t\t";
	list[2].itemCost = 7.75;
	list[3].menuitem = "Traditional Pancakes.\t";
	list[3].itemCost = 7.75;
	list[4].menuitem = "Cinnamon Toast Pancakes.\t";
	list[4].itemCost = 9.25;
	list[5].menuitem = "Oatmeal.\t\t\t";
	list[5].itemCost = 1.75;
	list[6].menuitem = "Bowl of Fruit.\t\t";
	list[6].itemCost = 2.55;
	list[7].menuitem = "Bowl of Cereal.\t\t";
	list[7].itemCost = 1.75;
	list[8].menuitem = "Juice.\t\t\t";
	list[8].itemCost = 1.75;
	list[9].menuitem = "Milk.\t\t\t";
	list[9].itemCost = 1.75;
	list[10].menuitem = "Coffee.\t\t\t";
	list[10].itemCost = 1.25;
	list[11].menuitem = "Smoothie.\t\t\t";
	list[11].itemCost = 1.55;
	list[12].menuitem = "Milkshake.\t\t";
	list[12].itemCost = 2.55;
	list[13].menuitem = "Hot Cocoa.\t\t";
	list[13].itemCost = 1.25;
	list[14].menuitem = "Pure Maple Syrup.\t\t";
	list[14].itemCost = 1.15;
	list[15].menuitem = "Hashbrown Side.\t\t";
	list[15].itemCost = 1.25;

}

int main()
{
	cout << "\n""\t\t\t\t" "Welcome to Ron'n'Jack Breakfast Shack!" << endl;
	cout << "\t\t\t\t" "-------------------------------------" "\n\n" << endl;
	itemsonMenu menuList[16] = {}; // 16 items on the menu struc
	itemsonMenu itemArr[16] = {}; //16 items on the menu struc
	int itemNum = 0;
	int j = 0;
	double totalCost=0;
	bool Order = true, number;
	char answer;
	while (Order == true)
	{
		getData(menuList, 16); //pull fnc getData to show
		showMenu(menuList, 16); // pull fnc showMenu to show

		while((cin>>itemNum).fail() || itemNum > 17 || itemNum < 1)
		{
			cout<<"Please enter correct input"<<endl;
			cin.clear();
			cin.ignore();
		}
		itemArr[j].menuitem = menuList[itemNum - 1].menuitem; //menu list option number. Since arrays start at 0, we must take the value of itemNum and subtract 1 to match up with the array index.
		//**Usually Breaks here**//
		itemArr[j].itemCost = menuList[itemNum - 1].itemCost; //menu list option price
		j++; //next index in the array

		cout << "You ordered:" << endl << endl;
		for (int i = 0; i < j; i++) //Counting through the array for each item ordered
		{
			cout << itemArr[i].menuitem << "\t$" << itemArr[i].itemCost << endl; //Printing each item ordered and their cost
			totalCost += itemArr[i].itemCost;
			cout << "\n\n\n";
		}

		cout << "Your total is: " << totalCost << endl << endl;
		cout << "Input 'n' for a new order or 'Q' to quit \t";
		cin >> answer;
		if (answer == 'q' || answer == 'Q')
		{
			return 0;
		}
		else while (answer != 'n')
		{
			cout << "Please make a valid choice.\t";
			cin >> answer;
		}
		totalCost = 0;
		j = 0;
		cout << endl << endl;
	}
	system("pause");
	return 0;
}
Interesting, that did the trick, thanks! Is there a way I can change it so that it accepts multiple items rather than just 1 per order?
Do something like this
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
#include <vector>
#include <iostream>
using namespace std;

int main()
{
	vector<int> orders;
	int itemNum;

	cout<<"Enter orders separated by a space. Enter -1 to end"<<endl;
	
	//NB DONT FORGET TO DO THE INPUT CHECK
	cin>>itemNum;
	do
	{
		orders.push_back(itemNum);
		cin>>itemNum;
	}while(itemNum != -1);

	for(int i = 0; i < orders.size(); i++)
	{
		cout<<"You choosed menu item "<<orders.at(i)<<endl;
	}

	system("pause");
	return 0;
}
Hope this is what you want
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
#include <iostream> 
#include <string> 
#include <vector>
#include <iostream>
using namespace std;

struct itemsonMenu //structure since they are good for outputting lists of things. in this case item and price 
{					// this will output together when i call it in the main function
	string menuitem; //string type
	double itemCost; //double type since price for ex. is 11.75 rather than just 11
};

void showMenu(itemsonMenu list[], int size) //calls in structure of menu item and price together to be out put once called in main
{
	cout << "\t\t" "Input the number of the items that you would like.""\n\n";
	cout << "\t\t" "After each input, press the Enter button." "\n\n";
	cout << "\t\t" "When you are done, type the number 17 and then the Enter button to find out your total." "\n\n\n" << endl;
	for (int i = 0; i < size; i++) //goes with size of list. in this case 16. output .) and $ in front of each item. can change if more items added
	{
		cout << "\t\t" << i + 1 << " .) " << list[i].menuitem << "$ " << list[i].itemCost << endl; //structure.   i+1 to start num at 1 rather than 0
		cout << "\n";
	}
	return;
}

void getData(itemsonMenu list[], int size) //menu output. menuitem=item  menuprice=price size of 16 in this case0-15
{
	list[0].menuitem = "Three Egg Omelet.\t\t"; //array starts at 0. output at 1.
	list[0].itemCost = 8.55;
	list[1].menuitem = "Breakfast Burritto.\t";
	list[1].itemCost = 10.75;
	list[2].menuitem = "Egg Sandwich.\t\t";
	list[2].itemCost = 7.75;
	list[3].menuitem = "Traditional Pancakes.\t";
	list[3].itemCost = 7.75;
	list[4].menuitem = "Cinnamon Toast Pancakes.\t";
	list[4].itemCost = 9.25;
	list[5].menuitem = "Oatmeal.\t\t\t";
	list[5].itemCost = 1.75;
	list[6].menuitem = "Bowl of Fruit.\t\t";
	list[6].itemCost = 2.55;
	list[7].menuitem = "Bowl of Cereal.\t\t";
	list[7].itemCost = 1.75;
	list[8].menuitem = "Juice.\t\t\t";
	list[8].itemCost = 1.75;
	list[9].menuitem = "Milk.\t\t\t";
	list[9].itemCost = 1.75;
	list[10].menuitem = "Coffee.\t\t\t";
	list[10].itemCost = 1.25;
	list[11].menuitem = "Smoothie.\t\t\t";
	list[11].itemCost = 1.55;
	list[12].menuitem = "Milkshake.\t\t";
	list[12].itemCost = 2.55;
	list[13].menuitem = "Hot Cocoa.\t\t";
	list[13].itemCost = 1.25;
	list[14].menuitem = "Pure Maple Syrup.\t\t";
	list[14].itemCost = 1.15;
	list[15].menuitem = "Hashbrown Side.\t\t";
	list[15].itemCost = 1.25;

}

int main()
{
	cout << "\n""\t\t\t\t" "Welcome to Ron'n'Jack Breakfast Shack!" << endl;
	cout << "\t\t\t\t" "-------------------------------------" "\n\n" << endl;

	itemsonMenu menuList[16] = {}; // 16 items on the menu struc
	int itemNum = 0;
	char answer;
	vector<int> orders;

	while (true)
	{
		double totalCost=0;
		getData(menuList, 16); //pull fnc getData to show
		showMenu(menuList, 16); // pull fnc showMenu to show
	
		cout<<"Enter your orders. Press Enter after every input. Enter 0 to end"<<endl;
		do
		{
			
			while((cin>>itemNum).fail() || itemNum < 0 || itemNum > 17)
			{
				cout<<"Enter correct input : ";
				cin.clear();
				cin.ignore();
			}
			orders.push_back(itemNum - 1);
		}while(itemNum != 0);

		cout<<endl<<"Your orderd"<<endl;
		for(int i = 0; i < orders.size() - 1; i++)
		{
			cout<<menuList[orders.at(i)].menuitem<<"\t\t$ "<<menuList[orders.at(i)].itemCost<<endl;
			totalCost += menuList[orders.at(i)].itemCost;
		}

		cout <<endl<<"Your total is: " << totalCost << endl << endl;
		cout << "Input 'n' for a new order or 'Q' to quit \t";
		cin >> answer;
		if (answer == 'q' || answer == 'Q')
		{
			return 0;
		}
		else while (answer != 'n')
		{
			cout << "Please make a valid choice.\t";
			cin >> answer;
		}
		cout << endl << endl;
	}
	system("pause");
	return 0;
}
Wow man this is working great, thank you a billion times. This is exactly the solution that I needed. I tried everything to get it working, but your changes made it work beautifully. Thanks dood!
Happy coding..
Topic archived. No new replies allowed.