Needing assist in my assignment.

// uses DEV-C++ 4.9.9.2 compiler //

#include <iostream>

using namespace std;

// struct for storing drinks variables //
struct drinkID
{
float price;
int invent ;
char name[15];
int inventNew;
float sales;

};
// declaring values of drinks //

drinkID C01 = { 1.50, 20, "Coke-Cola", 20};
drinkID P01 = { 1.50, 20, "Pepsi-Cola", 20};
drinkID S01 = { 1.20, 20, "Sprites", 20};
drinkID B01 = { 1.00, 20, "Soya-Bean", 20};
drinkID T01 = { 0.90, 20, "Green Tea", 20};
drinkID O01 = { 0.80, 20, "Orange", 20};

char choiceA; // for main menu selection //
int choiceB; // for drink selection //
char choiceC; // for confirmation of quit selection //
float payment; // for tabulation of sales //
float salesTotal; // for storing totalsales amount //

void mainMenu(); // main menu function //
void purchaseDrinks(); // purchasing function //
void displayInventory(); // displaying inventory function //
void displaySales(); // displaying sales function //
void quitMenu(); // confirmation of quitting function //
void sellItem();

int main(void)
{

mainMenu();


system("pause");
return 0;
}

void mainMenu()
{
cout << " Main Menu: " << endl;
cout << " a) Purchase Soft Drinks " << endl;
cout << " b) Display inventory summary " << endl;
cout << " c) Display sales summary " << endl;
cout << " q) Quit " << endl;
cin >> choiceA ;

// switching choices from main menu //

switch (choiceA)
{
case 'A':
case 'a':
purchaseDrinks();
break;
case 'B':
case 'b':
displayInventory();
break;
case 'C':
case 'c':
displaySales();
break;
case 'Q':
case 'q':
quitMenu();
break;
default:
cout << "Invalid selection." << endl;
}//end switch
mainMenu(); // back to mainMenu for invalid selection //
}

// purchasing of drinks //
void purchaseDrinks()
{
cout << " Please Chose an Option: " << endl;
cout << " 1. " << C01.name << endl;
cout << " 2. " << P01.name << endl;
cout << " 3. " << S01.name << endl;
cout << " 4. " << B01.name << endl;
cout << " 5. " << T01.name << endl;
cout << " 6. " << O01.name << endl;
cout << " 9. Quit " << endl;
cin >> choiceB;

// Switching in between selection of drinks //

switch (choiceB)
{
case 1:
void sellItem();
break;

case 2:
cout << P01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
P01.sales = P01.sales + payment;
static int count2 = P01.invent;
--count2;
P01.inventNew = count2 ;
cout << " Item dispensed, amount left: " << count2 << endl;
break;
}
case 3:
cout << S01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
S01.sales = S01.sales + payment;
static int count3 = S01.invent;
--count3;
S01.inventNew = count3 ;
cout << " Item dispensed, amount left: " << count3 << endl;
break;

case 4:
cout << B01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
B01.sales = B01.sales + payment;
static int count4 = B01.invent;
--count4;
B01.inventNew = count4 ;
cout << " Item dispensed, amount left: " << count4 << endl;
break;

case 5:
cout << T01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
T01.sales = T01.sales + payment;
static int count5 = T01.invent;
--count5;
T01.inventNew = count5 ;
cout << " Item dispensed, amount left: " << count5 << endl;
break;

case 6:
cout << O01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
O01.sales = O01.sales + payment;
static int count6 = O01.invent;
--count6;
O01.inventNew = count6 ;
cout << " Item dispensed, amount left: " << count6 << endl;
break;
case 9:
quitMenu(); // quit selection menu //

default:
cout << "Invalid selection." << endl;
}// end of switch //
mainMenu(); // back to main menu for further purchase //
}

void sellItem()
{
if (C01.invent > 0 )
{ cout << C01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
C01.sales = C01.sales + payment; // calculation of sales total for item //
static int count1 = C01.invent; // keep count static in database //
--count1;
C01.inventNew = count1 ;
cout << " Item dispensed, amount left: " << count1 << endl;
}

else
cout << " Item is sold out. " << endl;
}

// displaying inventory of drinks //
void displayInventory()
{
cout << "No of " << C01.name << " left is: " << C01.inventNew <<endl;
cout << "No of " << P01.name << " left is: " << P01.inventNew <<endl;
cout << "No of " << S01.name << " left is: " << S01.inventNew <<endl;
cout << "No of " << B01.name << " left is: " << B01.inventNew <<endl;
cout << "No of " << T01.name << " left is: " << T01.inventNew <<endl;
cout << "No of " << O01.name << " left is: " << O01.inventNew <<endl;

mainMenu();
}

// displaying total sales and individual sales //

void displaySales()
{
salesTotal = C01.sales + P01.sales + S01.sales + B01.sales + T01.sales + O01.sales;
cout << "Total sales for" << C01.name << "is: " << C01.sales << endl;
cout << "Total sales for" << P01.name << "is: " << P01.sales << endl;
cout << "Total sales for" << S01.name << "is: " << S01.sales << endl;
cout << "Total sales for" << B01.name << "is: " << B01.sales << endl;
cout << "Total sales for" << T01.name << "is: " << T01.sales << endl;
cout << "Total sales for" << O01.name << "is: " << O01.sales << endl;
cout << "Total sales for all items is : " << salesTotal << endl;
mainMenu(); // back to mainMenu for further input //

}

// for confirmation of quit selection //
void quitMenu()

{
cout << " Do you want to quit? (Y/N)" << endl;
cin >> choiceC;

switch (choiceC)
{
case 'Y':
case 'y':
break;

case 'N':
case 'n':
mainMenu();
}// end of switch //
}


This is the coding i have done. Now i have 2 difficulties that i need to solve.

1st: How am i going to make the program display Inventory empty if the count for the item is 0?

2nd: For the choices, example choiceB it is of int but if the user input char it will run non-stop. How do I solve this problem?

Please any help will be great and I know my coding is newbie style. >.<
Last edited on
The first issue, just use an if statement to check if your inventory is empty, and print that if it is, and use else for other code...
For the second issue read this thread: http://www.cplusplus.com/forum/articles/6046/
Last edited on
case 1:
void sellItem();
break;

void sellItem()
{
if (C01.invent > 0 )
{ cout << C01.price << endl;
cout << " Please pay in exact amount( no change will be given ): " << endl;
cin >> payment;
C01.sales = C01.sales + payment; // calculation of sales total for item //
static int count1 = C01.invent; // keep count static in database //
--count1;
C01.inventNew = count1 ;
cout << " Item dispensed, amount left: " << count1 << endl;
}

else
cout << " Item is sold out. " << endl;
}

this is wad i did. Is this if else staement wrong in any place?

The only things that I see is that CO1.invent is never updated... you subtract 1 from a different number and change CO1.inventNew. This means when multiples of this item are purchased throughout the program the inventory number isn't actually going down... I might have missed you updating invent somenwhere else in the program though.

Also, why :
static int count1 = C01.invent;
--count1;
C01.inventNew = count1 ;
instead of just
C01.inventNew = C01.invent - 1;
Last edited on
I thought by using static the count will remain through out the whole program? Meaning after purchase and he go to view inventory and then make another purchase again the count will still be stored isn't it?
that is correct. static remains but in your case I don't see why you use it, why not just keep it all in the struct?

i think you should compact your code a bit, you must also see that the five cases look very similar, instead of having five cases do one function that takes the P01,C01,... as argument. Also instead of treating them individually do a vector of your structs std::vector<drinkID> mydrinks, that makes printing and calculating the sum much more compact.

Topic archived. No new replies allowed.