Thread: help with program

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    help with program

    Can someone please help!
    heres the source code and what is required is this :
    make an inventory for four sodas (1though 4 as an id) and there must be an initial inventory gven before you start the programs which is menu driven
    the menu gives you the ability of Enter inventory (like going in and redoing the inventory) Purchasing soda(adding to initial amount given in the inventory)
    Selling soda (no explanation needed)
    Display inventory(here either)
    in it there are traps needed for
    atempting to sell more than in inventory,
    entering faulty letters for input(e,p,d,s, and q for quit are the only valid reads)
    entering any negative numbers.
    I think there is a problem with the way I set up the functions, because all I get is my error messages. If anyone could help me I would greatly appreciate it. Here is the code:

    Code:
    #include <iostream>
    using namespace std;
    
    void inventory(int& total_coke, int& total_pepsi, int&
    total_dry, int& total_hires);
    void sales_type(char& s_type, int& brand, int& quantity, int&
    total_coke, int& total_pepsi, int& total_dry, int& total_hires);
    void display_inventory(int& total_coke, int& total_pepsi, int&
    total_dry, int& total_hires);
    //void clearline();
    void instructions(char& s_type, int& brand, int& quantity,int&
    total_coke, int& total_pepsi, int& total_dry, int& total_hires);
    void purchase(int& brand,int& quantity, int& total_coke, int&
    total_pepsi, int& total_dry, int& total_hires);
    void sell(int& brand, int& quantity, int& total_coke, int&
    total_pepsi, int& total_dry, int& total_hires);
    void initialize();
    int get_total_coke(int& total_coke, int& coke_add);
    int get_total_pepsi(int& total_pepsi, int& pepsi_add);
    int get_total_dry(int& total_dry, int& dry_add);
    int get_total_hires(int& total_hires, int& hires_add);
    
    
    
    
    
    int main(){
    	
    	int  total_coke=0,
    		 total_pepsi=0,
    		 total_dry=0,
    		 total_hires=0,
    		 brand,
    		 quantity;	
    	char s_type;
    
    
    initialize();
    inventory(total_coke, total_pepsi, total_dry, total_hires);
    sales_type(s_type, brand, quantity, total_coke, total_pepsi,
    total_dry, total_hires);
    display_inventory(total_coke, total_pepsi, total_dry,
    total_hires);
    //clearline();
    return 0;
    }
    
    
    void inventory(int& total_coke, int& total_pepsi, int&
    total_dry, int& total_hires){
    int coke_add,
    	pepsi_add,
    	dry_add,
    	hires_add;
    
    	cout<<" Please enter the inventory for each item "<<endl
    		<<" Enter the number of Coke on hand ";	
    	
    	cin>> coke_add;
    while (cin.fail())
    	{	cin.clear();
    		cin.ignore(cin.fail());
    	 cout<<"Invalid input...please re-enter"<<endl;	
    	 cout<<" Enter the number of Coke on hand ";;		
    			cin>>coke_add;}
    	total_coke+=get_total_coke(total_coke, coke_add);
    
    				
    	cout<<" Enter the number of Pepsi on hand ";
    	cin>> pepsi_add;
    	
    	while (cin.fail())
    	{	cin.clear();
    		cin.ignore();
    	 cout<<"Invalid input...please re-enter"<<endl;	
    	 cout<<" Enter the number of Pepsi on hand "<<endl;		
    			cin>>pepsi_add;}
    	total_pepsi+=get_total_pepsi( total_pepsi, pepsi_add);
    
    
    	cout<<" Enter the number of Canada Dry on hand ";
    	cin>> dry_add;
    	
    	while (cin.fail())
    	{	cin.clear();
    		cin.ignore();
    	 cout<<"Invalid input...please re-enter"<<endl;	
    	 cout<<" Enter the number of Canada Dry on hand "<<endl;		
    			cin>>dry_add;}
    	total_dry+=get_total_dry(total_dry, dry_add);
    	
    	
    	cout<<" Enter the number of Hires on hand ";
    	cin>> hires_add;
    	
    	while (cin.fail())
    	{	cin.clear();
    		cin.ignore();
    	 cout<<"Invalid input...please re-enter"<<endl;	
    	 cout<<" Enter the number of Hires on hand "<<endl;		
    			cin>>hires_add;}
    	total_hires+=get_total_hires( total_hires, hires_add);
    //clearline();
    }
    
    void sales_type(char& s_type, int& brand, int& quantity,int&
    total_coke,int& total_pepsi,int& total_dry,int& total_hires){
    	cout<<" Enter the action and , if needed, company code and amount"; 
    	cin>>s_type>>brand>>quantity;
    
    	while (s_type!='Q'){
    	
    			switch(s_type){
    	
    	
    			case 'P':
    			purchase(brand,quantity, total_coke, total_pepsi, total_dry,
    total_hires);
    			break;
    			
    			case 'S':
    			sell(brand,quantity, total_coke, total_pepsi, total_dry,
    total_hires);
    			break;
    	
    			case 'D':
    			display_inventory(total_coke, total_pepsi, total_dry,
    total_hires);
    			break;
    	
    			case 'I':
    			instructions(s_type, brand, quantity,total_coke, total_pepsi, total_dry, total_hires);
    			break;
    	
    			default: cout<<"The action you requested is invalid...nothing has been done"<<endl;
    			}
    		}
    }
    
    
    
    void purchase(int& brand, int& quantity,int& total_coke, int&
    total_pepsi, int& total_dry, int& total_hires){
     
    	while (brand>=1 && brand<=4){
    			switch(brand){
    		
    			case'1':total_coke=total_coke+quantity;
    			cout<<"Inventory updated...";
    			break;
    			case'2':total_pepsi=total_pepsi+quantity;
    			cout<<"Inventory updated...";
    			break;
    			case'3':total_dry=total_dry+quantity;
    			cout<<"Inventory updated...";
    			break;
    			case'4':total_hires=total_hires+quantity;
    			cout<<"Inventory updated...";
    			break;
    			default: cout<<"bbThe action you requested is invalid...nothing has been done"<<endl;
    			}
    		   cin.clear();
    		   cin.ignore();
    		   cout<<"The action you requested is invalid...nothing has been done"<<endl;
    							}
    }
    
    
    void sell(int& brand, int& quantity,int& total_coke, int&
    total_pepsi, int& total_dry, int& total_hires){
    	while (brand>=1 && brand<=4){
    			 
    	switch(brand){
    	
    	case'1':
    		while (!cin.fail() && quantity>0)
    			if(quantity>total_coke)
    			{ cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    			else total_coke=total_coke-quantity;
    			     cout<<"Inventory updated...";
    			break;
    	case'2':	
    		while (!cin.fail() && quantity>0)
    			if(quantity>total_pepsi)
    			{ cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    			else total_pepsi=total_pepsi-quantity;
    			cout<<"Inventory updated...";
    			break;
    	case'3':	
    		while (!cin.fail() && quantity>0)
    			if(quantity>total_dry)
    			{ cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    			else total_dry=total_dry-quantity;
    			cout<<"Inventory updated...";
    			break;
    	case'4':	
    		while (!cin.fail() && quantity>0)
    			if(quantity>total_hires)
    			{ cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    			else total_hires=total_hires-quantity;
    			cout<<"Inventory updated...";
    			break;
    	
    	default: cout<<"ccThe action you requested is invalid...nothing has been done"<<endl;
    	}
    	}
    }
    
    
    
    void display_inventory(int& total_coke, int& total_pepsi, int&
    total_dry, int& total_hires){
    	cout<<"Present inventory:"<<endl;
    	cout<<"Coke--"<<total_coke<<endl;
    	cout<<"Pepsi--"<<total_pepsi<<endl;
    	cout<<"Canada Dry--"<<total_dry<<endl;
    	cout<<"Hires--"<<total_hires<<endl;
    }
    
    void instructions(char& s_type, int& brand, int& quantity, int&
    total_coke, int& total_pepsi, int& total_dry, int& total_hires){
    
    cout<<" Your commands should follow one of these formats:"<<endl
    	<<"  (P)urchase +<brand number> + <amount> "<<endl
    	<<"  (S)ell     +<brand number> + <amount> "<<endl
    	<<"  (D)isplay present inventory for the four brands"<<endl
    	<<"  Show (I)nstructions for user input "<<endl
    	<<"  (Q)uit"<<endl
    	<<" Brand number codes:"<<endl
    	<<"1 -- Coke  2 -- Pepsi  3 -- Canada Dry  4 -- Hires"<<endl;  
    //clearline();
    sales_type(s_type, brand, quantity, total_coke, total_pepsi,
    total_dry, total_hires);	
    	
    	 }
    	
    
    void initialize(){
    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<<" Your commands should follow one of these formats:"<<endl
    	<<"  (P)urchase +<brand number> + <amount> "<<endl
    	<<"  (S)ell     +<brand number> + <amount> "<<endl
    	<<"  (D)isplay present inventory for the four brands"<<endl
    	<<"  Show (I)nstructions for user input "<<endl
    	<<"  (Q)uit"<<endl
    	<<" Brand number codes:"<<endl
    	<<"1 -- Coke  2 -- Pepsi  3 -- Canada Dry  4 -- Hires"<<endl; 
    }
    int get_total_coke(int& total_coke, int& coke_add){
    while (coke_add<0){
    		cout<<" The action you have requested is invalid....nothing changed"<<endl;
    		cout<<" Please enter the amount of Coke on hand "<<endl;
    		cin.clear();
    		cin>>coke_add;}
     return coke_add;
    }
    
    int get_total_pepsi(int& total_pepsi, int& pepsi_add){
    while (pepsi_add<0){
    		cout<<" The action you have requested is invalid....nothing changed"<<endl;
    		cout<<" Please enter the amount of Pepsi on hand "<<endl;
    		cin.clear();
    		cin>>pepsi_add;}
     return pepsi_add;
    }
    
    int get_total_dry(int& total_dry, int& dry_add){
    while (dry_add<0){
    		cout<<" The action you have requested is invalid....nothing changed"<<endl;
    		cout<<" Please enter the amount of Canada Dry on hand "<<endl;
    		cin.clear();
    		cin>>dry_add;}
     return dry_add;
    }
    
    int get_total_hires(int& total_hires, int& hires_add){
    while (hires_add<0){
    		cout<<" The action you have requested is invalid....nothing changed"<<endl;
    		cout<<" Please enter the amount of Hires on hand "<<endl;
    		cin.clear();
    		cin>>hires_add;}
     return hires_add;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: help with program

    Two things:

    First, instead of this:
    Code:
    void sales_type(char& s_type, int& brand, int& quantity, int&
    total_coke, int& total_pepsi, int& total_dry, int& total_hires);
    // try this:
    void sales_type(char& s_type, int& brand, int& quantity, int&
                      total_coke, int& total_pepsi, int& total_dry, int& total_hires);
    Second, if you have errors, post the error messages.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    There are still some logical error. Fix it yourself...

    Code:
    void sales_type(char& s_type, int& brand, int& quantity,int&
    		total_coke,int& total_pepsi,int& total_dry,int& total_hires){
    
      s_type = ' ';
      while (s_type!='Q'){
    
      cout<<" Enter the action and , if needed, company code and amount "; 
      cin.clear();
      cin >> s_type >> brand >> quantity;
    
    	
        switch(s_type){
    	
    	
        case 'P':
        case 'p':
          purchase(brand,quantity, total_coke, total_pepsi, total_dry,
    	       total_hires);
          break;
    			
        case 'S':
        case 's':
          sell(brand,quantity, total_coke, total_pepsi, total_dry,
    	   total_hires);
          break;
    	
        case 'D':
        case 'd':
          display_inventory(total_coke, total_pepsi, total_dry,
    			total_hires);
          break;
    	
        case 'I':
        case 'i':
          instructions(s_type, brand, quantity,total_coke, total_pepsi, total_dry, total_hires);
          break;
    	
        default: cout<<"The action you requested is invalid...nothing has been done"<<endl;
        }
      }
    }
    
    
    
    void purchase(int& brand, int& quantity,int& total_coke, int&
    	      total_pepsi, int& total_dry, int& total_hires){
    
      switch(brand){
        
      case 1:total_coke=total_coke+quantity;
        cout<<"Inventory updated..." << endl;
        break;
      case 2:total_pepsi=total_pepsi+quantity;
        cout<<"Inventory updated..." << endl;
        break;
      case 3:total_dry=total_dry+quantity;
        cout<<"Inventory updated..." << endl;
        break;
      case 4:total_hires=total_hires+quantity;
        cout<<"Inventory updated..." << endl;
        break;
      default: cout<<"The action you requested is invalid...nothing has been done"<<endl;
      }
      cin.clear();
      cin.ignore();
      //    cout<<"The action you requested is invalid...nothing has been done"<<endl;
    }
    
    
    void sell(int& brand, int& quantity,int& total_coke, int&
    	  total_pepsi, int& total_dry, int& total_hires){
      if (brand>=1 && brand<=4){
    			 
        switch(brand){
    	
          case 1:
    	while (!cin.fail() && quantity>0)
    	  if(quantity>total_coke)
    	    { cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    	  else total_coke=total_coke-quantity;
    	cout<<"Inventory updated..." << endl;
          break;
          case 2:	
    	while (!cin.fail() && quantity>0)
    	  if(quantity>total_pepsi)
    	    { cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    	  else total_pepsi=total_pepsi-quantity;
    	cout<<"Inventory updated..." << endl;
          break;
          case 3:	
    	while (!cin.fail() && quantity>0)
    	  if(quantity>total_dry)
    	    { cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    	  else total_dry=total_dry-quantity;
    	cout<<"Inventory updated..." << endl;
          break;
          case 4:	
    	while (!cin.fail() && quantity>0)
    	  if(quantity>total_hires)
    	    { cout<< "Insufficient inventory to fill sell order,nothing changed"<<endl;}
    	  else total_hires=total_hires-quantity;
    	cout<<"Inventory updated..." << endl;
          break;
    	
        default: cout<<"ccThe action you requested is invalid...nothing has been done"<<endl;
        }
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM