[prog] validate input

wolf wolf at wolfrising.net
Sun Dec 21 21:23:40 EST 2003


Hi, I have this C ++ code that basically adds up out of 10 students who 
passed and who did not. You enter 1 if the student passed
and 2 if the student failed.  It loops around so you can enter your 
answer 10 times. All fine and good : ) However, how
would I have it check if the user correctly entered a 1 or a 2 and if 
they did not tell them "Invalid Entry Try Again".  The code
as is, does seem to work correctly : ) I'm working on sample practice 
questions and very much a beginner. Also, if there's
a way to improve the comments, or if the indentation is incorrect, 
please feel free to point that out as well.  I'd like to break
any bad habits at the beginning.  Thanks : )


#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()

{
	
	// initialize
     int passes = 0;
	int failures = 0;
	
	int studentCounter =1;
	
	
	int result;
	

	
	
	
	//  10 students
	while ( studentCounter <= 10 ) {
	
		
		
		cout << "Enter result (1 = pass, 2 = fail): ";
		cin >> result;
		
		if ( result == 1
			 )
			
			passes = passes + 1;
		else
			failures = failures + 1;
		
		studentCounter = studentCounter +1;
	}
	
	cout << "Passed " << passes << endl;
	cout << "Failed " << failures << endl;
	
	if (passes >= 8)
		cout << "Raise Tuition " << endl;
		
		return 0;
		
	}


More information about the Programming mailing list