While loop woes

2 while loops for 1 input both trying to avoid input errors

Page 1 of 1

8 Replies - 781 Views - Last Post: 10 January 2009 - 10:54 AM Rate Topic: -----

#1 martini1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 06

While loop woes

Posted 10 January 2009 - 08:40 AM

Hi people heres the section of my code thats the problem

	  cout<<"\nPlease Enter an Operator (1 for + , 2 for - , 3 for * or 4 for /): ";
	  cin>> operator_number_value;
	  while ( !( cin>> operator_number_value ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
	  while ( operator_number_value != 1 || 2 || 3 || 4 ) {
			cout<<"Invalid Input, Try Again: ";
			cin>> operator_number_value;
			}



the problem is the 2 while loops are to prevent 2 different types of input error. the first loop for stopping letter input from crashing the program as it is a int variable. the second loop is for only allowing 1,2,3 or 4 as valid input values, but if i test the second while loop then test the first the program drops to an infinite loop.

any help appreciated thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: While loop woes

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: While loop woes

Posted 10 January 2009 - 09:11 AM

Take a look at this line - you seem to be somewhat confused about the logical 'or' operator.
while ( operator_number_value != 1 || 2 || 3 || 4 )

This roughly translates to
while ( operator_number_value is not 1 )
or while ( 2 )
or while ( 3 )
or while ( 4 )

2, 3 and 4 are all non-zero values, which means they'll always evaluate to a true condition - you're stuck in an infinite loop

Perhaps you intended to do
while ( operator_number_value != 1 &&
        operator_number_value != 2 &&
        operator_number_value != 3 &&
        operator_number_value != 4 )
There are other ways to express this, depending on your preference, you could use < and > to check a range of values.

This post has been edited by Bench: 10 January 2009 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 demongill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-January 09

Re: While loop woes

Posted 10 January 2009 - 09:19 AM

how about change the 2nd loop to be like this:
while ( operator_number_value == 0 && operator_number_value > 4 ) 
{
	  cout <<"Invalid Input, Try Again: ";
	  cin >> operator_number_value;
}



i hope it works ^^
Was This Post Helpful? 0
  • +
  • -

#4 martini1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 06

Re: While loop woes

Posted 10 January 2009 - 09:26 AM

thanks for the speedy reply ive given what you said a go and the program behaves the same way using my first method, your method and i also tryed the < and > to do a range and even that bahaved the same way.
I'll send all of my source code for you to compile and fiddle around with if you like
int main()
{
  float number1;
  int operator_number_value;
  float number2;
  float sum;
  char operator_sign_value;
  bool new_sum = 0;

cout<<"--------------------Welcome to My Simple Calculator--------------------\n";
cout<<"A Simple 4 Function 1 Operation Calculator for the Windows command line\n";
while ( new_sum == 0 ) {
	  
	  cout<<"\nPlease Enter First Number: ";
	  while ( !( cin>> number1 ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
			
	  cout<<"\nPlease Enter an Operator (1 for + , 2 for - , 3 for * or 4 for /): ";
	  cin>> operator_number_value;
	  while ( !( cin>> operator_number_value ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
	  while ( operator_number_value != 1 || 2 || 3 || 4 ) {
			cout<<"Invalid Input, Try Again: ";
			cin>> operator_number_value;
			}
			
	  cout<<"\nPlease Enter a Second Number: ";
	  while ( !( cin>> number2 ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
			
	  cin.ignore();
	  
	  if ( operator_number_value == 1 ) {
		 operator_sign_value = '+';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 + number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 2 ) {
		 operator_sign_value = '-';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 - number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 3 ) {
		 operator_sign_value = '*';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 * number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 4 ) {
		 operator_sign_value = '/';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 / number2 <<"\n";
		 }
		 
		 cout<<"\nNew Sum? (0 for Yes or 1 for No): ";
		 while ( !( cin>> new_sum ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
		 
	  }
	  
	  cin.get();
	  
}



tryed the second post but no joy.

This post has been edited by martini1992: 10 January 2009 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 F!st!cuffs  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 153
  • Joined: 15-July 08

Re: While loop woes

Posted 10 January 2009 - 09:28 AM

Actually for this purpose it would be
while ( operator_number_value < 1 || operator_number_value > 4 )
{
	cout <<"Invalid Input, Try Again: ";
	cin >> operator_number_value;
}


This post has been edited by F!st!cuffs: 10 January 2009 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#6 martini1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 06

Re: While loop woes

Posted 10 January 2009 - 09:32 AM

hmm... this is weird it still doesnt want to work right.
in c++ is there a "goto line" or "goto command" command?
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: While loop woes

Posted 10 January 2009 - 09:34 AM

You haven't changed the problematic line at all in your second post, its still there -
while ( operator_number_value != 1 || 2 || 3 || 4 )
Was This Post Helpful? 0
  • +
  • -

#8 martini1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 06

Re: While loop woes

Posted 10 January 2009 - 09:35 AM

i did i must have copied it from the wrong window as i didnt want to lose anything i had done by accident
here is the full updated code
#include <iostream>

using namespace std;
int main()
{
  float number1;
  int operator_number_value;
  float number2;
  float sum;
  char operator_sign_value;
  bool new_sum = 0;

cout<<"--------------------Welcome to My Simple Calculator--------------------\n";
cout<<"A Simple 4 Function 1 Operation Calculator for the Windows command line\n";
while ( new_sum == 0 ) {
	  
	  cout<<"\nPlease Enter First Number: ";
	  while ( !( cin>> number1 ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
			
	  cout<<"\nPlease Enter an Operator (1 for + , 2 for - , 3 for * or 4 for /): ";
	  cin>> operator_number_value;
	  while ( !( cin>> operator_number_value ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
	  while ( operator_number_value < 1 || operator_number_value > 4 ) {
			cout<<"Invalid Input, Try Again: ";
			cin>> operator_number_value;
			}
			
	  cout<<"\nPlease Enter a Second Number: ";
	  while ( !( cin>> number2 ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
			
	  cin.ignore();
	  
	  if ( operator_number_value == 1 ) {
		 operator_sign_value = '+';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 + number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 2 ) {
		 operator_sign_value = '-';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 - number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 3 ) {
		 operator_sign_value = '*';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 * number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 4 ) {
		 operator_sign_value = '/';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 / number2 <<"\n";
		 }
		 
		 cout<<"\nNew Sum? (0 for Yes or 1 for No): ";
		 while ( !( cin>> new_sum ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
		 
	  }
	  
	  cin.get();
	  
}



is there any way i can cut the 2 while loops into one or change one or both to an if?

This post has been edited by martini1992: 10 January 2009 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#9 martini1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 06

Re: While loop woes

Posted 10 January 2009 - 10:54 AM

no matter now i have fixed it with a goto command

#include <iostream>

using namespace std;
int main()
{
  float number1;
  int operator_number_value;
  float number2;
  float sum;
  char operator_sign_value;
  bool new_sum = 0;

cout<<"--------------------Welcome to My Simple Calculator--------------------\n";
cout<<"A Simple 4 Function 1 Operation Calculator for the Windows Command Line\n";
while ( new_sum == 0 ) {
	  
	  cout<<"\nPlease Enter First Number: ";
	  while ( !( cin>> number1 ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
			
	  cout<<"\nPlease Enter an Operator (1 for + , 2 for - , 3 for * or 4 for /): ";
here:
	  while ( !( cin>> operator_number_value ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
	  while ( operator_number_value < 1 || operator_number_value > 4 ) {
			cout<<"Invalid Input, Try Again: ";
			goto here;
			}
			
	  cout<<"\nPlease Enter a Second Number: ";
	  while ( !( cin>> number2 ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
			
	  cin.ignore();
	  
	  if ( operator_number_value == 1 ) {
		 operator_sign_value = '+';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 + number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 2 ) {
		 operator_sign_value = '-';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 - number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 3 ) {
		 operator_sign_value = '*';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 * number2 <<"\n";
		 }
		 
	  else if ( operator_number_value == 4 ) {
		 operator_sign_value = '/';
		 cout<<"\nEntered Sum: "<< number1 << operator_sign_value << number2 <<"\n";
		 cout<<"\nYour Answer: "<< number1 / number2 <<"\n";
		 }
		 
		 cout<<"\nNew Sum? (0 for Yes or 1 for No): ";
		 while ( !( cin>> new_sum ) ) {
			cerr<<"Invalid Input, Try Again: ";
			cin.clear();
			cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
			}
		 
	  }
	  
	  cin.get();
	  
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1