![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
help with correcting a problem
I keep getting a error message in my purchase() and sell() i can input what i need but it dont keep going until i exit the program like the others do
#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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
both your purchase and sell functions will go into infinte loops... once in the while loop 'x' is never changed. you need to prompt the user for another choice within the while loop.
EDIT: you could just use a do while instead of repeating the Purchase menu code 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;
}
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;
}
}//end function |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|