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.
#include <iostream>
usingnamespace 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;
}
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.
#include <iostream>
usingnamespace 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;
}