• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need help with nested swtich in programming project

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

amin

New Member
Joined
Oct 19, 2003
Hi i was wondering if sombody could help me code nested switch statments. I'am trying to write an inventory program that keeps track of the quantity of 5 brands of soda and lets the user sell or purchase the soda and updated the quantity.
the pseudocode for this is as follows:
while(action gotten is not Quit)
{
If (action indicates a sale)
make amount a negative value
switch (action)
{
carry out sale or purchase ...or
carry out inventory display... // displays inventory when user types in "d"
carry out instruction display.. // displays instructions when user types in "I"
carry out invalid display message //default label

And within the aforementioned switch statment we have to include a update function that updates the inventory to the proper brand of soda.
i.e.:
void update(int &inventory, int amount)
so how do i do this in a nestesd switch statement?
this is part of what i have so far(i can post the rest of it if this looks too vague):
Code:
while (cin>>ch>>Brand_number>>amount,ch!='Q')
	{
      if (ch==isupper('S'))
	  {
      amount= amount * (-1);
	  }
      if(ch==isupper('S'||'P'||'D'||'I'||'Q'));
	  switch (ch)
      {
		  case 'S'|| 'P':
		  
sell_purchase(amount,quantity_c,quantity_p,quantity_d,quantity_h,Brand_number);
		  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 changed "<< endl;
	  }
	  cout<<"Closing inventory: " << endl;
	  inven_display(quantity_c,quantity_p,quantity_d,quantity_h);
	}
      return 0;
}
void inven_display(
			       int& quantity_c,
			       int& quantity_p,
			       int& quantity_d,
			       int& quantity_h
				   )
{
	cout<<"Coke       -- "<<quantity_c<<endl;
    cout<<"Pepsi      -- "<<quantity_p<<endl;
	cout<<"Canada Dry -- "<<quantity_d<<endl;
    cout<<"Hires      -- "<<quantity_h<<endl;
}

sell_purchase(int& amount,
			       int& quantity_c,
                   int& quantity_p,
			       int& quantity_d,
			       int& quantity_h,
			       int Brand_number)
{
   for(Brand_number=1;Brand_number<5;)
   {
       if(Brand_number==1){
		   if(amount<=quantity_c)
             quantity_c=amount+quantity_c;
		   else
			 cout<<"insuficient inventory to fill sell order,nothing changed "<<endl;
	   	   }
	   if(Brand_number==2){
           if(amount<=quantity_p)
            quantity_p=amount+quantity_p;
		   else
			 cout<<"insuficient inventory to fill sell order,nothing changed "<<endl;
	   }
       if(Brand_number==3){
           if(amount<=quantity_d)
            quantity_d=amount+quantity_d;
		   else
             cout<<"insuficient inventory to fill sell order,nothing changed "<<endl;
	   }
	   if(Brand_number==4){
	       if(amount<=quantity_c)
			  quantity_p=amount+quantity_p;
		   else
			 cout<<"insuficient inventory to fill sell order,nothing changed "<<endl;
	   }
	    cout<<"Inventory updated...."<<endl;
	    Brand_number++;
		return 0;
   }
  }
 
What, exactly, are you having problems with? Do you need to know how to implement an update function, or where to put the calls to an update function? I would probably call update from within the sell or purchase routine, but you can have as many lines of code as you want after a case condition and its corresponding break statement.

I just don't quite understand what you are having trouble doing...
 
oh iam sorry, iam just confused about how to implement the update function within the switch.
 
Back