Infinite Loop

It starts looping when you enter alphabet instead of numbers

Page 1 of 1

10 Replies - 2182 Views - Last Post: 13 November 2007 - 05:39 AM Rate Topic: -----

#1 manzoor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Infinite Loop

Posted 11 November 2007 - 09:17 AM

/* Software Name = Temperature Converter Calculator...
Made by Manzoor Ahmed.*/
// Program that helps converting Temperature degrees.
// Formulas obtained from wikipedia.org.

#include <cstdlib>
#include <cctype>
#include <iostream>

using namespace std;

int CelFunc();

void CnvrtTemp()
{
	char Cnvrt_Temp_Option;


		 cout << "\n==================== Convert Temperature =======================" << endl;
	 do
	  {
		 cout << "\n\n[C] Celsius Converter" << endl;
		 cout << "[F] Fahrenheit Converter" << endl;
		 cout << "[K] Kelvin Converter" << endl;
		 cout << "[R] Rankine Converter" << endl;
		 cout << "[M] Back to Main Menu." << endl;
	


		 cout << "\n(Characters enclosed by the square brackets are the options.)" << endl;
	
		 cout << "\nEnter your option: ";
		 cin >> Cnvrt_Temp_Option;
		 cin.ignore();

		 if ( Cnvrt_Temp_Option == 'C' || Cnvrt_Temp_Option == 'c' )
			{
			 CelFunc();
			}
			
		 if ( Cnvrt_Temp_Option == 'F' || Cnvrt_Temp_Option == 'f' )
			{
			 cout << "FahrFunc()" << endl;
			}
	
		 if ( Cnvrt_Temp_Option == 'K' || Cnvrt_Temp_Option == 'k' )
			{
			 cout << "KelFunc()" << endl;
			}

		 if ( Cnvrt_Temp_Option == 'R' || Cnvrt_Temp_Option == 'r' )
			{
			 cout << "RankFunc()" << endl;
			}
	   } while (!( Cnvrt_Temp_Option == 'M' || Cnvrt_Temp_Option == 'm' ));
}

int main()
{
		  char Main_Menu_Option;
							// program name output
		  cout << "################################################################################";
		  cout << "#																			  #";
		  cout << "#																			  #";
		  cout << "#																			  #";
		  cout << "#					   Temperature Converter Calculator					   #";
		  cout << "#																			  #";
		  cout << "#																			  #";
		  cout << "#																			  #";
		  cout << "#																			  #";
		  cout << "################################################################################\n\n";


		  do
			{					  // Main Menu

		  cout << "\n================================ Main Menu =====================================\n\n\n";
					 // Menu
		  cout << "[C] Convert Temperature" << endl;
		  cout << "[H] Help" << endl;
		  cout << "[E] Exit" << endl;

		  cout << "\n(Characters enclosed by the square brackets are the options.)\n" << endl;



			   cout << "\nEnter your option : ";
			   cin >> Main_Menu_Option;

			 
			   if (!( Main_Menu_Option == 'C' || Main_Menu_Option == 'c' || Main_Menu_Option == 'H' || Main_Menu_Option == 'h' || Main_Menu_Option == 'E' || Main_Menu_Option == 'e' ))
			   {
				  cout << "\nWrong option entered\n";
			   }



				if ( Main_Menu_Option == 67 || Main_Menu_Option == 99) // 67 == C , 99 == c.
				   {
					CnvrtTemp();
				   }
				
				if ( Main_Menu_Option == 69 || Main_Menu_Option == 104 )
					{
						cout << "Help section is under construction.\n";
					}	


		   } while (!( Main_Menu_Option == 69 || Main_Menu_Option == 101 ));
		  system ("pause");
		  return 0;
}

int CelFunc()
	{	char again;
		cout << "\n=============================== Celsius Converter ==============================\n\n";		
		
		
		do {
		cout << "Enter C (Celsius) degree to convert :";
		double cTemp2Conv;
		cin >> cTemp2Conv;
		cin.ignore();
		
			if (!(isdigit(cTemp2Conv))) {
				
				
				double cFahrenheit = (cTemp2Conv * 1.8) + 32;
				double cKelvin = cTemp2Conv + 273.15;
				double cRankine = (cTemp2Conv + 273.15) * 1.8;
				
				
				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Fahnrenheit scale = " << cFahrenheit;
				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Kelvin scale = " << cKelvin; 
				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Rankine scale = " << cRankine;
				cout << "\n\n";
			}
			else {
				cout << "\nINVALID INPUT !";
				cout << "\nPlease enter numerical values.\n";
			}			
			cout << "\nDo you want to convert temperature again ?";
			cout << "\nEnter Y for yes, N for no.";
			cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu.";
			cout << "\n[y]/[n]:";
			cin >> again;
				
		} while (again == 'y'|| again == 'Y');
	} 


Well the problem is in the CelFunc(),

When you input an alphabet instead of numerical values then it starts looping infinitely, I don't know whats wrong with my code may you could help.

You can check it out if you compile the code then go to Convert Temperature > Celsius Converter, and there enter alphabet and see it looping infinitely.

Please check the method of my input checking in CelFunc() and tell me is it fine or not, because someone told me that isdigit() only takes char may be thats the problem, but I also checked with cin.good() and got the same error, Infinite loop.

And I'm curious why no one answer my questions xD, Are they too difficult ?

This post has been edited by k00ldude47: 11 November 2007 - 09:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Infinite Loop

#2 Lt_Kernel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: Infinite Loop

Posted 11 November 2007 - 10:30 AM

 You need to force the users input to be a number. Run a check on the value of the variable with the isdigit() function. If it isn't a digit, prompt them to input it again, & don't compare it.



thats what the "no2pencil" told me to do and i don't understand
anyway goodluck
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,513
  • Joined: 10-May 07

Re: Infinite Loop

Posted 11 November 2007 - 10:37 AM

char c;
   scanf( "%c", &c );
   if( isdigit(c) )  printf( "You entered the digit %c\n", c );  
   else printf("You did not enter a digit\n");


Was This Post Helpful? 0
  • +
  • -

#4 manzoor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Infinite Loop

Posted 12 November 2007 - 02:59 AM

no2pencil are you talking to me or Lt_Kernel ?

cuz my program is of C++ not C and the code you posted is of C as far as I remember
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Infinite Loop

Posted 12 November 2007 - 07:37 AM

You'll need to flush the input buffer using fflush().
Click here to see how it's done.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,513
  • Joined: 10-May 07

Re: Infinite Loop

Posted 12 November 2007 - 07:42 AM

View Postk00ldude47, on 12 Nov, 2007 - 02:59 AM, said:

no2pencil are you talking to me or Lt_Kernel ?

cuz my program is of C++ not C and the code you posted is of C as far as I remember

C or C++, doesn't matter, it was just an example of how to check for digits.
Was This Post Helpful? 0
  • +
  • -

#7 manzoor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Infinite Loop

Posted 12 November 2007 - 09:06 AM

Hmmm I couldn't understand about the fflush thingy :P

dont know how to use it

But the problem is now solved


Some one tell me what to use

I use this

if (cin >> cTemp2Conv)


to check whether the input is ok or not

he told me how to use it but I don't know what or how it checks the input but finally its working fine


Any one tell how this thing is used ?
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,513
  • Joined: 10-May 07

Re: Infinite Loop

Posted 12 November 2007 - 09:19 AM

cin >> cTemp2Conv;
if(isdigit(cTemp2Conv)) {
	if (cTemp2Conv=="Whatever your value checking for") {
		cout << "Match!";
	}
	else {
		cout << "Not a match";
	}
}
else {
	cout << "Not a digit.";
}


This post has been edited by no2pencil: 12 November 2007 - 09:21 AM

Was This Post Helpful? 0
  • +
  • -

#9 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Infinite Loop

Posted 12 November 2007 - 12:59 PM

View Postk00ldude47, on 12 Nov, 2007 - 09:06 AM, said:

if (cin >> cTemp2Conv)


The >> operator is a formatted stream extraction operator in this case. Using it to get a number from the input stream only works if the inputted data is numerical. If the user enters a character string, it returns false and sets an error flag in cin to true.

Doing this will take care of making sure that the entered data is of the appropriate format for the variable you are trying to extract into. However, it will still leave the characters on the input buffer, and leave an error flag set for cin, so you'll need to deal with those.

The following short program demonstrates user input within a loop and proper clearing of the input buffer:
#include <iostream>

using namespace std;


int main() {
	
	double val;
	
	while (1) {
		  cout << "enter a number" << endl;
		  if (cin >> val) {
				 cout << cin.good() << endl;
				 cout << "number entered: " << val << endl;
				 break;
		  } else {
				 cout << cin.good() << endl;
				 cin.clear();
				 cin.ignore(99999,'\n');
				 cout << cin.good() << endl;
		  }
	}
	system ("pause");
	return 0;
}

If the user enters a number, everything works fine (the value is stored in val and the loop breaks. If not, the input step 1) sets an error flag, and 2) leaves the characters on the input buffer. This is what is taken care of in the else statement. The output of cin.good() will return false (0), because there has been an input failure if the program reaches this point. So we clear the flags using cin.clear(), allowing for further input. We then flush the input buffer by calling cin.ignore(99999,'\n'), which flushes up to 99999 characters from the buffer, stopping the flush when a newline character is found. This is because if the user inputs a string, there will still be a newline on the buffer, so we need to get rid of this as well. The second output of cin.good() is just a visualization to prove to you that the flags have been reset; it should ouput true (1) now.

And finally, once all the error flag clearing and input buffer flushing has been taken care of, the loop starts up at the top again, asking for an input.

Try working through the logic of the above program, and test it out a couple of times. And then once you're confident using those methods, incorporate them into your original program. Let us know if you have any further problems.

-jjh
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6839
  • View blog
  • Posts: 31,513
  • Joined: 10-May 07

Re: Infinite Loop

Posted 12 November 2007 - 01:06 PM

Much better than my example, thank you.
Was This Post Helpful? 0
  • +
  • -

#11 manzoor   User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Infinite Loop

Posted 13 November 2007 - 05:39 AM

Thanx jjhaag, no2pencil and born2code for helping

I currently don't have any more problems regarding this one
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1