-
Keeps getting error
everything runs perfect but in my purchase() I can cin what i need but I keep getting an error and the same goes for my sell() everything else runs fine until i quit the program by command
Code:
#include <iostream.h>
#include <stdlib.h>
int coke = 0, pepsi = 0, dry = 0, hires = 0, brand, squantity, x, y, quantity, totalcoke, totalpepsi, totaldry, totalhires, soldcoke, soldpepsi, solddry, soldhires, pcoke = 0, ppepsi = 0, pdry = 0, phires = 0, addcoke ,addpepsi, adddry, addhires, coke_add, pepsi_add, dry_add, hires_add;
void initialize();
void instructions();
void sale_type(char option);
void inventory(), purchased(),sell(), display_inventory(), quit();
void main()
{
initialize();
instructions();//call instructions function, say goodbye to main forever
}//end function
void initialize()
{
cout << "\n\n Please enter the amount of Coke cases: ";
cin.clear();
cin >> coke_add;
coke = coke_add;
cout << " Please enter the amount of Pepsi cases: ";
cin.clear();
cin >> pepsi_add;
pepsi = pepsi_add;
cout << " Please enter the amount of Canada Dry cases: ";
cin.clear();
cin >> dry_add;
dry = dry_add;
cout << " Please enter the amount of Hires cases: ";
cin.clear();
cin >> hires_add;
hires = hires_add;
}
void instructions()
{
char option;//declare option variable, this will be used OVER AND OVER
do
{
cout << "\n\nWhat would you like to do?\n";
cout << " (E)nter Inventory\n";
cout << " (P)urchase Soda\n";
cout << " (S)old Soda\n";
cout << " (D)isplay Inventory\n";
cout << " (Q)uit\n";//print us some options
cin >> option;
cin.ignore(80,'\n'); //take in an answer
sale_type(option);//send that answer to sale_type!
}while(option!='Q' && option!='q');//keep on keepin on till they pick exit!
//end program
}//end function
void sale_type(char option)
{
switch (option)//roll thru options
{
case 'E': case 'e': //if they picked the number 1, its gonna go to this function,
//thats pretty much how the whole thing works
inventory();
break;
case 'P': case 'p':
purchased();
break;
case 'S': case 's':
sell();
break;
case 'D': case 'd':
display_inventory();
break;
case 'Q': case 'q':
quit();
break;
default://THEY DIDNT READ THE MENU, WHO WOULD HAVE THOUGHT?!?!
cout << "Please read and follow all directions\n";//if directions aren't followed
break;
}
}//end function
void inventory()
{
cout << "\n\n\n-------------------INVENTORY----------------" << endl;
cout << "\n\nPlease enter the inventory for each item "<< endl;
cout << "\nEnter the number of Coca-Cola cases: ";
cin >> addcoke;
while (cin.fail())
{
cin.clear();
cin.ignore(cin.fail());
cout << "Invalid input...please re-enter"<< endl;
cout << "\nEnter the number of Coca-Cola cases: ";
cin >> addcoke;
}
totalcoke = coke + addcoke;
cout << "\nEnter the number of Pepsi cases: ";
cin >> addpepsi;
while (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Invalid input...please re-enter" << endl;
cout << "\nEnter the number of Pepsi cases: " << endl;
cin >> addpepsi;
}
totalpepsi = pepsi + addpepsi;
cout << "\nEnter the number of Canada Dry cases: ";
cin >> adddry;
while (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Invalid input...please re-enter" << endl;
cout << "\nEnter the number of Canada Dry cases: " << endl;
cin >> adddry;
}
totaldry = dry + adddry;
cout << "\nEnter the number of Hires cases: ";
cin >> addhires;
while (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Invalid input...please re-enter" << endl;
cout << "\nEnter the number of Hires cases: " << endl;
cin >> addhires;
}
totalhires = hires + addhires;
//clearline();
}//end function
void purchased()
{
cout << "\n\n\n-------------Purchase Menu------------------" << endl;
cout << "\nBrand Identification numbers are as follow: " << endl;
cout << "1---Coca-Cola" << endl;
cout << "2---Pepsi" << endl;
cout << "3---Canada Dry" << endl;
cout << "4---Hires" << endl;
cout << "\nPlease enter the brand you would like to purchase: ";
cin >> x;
cout << "\nPlease enter the quantity of the brand you would like to purchase: ";
cin >> quantity;
while(x >= 1 && x <= 4)
{
switch(x)
{
case 1:
pcoke = quantity;
totalcoke = totalcoke + quantity;
cout << "Inventory updated..." << endl;
break;
case 2:
ppepsi = quantity;
totalpepsi = totalpepsi + quantity;
cout << "Inventory updated..." << endl;
break;
case 3:
pdry = quantity;
totaldry = totaldry + quantity;
cout << "Inventory updated..." << endl;
break;
case 4:
phires = quantity;
totalhires = totalhires + quantity;
cout << "Inventory updated..." << endl;
break;
default:
cout << "bbThe action you requested is invalid...nothing has been done..." << endl;
}
}
}//end function
void sell()
{
cout << "\n\n\n------------------Sell Menu-------------------------------" << endl;
cout << "\nPlease enter the brand id number you would like to sell: ";
cin >> y;
cout << "\nPlease enter the quantity of the brand you would like to sell: ";
cin >> quantity;
while (y >= 1 && y <= 4)
{
switch(y)
{
case 1:
soldcoke = squantity;
totalcoke = totalcoke - squantity;
while (!cin.fail() && quantity > 0)
{
if(squantity > coke)
{
cout << "Insufficient inventory to fill sell order, nothing changed." << endl;
}
}
cout << "Inventory updated...";
break;
case 2:
soldpepsi = squantity;
totalpepsi = totalpepsi - squantity;
cin >> squantity;
while (!cin.fail() && squantity > 0)
{
if(squantity > pepsi)
{
cout << "Insufficient inventory to fill sell order, nothing changed." << endl;
}
}
cout << "Inventory updated...";
break;
case 3:
solddry = squantity;
totaldry = totaldry - squantity;
while (!cin.fail() && squantity > 0)
{
if(squantity > dry)
{
cout << "Insufficient inventory to fill sell order, nothing changed." << endl;
}
}
cout << "Inventory updated...";
break;
case 4:
soldhires = squantity;
totalhires = totalhires - squantity;
while (!cin.fail() && squantity > 0)
{
if(squantity > hires)
{
cout << "Insufficient inventory to fill sell order, nothing changed." << endl;
}
}
cout << "Inventory updated...";
break;
default:
cout << "ccThe action you requested is invalid...nothing has been done" << endl;
}
}
}//end function
void display_inventory()
{
cout << " Present inventory"<< endl;
cout << " BRAND ID CASES TOTAL PURCHASED SOLD " << endl;
cout << " Coke-- 1 -- " << coke << "\t" << totalcoke << "\t" << pcoke << "\t-" << soldcoke << endl;
cout << " Pepsi-- 2 -- " << pepsi << "\t" << totalpepsi << "\t" << ppepsi << "\t-" << soldpepsi << endl;
cout << "Canada Dry-- 3 -- " << dry << "\t" << totaldry << "\t" << pdry << "\t-" << solddry << endl;
cout << " Hires-- 4 -- " << hires << "\t" << totalhires << "\t" << phires << "\t-" << soldhires << endl;
}//end function
void quit()
{
cout << "\nPlease have a nice day!!!" << endl;
}//end function
-
For future reference, it is much more useful to say what kind of an error you are getting (infinite loop, seg fault) and where you are getting it rather than simply saying "I keep getting an error."
The while loop in the purchased() function is around the wrong block of code. Notice that since you never change the value of x, it will loop there forever. You need something like this instead:
Code:
//show options
do
{
cout << "\nPlease enter the brand you would like to purchase: ";
cin >> x;
} while (x >= 1 && x <= 4);
//do the rest
The same sort of logic error exists in your sell() function.
-
Code:
#include <iostream> // drop the .h
#include <cstdlib> // drop the .h
using namespace std; // This is because we dropped the .h
...
int main() // main always always always returns an int.