Thread: stack question
View Single Post
Old May 8th, 2006, 6:52 AM   #4
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
You can make your driver shorter by doing something like:
while (1)//begin main while
	{
		int mainMenuChoice;  //variable for stack type

		cout<<"which stack would you like to create?"<<endl;
		cout<<"1:   int\t2:   long\n3:   float\t4:   char"<<endl;
		cout<<"5:   quit"<<endl;
		cout<<"(choose 1-5)\n\n"<<endl;

		cin>>mainMenuChoice;		
		switch(mainMenuChoice)
		{
		case 1:
			Stack<int, MaxStack>* stackClassDriver = new Stack<int, MaxStack>;
			int stackItem;
			break;
		case 2:
			Stack<long, MaxStack>* stackClassDriver = new Stack<long, MaxStack>;
			long stackItem;
			break;
		case 3:
			Stack<float, MaxStack>* stackClassDriver = new Stack<float, MaxStack>;
			float stackItem;
			break;
		case 4:
			Stack<char, MaxStack>* stackClassDriver = new Stack<char, MaxStack>;
			char stackItem;
			break;
		case 5:
			return 0;
		default:
			cout<<"invalid selection...\n\n"<<endl;
			continue;
		}
	 done=false;
	 while (!done)
	 {
		int stackOperation;

		cout<<"1:\tpush\t2:\tpop"<<endl;
		cout<<"3:\tprint\t4:\tmain menu"<<endl;
		cout<<"(choose 1-4)\n\n"<<endl;
		cin>>stackOperation;

		switch(stackOperation)
		{
		case 1:
			cout << "Enter stackItem to push: ";
			cin >> stackItem;
			stackClassDriver->push(stackItem);
			break;
		case 2:
			stackClassDriver->pop();
			break;
		case 3:
			stackClassDriver->print();
			break;

		case 4:
			delete stackClassDriver;
			stackClassDriver = 0;
			done=true;
			break;
		default:
			cout<<"invalid selection...\n\n"<<endl;
			continue;
		}
	}//end while
}
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote