# Infinite Loop

Page 1 of 1

## 10 Replies - 2182 Views - Last Post: 13 November 2007 - 05:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=37210&amp;s=95073c85317ff2a3b29c9c0f62b7b6fb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 manzoor

• D.I.C Regular

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

# Infinite Loop

Posted 11 November 2007 - 09:17 AM

```/* Software Name = Temperature Converter Calculator...
// 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()
{
// program name output
cout << "################################################################################";
cout << "#																			  #";
cout << "#																			  #";
cout << "#																			  #";
cout << "#					   Temperature Converter Calculator					   #";
cout << "#																			  #";
cout << "#																			  #";
cout << "#																			  #";
cout << "#																			  #";
cout << "################################################################################\n\n";

do

cout << "\n================================ Main Menu =====================================\n\n\n";
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 : ";

{
cout << "\nWrong option entered\n";
}

if ( Main_Menu_Option == 67 || Main_Menu_Option == 99) // 67 == C , 99 == c.
{
CnvrtTemp();
}

{
cout << "Help section is under construction.\n";
}

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

Reputation: 0
• 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

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 6839
• 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");

```

### #4 manzoor

• D.I.C Regular

Reputation: 13
• 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

### #5 born2c0de

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

Reputation: 187
• 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().

### #6 no2pencil

• Professor Snuggly Pants

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

## Re: Infinite Loop

Posted 12 November 2007 - 07:42 AM

k00ldude47, 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.

### #7 manzoor

• D.I.C Regular

Reputation: 13
• 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

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 ?

### #8 no2pencil

• Professor Snuggly Pants

Reputation: 6839
• 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

### #9 jjhaag

• me editor am smartastic

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

## Re: Infinite Loop

Posted 12 November 2007 - 12:59 PM

k00ldude47, 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

### #10 no2pencil

• Professor Snuggly Pants

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

## Re: Infinite Loop

Posted 12 November 2007 - 01:06 PM

Much better than my example, thank you.

### #11 manzoor

• D.I.C Regular

Reputation: 13
• 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