For this assignment, NO GLOBAL VARIABLES are allowed. You should write and call
ID: 3552341 • Letter: F
Question
For this assignment, NO GLOBAL VARIABLES are allowed. You should write and call
appropriate functions to perform the menu tasks.
Your program should follow reasonable rules of style: proper spacing, proper declaration and
naming of variables, proper indentation such that blocks of code should show correct indentation,
proper commenting on blocks of code, user friendly instructions as well as formatting and display
of outputs.
The purpose of this assignment is for you to demonstrate a working knowledge of function
declaration, function definition, making function calls, array declaration, and use of array
elements while continually synthesizing variable declarations, input and output, use of data types
and expressions, loops and control of logical flow of the program.
Explanation / Answer
#include <iostream>
using namespace std;
bool purchase(int quantity, int & inventory, double price, double & cost){
if(quantity>inventory){
cout<<"Quantity not in stock"<<endl;
return false;
}
else if(quantity<0){
cout<<"Quantity should be positive"<<endl;
return false;
}
else{
inventory-=quantity;
cost-= quantity*price;
} //return false;
}
bool sell(int quantity, int & inventory, double price, double & cost){
if(quantity<0){
cout<<"Quantity should be positive"<<endl;
return false;
}
else{
inventory+=quantity;
cost+= quantity*price;
} //return false;
}
void display(int id, int inventory, int price, int cost){
cout<<"id: "<<id<<" inventory: "<<inventory<<" price: "<<price<<" inventory cost: "<<cost<<endl;
}
void displayAll(int* id, int* inventory, double price[2][8]){
cout<<"id invent unitprice inventCost"<<endl;
for(int i=0;i<8;i++){
cout<<id[i]<<" "<<inventory[i]<<" "<<price[0][i]<<" "<<price[1][i]<<endl;
}
}
void displayMenu(){
cout<<"------------------------------ Press 1 for Purchase, 2 for Sell, 3 for displaying inventory, 4 for displaying all, 0 to quit"<<endl;
}
int main(){
int id[8] = {11,12,13,14,15,16,17,18};
int inventory[8] = {100,100,100,100,100,100,100,100};
double price[2][8] = {{5.5,8.0,10.5,13.0,15.5,18.0,20.5,23.0},{550,800,1050,1300,1500,1800,2050,2300}};
int response = 0;
do{
displayMenu();
cin>>response;
if(response==1){
displayAll(id,inventory,price);
int idt;
int quantity;
cout<<"enter id: ";
cin>>idt;
if(idt<11 || idt>18){
cout<<"Id should be 11 to 18 ";
continue;
}
cout<<"enter quantity: ";
cin>>quantity;
idt = idt-11;
if(purchase(quantity,inventory[idt],price[0][idt],price[1][idt])){
cout<<"Done"<<endl;
}
}
if(response==2){
displayAll(id,inventory,price);
int idt;
int quantity;
cout<<"enter id: ";
cin>>idt;
if(idt<11 || idt>18){
cout<<"Id should be 11 to 18 ";
continue;
}
cout<<"enter quantity: ";
cin>>quantity;
idt = idt-11;
if(sell(quantity,inventory[idt],price[0][idt],price[1][idt])){
cout<<"Done"<<endl;
}
}
if(response==3){
int idt;
cin>>idt;
if(idt<11 || idt>18){
cout<<"Id should be 11 to 18 ";
continue;
}
display(id[idt-11],inventory[idt],price[0][idt],price[1][idt]);
}
if(response==4){
displayAll(id,inventory,price);
}
}
while(response!=0);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.