Here's my program. It compiles, runs but there's a bug in it.
It has to do with the function get_action; the problem is in the OR statements :
Brand_number!=('1'))||(Brand_number!=('2'))||(Brand_number!=('3'))||(Brand_number!=('4')))
It just doesn't work. The brand number has to equal 1,2,3, or 4, but whenever i try the above operator it only displays the error messages, even when there is a valid number.
Code:
/*This program prompts the user for inventory values for four different sodas then it prompts the user for an action,a soda brand name and an amount.It then carries out the action requested.Main is divided into three functions namely instr_display,initialize and process_orders*/
#include <iostream>
#include <string>
using namespace std;
void initialize(int&,int&,int&,int&);
void process_orders(int& quantity_c,int& quantity_p,int& quantity_d,int& quantity_h);
void sell_purchase(int,int&,char);
void inven_display(int quantity_c,int quantity_p,int quantity_d,int quantity_h);
void instr_display();
void clear_line();
int get_amount();
char get_action(char& Brand_number,int& amount);
bool valid_read(string Brand_name,int& quantity);
const int NEGATIVE_VALUE = -1;
const char INVALID= 'X';
int main()
{
int coke=0; // quantity of Coke entered by user for inventory
int pepsi=0; // quantity of Pepsi entered by user for inventory
int canada_dry=0; // quantity of Canada Dry entered by user for inventory
int hires=0; // quantity of hires entered by user for inventory
instr_display();
initialize(coke,pepsi,canada_dry,hires);
process_orders(coke,pepsi,canada_dry,hires);
clear_line();
return 0;
}
/*displays the instruction at the begining of the program and whenever user enter 'I'*/
void instr_display( )
{
cout <<"You will first be prompted for the inventories for each of the four brands."<<endl;
cout <<"You should then enter the initial number of cases available for each brand."<<endl;
cout <<"All other commands should follow one of these formats:"<< endl;
cout << " "<<"(P)urchase + <brand number> + <amount>."<< endl;
cout << " "<<"(S)ell + <brand number> + <amount>."<< endl;
cout << " "<<"(D)isplay present inventory for the four brands."<< endl;
cout << " "<<"Show(I)structions for user input."<< endl;
cout << " "<<"(Q)uit."<< endl;
cout <<"Brand Number codes:"<< endl;
cout <<" "<<"1--Coke 2--Pepsi 3--Canada Dry 4-- Hires "<< endl;
}
/*this function initializes the inventory with different quantities of soda*/
void initialize(int& quantity_c,int& quantity_p,int& quantity_d ,int& quantity_h)
{
while (!(valid_read("Coke",quantity_c)))
{
cin.clear();
cin.ignore (80,'\n');
cout<<" "<<"Invalid input ...... please re-enter. "<<endl;
}
while (!(valid_read("Pepsi",quantity_p)))
{
cin.clear();
cin.ignore (80,'\n');
cout<<" "<<"Invalid input ...... please re-enter. "<<endl;
}
while (!(valid_read("Canada Dry ",quantity_d)))
{
cin.clear();
cin.ignore (80,'\n');
cout<<" "<<"Invalid input ...... please re-enter. "<<endl;
}
while (!(valid_read("hires",quantity_h)))
{
cin.clear();
cin.ignore (80,'\n');
cout<<" "<<"Invalid input ...... please re-enter. "<<endl;
}
}
/* This function uses the action type entered by the user to choose if the user wants to purchase,sell display inventory ,instructions or quit*/
void process_orders(
int& quantity_c, // refernce for c
int& quantity_p, //refernce for p
int& quantity_d, //refernce for d
int& quantity_h //refernce for h
)
{
char ch; //holds the action enterd by user
char Brand_number; //holds the company code entered by the user
int amount; //holds the amount of soda entered
// int quantity; //holds quantity of soda in inventory
while((ch=get_action(Brand_number,amount))!='Q')
{
switch (ch)
{
case 'S':
case 'P':switch (Brand_number)
{
case '1':
sell_purchase(amount,quantity_c,ch);
break;
case '2':
sell_purchase(amount,quantity_p,ch);
break;
case '3':
sell_purchase(amount,quantity_d,ch);
break;
case '4':
sell_purchase(amount,quantity_h,ch);
break;
default:
cout<<" "<<"The action you requested is invalid..nothing has been doneD"<<endl;
}
break;
case 'D':
cout<<"Present Inventory:"<<endl;
inven_display(quantity_c,quantity_p,quantity_d,quantity_h);
break;
case 'I':
instr_display();
break;
default:
cout<<" "<<"The action you requested is invalid..nothing has been doneC"<<endl;
}
}
cout<<"Closing Inventory:"<<endl;
inven_display(quantity_c,quantity_p,quantity_d,quantity_h);
}
/*This function displays the inventory*/
void inven_display(int quantity_c,
int quantity_p,
int quantity_d,
int quantity_h
)
{
cout<<" "<<"Coke -- "<<quantity_c<<" "<<"cases"<<endl;
cout<<" "<<"Pepsi -- "<<quantity_p<<" "<<"cases"<<endl;
cout<<" "<<"Canada Dry -- "<<quantity_d<<" "<<"cases"<<endl;
cout<<" "<<"Hires -- "<<quantity_h<<" "<<"cases"<<endl;
}
/* function computes the sell and purchase order*/
void sell_purchase( int amount,int& quantity,char ch)
{
if(ch=='S')
{
if(amount>quantity){
cout<<" "<<"Insufficient inventory to fill sell order,nothing changed"<<endl;
}
else
{
quantity=quantity - amount;
cout<<" "<<"Inventory updated.."<<endl;
}
}
else
{
quantity= amount + quantity;
cout<<" "<<"Inventory updated.."<<endl;
}
}
/* function extracts the action,company code and amount from keyboard*/
char get_action(char& Brand_number,int& amount)
{
char ch;
cout<<"Enter action, and, if needed, company code and amount: ";
cin>>ch;
cout<<ch<<endl;
if(ch=='P' ||ch =='S')
{
if(!(cin>>Brand_number))||(Brand_number!=('1'))||(Brand_number!=('2'))||(Brand_number!=('3'))||(Brand_number!=('4')))
{
cin.clear();
cin.ignore();
cout<<" "<<"The action you requested is invalid..nothing has been doneA."<<endl;
//cout<<"Enter action, and, if needed, company code and amount: ";
return INVALID;
//cin>>ch;
}
else
if(!((amount=get_amount())>NEGATIVE_VALUE))
{
cin.clear();
cin.ignore();
cout<<" "<<"The action you requested is invalid..nothing has been doneB."<<endl;
// cout<<"Enter action, and, if needed, company code and amount: ";
return INVALID;
}
}else
clear_line();
return ch;
}
/*error trap for amount*/
int get_amount()
{
int amount;
if (!(cin>>amount)){
cin.clear();
cin.ignore();
return NEGATIVE_VALUE;
}
return amount;
}
void clear_line()
{
cin.ignore(80,'\n');
}
bool valid_read(string Brand_name,int& quantity)
{
cout<<"Enter number of cases of"<<" "<<Brand_name<<" "<<"on hand: ";
quantity =get_amount();
return quantity> NEGATIVE_VALUE;
}
Thanks in Advance