Basic sales + inventory code

Hello. I'm having trouble with this basic sales and inventory code I made. When I enter a quantity of balloons and try to input the quantity afterwards, the else statement keeps popping up. How do I prevent that from doing so? I think I need to use a loop, but I'm not entirely sure on that. Any help would be 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
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
  #include <iostream>
using namespace std;
char choice;

int sumquant=100;
double sumsales=0;

void sale()
{
float quantity;
float total;
cout <<"Please enter the quantity of balloons sold:" <<endl;
cin >>quantity;
total=quantity*0.99;
    if (quantity <= 100) 
		cout<< "Your total sale is $" <<total <<endl;
	else 
		cout<<"Error. Please enter another number.";

sumquant-=quantity;
sumsales+=total;
}

void adding()
{
float quantity1;
cout <<"Please enter the amount of balloons you wish to add:" <<endl;
cin >>quantity1;
sumquant+=quantity1;
int main();
}

void inventorycheck()
{
cout <<"Here is the current number of balloons in the inventory:" <<sumquant<<endl;
int main();
}

void cashin()
{
cout <<"Here is the total sales:" << sumsales<<endl;
int main();
}

int main()
{
	int sumQuant=100; 
cout <<"What would you like to do? \n\Please enter the letter of the corresponding choice." <<endl;//defining the input choices with if statements
cout <<"s) for sale, a) adding an item, i) inventory check, c) cash in, e) exit" <<endl;
cin >>choice;

if (choice=='s')  
{
sale();
}
if (choice=='a')  
{
adding();
}
if (choice=='i')  
{
inventorycheck();
}
if (choice=='c')  
{
cashin();
}
if (choice=='e')  
{
return EXIT_SUCCESS;
}
else 
	cout <<"Not a valid choice. Please enter one of the following: s, a, i, c, or e. Thank you." <<endl;
main();



getchar();
getchar();

	return 0;
}
closed account (48T7M4Gy)
Here's a start. Use while instead of the call to main(). You should be able to see that the validity message will always occur with the way you have set up the if logic. A switch control would be better or you could set a flag as invalid at the start of each loop which is rest to valid if the aresponses is valid, and the message is only displayed if the flag remains invalid.

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

using namespace std; /// AVOID this

char choice;

int sumquant = 100;
double sumsales = 0;

void sale()
{
	float quantity;
	float total;
	cout << "Please enter the quantity of balloons sold:" << endl;
	cin >> quantity;
	total = quantity*0.99;
	if (quantity <= 100)
		cout << "Your total sale is $" << total << endl;
	else
		cout << "Error. Please enter another number.";

	sumquant -= quantity;
	sumsales += total;
}

void adding()
{
	float quantity1;
	cout << "Please enter the amount of balloons you wish to add:" << endl;
	cin >> quantity1;
	sumquant += quantity1;
	int main();
}

void inventorycheck()
{
	cout << "Here is the current number of balloons in the inventory:" << sumquant << endl;
	int main();
}

void cashin()
{
	cout << "Here is the total sales:" << sumsales << endl;
	int main();
}

int main()
{
	int sumQuant = 100;

	char choice = '0'; ///

	/// cout << "What would you like to do? \n\Please enter the letter of the corresponding choice." << endl;//defining the input choices with if statements
	/// cout << "s) for sale, a) adding an item, i) inventory check, c) cash in, e) exit" << endl;
	/// cin >> choice;

	while (choice != 'e')///
	{///
		cout << "What would you like to do? \n\Please enter the letter of the corresponding choice." << endl;//defining the input choices with if statements
		cout << "s) for sale, a) adding an item, i) inventory check, c) cash in, e) exit" << endl;
		cin >> choice;

		if (choice == 's')
		{
			sale();
		}
		if (choice == 'a')
		{
			adding();
		}
		if (choice == 'i')
		{
			inventorycheck();
		}
		if (choice == 'c')
		{
			cashin();
		}
		if (choice == 'e')
		{
			return EXIT_SUCCESS;
		}
		else
			cout << "Not a valid choice. Please enter one of the following: s, a, i, c, or e. Thank you." << endl;
		/// NEVER call main();
	} ///

	///Becaause of choice e this will never happen getchar();
	/// ditto getchar();
		return 0;
	}


Topic archived. No new replies allowed.