loop, cases

loop, cases

Page 1 of 1

2 Replies - 625 Views - Last Post: 27 July 2009 - 02:40 PM Rate Topic: -----

#1 j108jason  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-June 09

loop, cases

Posted 27 July 2009 - 01:53 PM

hello
plz help me
my program has would not complie because of the "illegl case" there are3 of them im not sure why
oh and the loop is all messed up they link together sometimes
thank you
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
#include<iomanip.h>
main()
{   srand((unsigned)time(NULL));
	 char name[20], shipname, city;
	int shopping_options, market_options, rice, spice, wine, quit,market_item, ricecount, money, winecount, spicecount;
	int pearl, jewel, silk, silkcount, jewelcount, pearlcount, cargo_space, ship_health, cannon_count, shipyard_option;
	int travel_place;
	cout<<"Hello Captain. What is your first name as to properly address you?";
	cin.get(name,20);
	cin.ignore(80,'\n');
	money=1000;
	travel_place=1;
	ship_health=300;
	cargo_space=200;
	cannon_count=10;
silkcount= jewelcount= pearlcount=winecount= spicecount=ricecount=0;
do
	{	rice=rand()%10+1;
	spice=rand()%125+1;
	wine=rand()%75+1;
	pearl=rand()%250+1;
	jewel=rand()%575+1;
	silk=rand()%1250+1;

	if (spice>=75)
	{spice=spice+75;}	
	if (wine>=25)
	{wine=wine+25;}
	if (pearl>=150)
	{pearl=pearl+150;}
	if (jewel>=425)
	{jewel=jewel+425;}
	if (silk>=750)
	{silk=silk+750;}

	cout<<"Hello Captain "<<name<<", what would you like to do?"<<endl;
	cout<<"Please enter the choose number you want."<<endl;
	cout<<" 1-merchant market "<<endl;
	cout<<" 2-ship yard "<<endl;
	cout<<" 3-inventory menu "<<endl;
	cout<<" 4-travel to other areas "<<endl;
	cin>>shopping_options;

	switch(shopping_options)
	{
	case 1:
	cout<<"You have entered the merchant's market!"<<endl;
	cout<<"Would you like to buy or sell?"<<endl;
	cout<<"1-buy, 2-sell, 3-leave market"<<endl;
	cin>>market_options;
	switch(market_options)
	{case 1:
	cout<<"What would you like to buy?"<<endl;
	cout.setf(ios::fixed);
	cout.setf(ios::left);
	cout << setw(20)<<"1-Rice" << setw(20)<< "2-Spice" << setw(20)<< "3-wine" << endl; 
	cout << "$" << setw(20)<< setprecision(2)<<rice<< "$"<< setw(20)<< setprecision(2) <<spice<< "$"<< setw(20)<< setprecision(2)<< wine <<'\n'<< endl;
	 cout << setw(20)<<"4-Pearl" << setw(20)<< "5-Jewel" << setw(20)<< "6-Silk" << endl; 
	cout << "$" << setw(20)<< setprecision(2)<<pearl<< "$"<< setw(20)<< setprecision(2) <<jewel<< "$"<< setw(20)<< setprecision(2)<< silk <<'\n'<< endl;
	cout <<"Enter the number choose of the item you want";
	cin >>market_item;
	if (market_item==1)
	{cout<<"How many rice would you like to purchase?";
	cin>>ricecount;
	cout<<"You have purchased: "<<ricecount<<" rice"<<endl;
	money=money-rice*ricecount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}
	if (market_item==2)
	{cout<<"How many spice would you like to purchase?";
	cin>>spicecount;
	cout<<"You have purchased: "<<spicecount<<" spice"<<endl;
	money=money-spice*spicecount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}
if (market_item==3)
	{cout<<"How many wine would you like to purchase?";
	cin>>winecount;
	cout<<"You have purchased: "<<winecount<<" wine"<<endl;
	money=money-wine*winecount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}
if (market_item==4)
	{cout<<"How many pearl would you like to purchase?";
	cin>>pearlcount;
	cout<<"You have purchased: "<<pearlcount<<" pearl"<<endl;
	money=money-pearl*pearlcount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}
if (market_item==5)
	{cout<<"How many jewel would you like to purchase?";
	cin>>jewelcount;
	cout<<"You have purchased: "<<jewelcount<<" jewel"<<endl;
	money=money-pearl*pearlcount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}
if (market_item==6)
	{cout<<"How many silk would you like to purchase?";
	cin>>silkcount;
	cout<<"You have purchased: "<<silkcount<<" silk"<<endl;
	money=money-silk*silkcount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}break;
	case 2:
	cout<<"Hello captain, what would you like to sell?"<<endl;
	cout.setf(ios::fixed);
	cout.setf(ios::left);
	cout << setw(20)<<"1-Rice" << setw(20)<< "2-Spice" << setw(20)<< "3-wine" << endl; 
	cout << "$" << setw(20)<< setprecision(2)<<rice<< "$"<< setw(20)<< setprecision(2) <<spice<< "$"<< setw(20)<< setprecision(2)<< wine <<'\n'<< endl;
	 cout << setw(20)<<"4-Pearl" << setw(20)<< "5-Jewel" << setw(20)<< "6-Silk" << endl; 
	cout << "$" << setw(20)<< setprecision(2)<<pearl<< "$"<< setw(20)<< setprecision(2) <<jewel<< "$"<< setw(20)<< setprecision(2)<< silk <<'\n'<< endl;
	cout <<"Enter the item option you want to sell";
	cin>>market_item;
	if (market_item==1)
	{cout<<"How many rice would you like to sell?";
	cin>>ricecount;
	cout<<"You have sold: "<<ricecount<<" rice"<<"at the price of: "<<rice<<endl;
	money=money+rice*ricecount;
	cout<<"You now have: "<< "$" <<money<<" left"<<endl;
	}
	if (market_item==2)
	{cout<<"How many spice would you like to sell?";
	cin>>spicecount;
	cout<<"You have sold: "<<spicecount<<" spice"<<"at the price of: "<<spice<<endl;
	money=money+spice*spicecount;
	cout<<"You now have: "<< "$" <<money<<endl;
	}
if (market_item==3)
	{cout<<"How many wine would you like to sell?";
	cin>>winecount;
	cout<<"You have sold: "<<winecount<<" wine"<<"at the price of: "<<wine<<endl;
	money=money+wine*winecount;
	cout<<"You now have: "<< "$" <<money<<endl;
	}
if (market_item==4)
	{cout<<"How many pearl would you like to sell?";
	cin>>pearlcount;
	cout<<"You have sold: "<<pearlcount<<" pearl"<<"at the price of: "<<pearl<<endl;
	money=money+pearl*pearlcount;
	cout<<"You now have: "<< "$" <<money<<endl;
	}
if (market_item==5)
	{cout<<"How many jewel would you like to sell?";
	cin>>jewelcount;
	cout<<"You have sold: "<<jewelcount<<" jewel"<<"at the price of: "<<jewel<<endl;
	money=money+pearl*pearlcount;
	cout<<"You now have: "<< "$" <<money<<endl;
	}
if (market_item==6)
	{cout<<"How many silk would you like to sell?";
	cin>>silkcount;
	cout<<"You have purchased: "<<silkcount<<" silk"<<"at the price of:"<<silk<<endl;
	money=money+silk*silkcount;
	cout<<"You now have: "<< "$" <<money<<endl;
}break;}}
	case 2:
	cout<<"Welcome to the shipyard captain "<<name<<" what would you like to do?"<<endl;
	cout<<"1-buy ship"<<endl;
	cout<<"2-buy cannon"<<endl;
	cout<<"3-sell cannon"<<endl;
	cin>>shipyard_option;
	switch (shipyard_option)
	{case 1:
	cout.setf(ios::fixed);
	cout.setf(ios::left);
	cout<<"What magificant ship would you like to purchase for your domaination of the world?"<<endl;
	cout << setw(30)<<"Dragon ship" << setw(30)<< "Legenday ship of peace" << endl; 
	cout << "$" << setw(30)<< setprecision(3)<<50000  << "$" << setw(30)<< setprecision(3) <<150000<<'\n'<< endl;
	cout << setw(30)<<"Armada" << setw(30)<< "Stolen ship of the pirate king" << endl; 
	cout << "$" << setw(30)<< setprecision(3)<<1000000<< "$" << setw(30)<< setprecision(3) <<3333333 <<'\n'<< endl;	
	break;

	case 2:
	cout.setf(ios::fixed);
	cout.setf(ios::left);
	cout<<"What magificant cannon would you like to purchase for your domaination of the world?"<<endl;
	cout << setw(20)<<"cannon" << setw(20)<< "thunder spear" << endl; 
	cout << "$" << setw(30)<< setprecision(3)<<250  << "$" << setw(30)<< setprecision(3) <<10000<<'\n'<< endl;
	cout << setw(30)<<"Gigantic Jet cannon" << setw(30)<< "Super bomb cannon" << endl; 
	cout << "$" << setw(30)<< setprecision(3)<<100000<< "$" << setw(30)<< setprecision(3) <<1000 <<'\n'<< endl;	
	break;

		

	case 3:
		break;
	break;}

	case 3:
	if (travel_place==1){cout<<"Beijing"<<endl;} if (travel_place==2){cout<<"Hong Kong"<<endl;} if (travel_place==3){cout<<"Saigon"<<endl;} if (travel_place==4){cout<<"Shanghai"<<endl;}if (travel_place==5){cout<<"Delhi"<<endl;}if (travel_place==6){cout<<"Manila"<<endl;}	if (travel_place==7){cout<<"Bangkok";}if (travel_place==8){cout<<"Taiwian";}
		cout<<"Welcome to your inventory"<<endl;
	cout.setf(ios::fixed);
	cout.setf(ios::left);
	cout<<"Personal information"<<endl;
	cout << setw(20)<<"Money" << setw(20)<< "Ship name" << setw(20)<< "City" << endl; 
	cout << "$" << setw(20)<< setprecision(2)<<money<< setw(20)<< setprecision(2) <<shipname<< setw(20)<< setprecision(2)<<  travel_place << endl; 
	
	cout << setw(20)<<"Ship health" << setw(20)<< "Cannon count" << setw(20)<< "Cargo Space" << endl; 
	cout << setw(20)<< setprecision(2)<<ship_health<< setw(20)<< setprecision(2) <<cannon_count<< setw(20)<< setprecision(2)<< cargo_space <<'\n'<< endl;	
	cout<<"Item information"<<endl;
	cout << setw(20)<<"Rice" << setw(20)<< "Spice" << setw(20)<< "wine" << endl; 
	cout << setw(20)<< setprecision(2)<<ricecount<< setw(20)<< setprecision(2) <<spicecount<< setw(20)<< setprecision(2)<< winecount <<'\n'<< endl;
	cout << setw(20)<<"Pearl" << setw(20)<< "Jewel" << setw(20)<< "Silk" << endl; 
	cout << setw(20)<< setprecision(2)<<pearlcount<< setw(20)<< setprecision(2) <<jewelcount<< setw(20)<< setprecision(2)<< silkcount <<'\n'<< endl;	
		
			break;
	case 4:
		cout<<"Where would you like to travel to?"<<endl;	  
		cout<<"1-Beijing, 2-Hong Kong, 3-Saigon, 4-Shanghai"<<endl;
		cout<<"5-Delhi, 6-Manila, 7-Bangkok, 8-Taiwian"<<endl;
		cin>>travel_place;
		
		if (travel_place=travel_place)
		{switch(travel_place)
		{
		case 1:
		cout<<"Congratulations on your arrival to Beijing"<<endl;

		break;
		case 2:
		cout<<"Congratulations on your arrival to Hong Kong"<<endl;

		break;
		case 3:
		cout<<"Congratulations on your arrival to Saigon"<<endl;

		break;
		case 4:
		cout<<"Congratulations on your arrival to Shanghai"<<endl;
	  
		break;
		case 5:
		cout<<"Congratulations on your arrival to Delhi"<<endl;
		
		break;
		case 6:
		cout<<"Congratulations on your arrival to Manila"<<endl;
		
		break;
		case 7:
		cout<<"Congratulations on your arrival to Bankok"<<endl;

		break;
		case 8:
		cout<<"Congratulations on your arrival to Taiwian"<<endl;
	   
		break;

}}}}}

while (1);
		return 0;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: loop, cases

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: loop, cases

Posted 27 July 2009 - 02:09 PM

You have too many }'s

I commented out two of them and it will compile for me.
One here (with the code around it included so you can see where it is:
if (market_item==6)
	{cout<<"How many silk would you like to sell?";
	cin>>silkcount;
	cout<<"You have purchased: "<<silkcount<<" silk"<<"at the price of:"<<silk<<endl;
	money=money+silk*silkcount;
	cout<<"You now have: "<< "$" <<money<<endl;
}break;}//}
	case 2:
	cout<<"Welcome to the shipyard captain "<<name<<" what would you like to do?"<<endl;


That was causing you to exit the switch when you didn't want to.

and the one at the bottom:
}}}}//}

while (1);
		return 0;
	}


That's one good reason to indent your code properly. It is not only easier to read, but it's easier to keep track of which { goes with which }.
Was This Post Helpful? 1

#3 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: loop, cases

Posted 27 July 2009 - 02:40 PM

Just glancing at this, so I probably didnt catch anywhere near what all is wrong with it.

First thing I noticed was nested switch cases. Dont go there. Way too complicated to try to debug. There is really no reason to ever do nested switch cases. Call a function with the other switch case inside it at the very least.

I also noticed that you have many places in your code that repeat.. this means that if one part of that code needs to change.. you will need to change it in every different place it occurs. This is bad coding. Try to encapsulate the code into one function you can call from all of those places.. I realize its not easy to imagine so I will try to give an example.


if (market_item==2)
 money = makePurchase("rice", ricePrice, money);



then everything else is handled inside makePurchase



float makePurchase( string item, float cost, float money)
{
   int amount;
   float result;
   float total;

   cout << "How many " << item << " would you like to buy?" <<endl;
   cin >> amount;
   total = (amount * cost);
   result = money - total;
   // validate if there is enough money?  can be done here..
		if(result < 0)
		{ 
		   // tell the player he cant purchase that.. not enough money!
		   //return the money.. 
		   result = money;
		}

return result;
}









good luck! It looks like the beginning of a fun game.

Aet
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1