Help! Program terminating early :(

My program is building and running, but the program terminates after the input without going into the if else section, and nothing is outputted. I'm not sure how to fix it because there aren't any actual errors being displayed. I know I must have coded something wrong, but I can't figure out what. Any help would be greatly appreciated!

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

using namespace std;

int main ()
{
string name, address;
int item = 0, quantity = 0;
int price = 0;
int SIZE = 6;
int VALID_ITEM [6] = {106, 108, 307, 405, 457, 688};
int VALID_ITEM_PRICE [6] = {0.59, 0.99, 4.50, 15.99, 17.50, 39.00};
int sub;
string foundIt = "N";
string MSG_YES = "Item available";
string MSG_NO = "Item not found";

	cout << "Please enter your name: ";
	cin >> name;
	cout << "Please enter your address: ";
	cin >> address;
	cout << "Please enter the item: ";
	cin >> item;
	cout << "Please enter the item quantity: ";
	cin >> quantity;

sub = 1;
while (sub <= SIZE)
{
	if (item == VALID_ITEM [sub])
		foundIt = "Y";
		price = VALID_ITEM_PRICE [sub]; 
	sub = sub + 1;
}
	
if (foundIt == "Y")
	cout << MSG_YES << quantity << " at $" << price << " each" << "Total: " << quantity * price;
else
	cout << MSG_NO;

return 0;
}
Thank you, I went in and read the links you shared, and added the following based on the feedback in those posts. But my program is still not outputting anything. It just askes the user to input and then ends. Did I do the while loop or if/else wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main ()
{

...

std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
}
Please provide the input that you are using, the output that you are getting, and the output that you should get.
I am inputting the following:
name >> Jen
address >> 55
item >> 307
quantity >> 2

I am getting NO output. After inputting quantity and pressing enter, the program terminates.

output should be what is written in the if else section.
If item is found, output: "Item available 1 at $4.50 each Total: 9.00
etc.

It's rough, I know. I will clean it up after, but first I would like it to actually output! lol
There is an overflow I guess, try changing the condition in while to sub< size. Also if you are looking for only one item, break the while once the item is found.

I am not sure weather it works or not, but worth a try
Hmm...that didn't seem to do anything, but thank you for the suggestion cityhunter! When you say break the while, do you mean the following?
1
2
3
4
5
6
7
8
9
sub = 1;
while (sub <= SIZE)
{
	if (item == VALID_ITEM [sub])
		foundIt = "Y";
		price = VALID_ITEM_PRICE [sub]; 
	sub = sub + 1;
	break;
}
Topic archived. No new replies allowed.