Skipping to end of program not using goto

December 13, 2009 at 06:43:19
Specs: Windows 7
Is there an easier way of if 'x' is pressed the
program skips from the loop to the end of the
code rather than a goto?

#include <iostream.h>
#include <process.h>

void main()
{
	char chHappy = 'Y';
	char chUse = 'Y';
	int iNumber = 0;
	int iUse = 0;
	int iHappy = 0;
	float fPercent = 0.0;
	int i = 0;
	char chAnswer = 'N';
	int iMenu = 0;
	int iSand = 0;
	int iSalad = 0;
	int iSnack = 0;
	int iMeal = 0;
	int iBaked = 0;

	for(i=0;i<6;i++)																			
//DO WHILE loop
	{																			
//LOOP
			
			cout << "Do you use the 
student refectory regularly? :>";					
//DISPLAY
			cin  >>	chUse;														
//INPUT iNumber


		if(chUse != 'Y' && chUse != 'y' && 
chUse != 'N' && chUse != 'n' && chUse != 'X' 
&& chUse != 'x')										
//Validation
		{
			cout << "Unrecognised input - 
please re-enter." << endl;
		}

		if (chUse =='X' || chUse =='x')
		{
			goto freedom;
		}

		if (chUse =='Y' || chUse =='y')
		{

			cout << endl; 
			cout << "Are you happy with 
the service provided? :>";			
//OUTPUT
			cin >> chHappy;
			iUse++;	

				cout << endl;
				cout << "Which product 
did you purchase?" << endl;

				cout << "1. a snadwich" 
<< endl;												
// DISPLAY option 1
				cout << "2. a salad" << 
endl;												
// DISPLAY option 2
				cout << "3. a baked 
potato" << endl;												
// DISPLAY option 3
				cout << "4. a hot snack" 
<< endl;												
// DISPLAY option 4
				cout << "5. a full meal" 
<< endl;

				cout <<endl << "Please 
choose one of the options above [valid entries 
1 - 5]: ";	// PROMPT user for valid entry
				cin >> iMenu;																
// INPUT into iMenu
			
				switch(iMenu)																
// CASE iMenu
				{
				case 1: cout << "You 
selected option 1."<<endl; iSand++;									
// 1 OUTPUT you selected option 1
					break;		
				case 2: cout << "You 
selected option 2."<<endl;	iSalad++;								
// 2 OUTPUT you selected option 2
					break;
				case 3: cout << "You 
selected option 3."<<endl;	iBaked++;								
// 3 OUTPUT you selected option 3
					break;
				case 4: cout << "You 
selected option 4."<<endl;	iSnack++;								
// 4 OUTPUT you selected option 4
					break;
				case 5: cout << "You 
selected option 5."<<endl; iMeal++;
					break;
				default: cout << "Invalid 
input expected values from 1 - 5."<<endl;		
// OTHERWISE OUTPUT "Invalid input 
expected values from 1 - 5"
			}																			
// END CASE


		if (chHappy =='Y' || chHappy =='y')
		{
			iHappy++;
		}

				
	/*
		else																	
//ELSE
		{
			cout << "Thank you for taking 
this survay" << endl;
			+i;
		}																		
//END IF
	*/
		}

	}
	freedom:
		cout << "Out of " << i << " values 
entered "					// Output of 
results
			 << iUse;
		(iUse>1)? cout << " were": cout << 
" was";
		cout << " use the student refectory 
regularly and " << endl;
		cout << iHappy; 
		(iHappy>1)? cout << " were": cout 
<< " was";
		cout << " are happy with the 
service." << endl;
		
		cout << endl;

		cout << iSand;								
// Display how many people brought which 
item
		cout << " brought a sandwich." << 
endl; 

		cout << iSalad; 
		cout << " brought a salad." << 
endl; 

		cout << iBaked; 
		cout << " brought a baked potato." 
<< endl; 

		cout << iSnack; 
		cout << " brought a snack." << 
endl;

		cout << iMeal; 
		cout << " brought a full meal." << 
endl; 



		fPercent = ((float)iUse / 
(float)i)*(float)100;					// 
Calculation to display percentages

		cout << fPercent << "% use the 
student refectory regularly" << endl;

		fPercent = ((float)iHappy / 
(float)iUse)*(float)100; 

			if (fPercent <1)						
// Fix's devide by zero error
			{
				fPercent = 0;
			}										
// End of fix

		cout << fPercent << "% are happy 
with the service" << endl;

		cout << endl;


	}																				
//END IF exercise


See More: Skipping to end of program not using goto

Report •


#1
December 14, 2009 at 03:05:35
Hi,

It's hard to see the logic flow of your program because of the way this website has caused your pasted source code to wrap around. I would have suggested ways to add the condition in a loop control (such as 'for' or 'while'), if I could tell exactly what your program is doing (it's difficult to read) but in the meantime you could use 'break' instead of 'goto'.

By the way, if you are trying to write Standard C++ then you don't need process.h, iostream.h should be <iostream> and main returns int, not void. You only need to use the .h headers if you are using a very old (i.e. from the mid-90s) compiler.


Report •
Related Solutions


Ask Question