1. ## Input Error

Okay I made a prog its for temperature conversion its still not developed yet and Im having problems

heres the code

Code:
```/* 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 ;

if ( Cnvrt_Temp_Option == 67 || Cnvrt_Temp_Option == 99 ) // 67 == C      99 == c
{
CelFunc();
}

// 70 = F  and 102 = f
if ( Cnvrt_Temp_Option == 70 || Cnvrt_Temp_Option == 102 )
{
cout << "FahrFunc()" << endl ;
}

if ( Cnvrt_Temp_Option == 75 || Cnvrt_Temp_Option == 107 )
{
cout << "KelFunc()" << endl;
}

if ( Cnvrt_Temp_Option == 82 || Cnvrt_Temp_Option == 114 )
{
cout << "RankFunc()" << endl ;
}
} while (!( Cnvrt_Temp_Option == 77 || Cnvrt_Temp_Option == 109 )); // 77 == M and 109 == 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 : ";

// Following I have used ASCII numbers to check the input from user
// whether the input is correct or not
// 67 = C,  99 = c, 72 = H, 104 = h, 69 = E, 101 = e .
{
cout << "\nWrong option entered\n";
}
//  67 == C,  99 == c, 72 == H, 104 == h, 69 == E, 101 == e .

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 :";
long double cTemp2Conv;
cin >> cTemp2Conv;

if (isdigit(cTemp2Conv)) {

long double cFahrenheit = (cTemp2Conv * 1.8) + 32;
long double cKelvin = cTemp2Conv + 273.15;
long 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');
}```
the problem is when you go to Celsius Converter and there input alphabets then the programs start looping infinitely

I tried cin.good() and isdigit() for checking the input but both gave me same results

any help is really appreciated

thanks

2. Surely you don't need 20+ digits precision in your temperature conversion? Using float or plain double should be perfectly sufficient, rather than using "long double".

Second, you are doing a lot of checking for letters with numbers. Why not do
Code:
`if (tolower(input) == 'c')  ...`
It save you writing a commend saying "99 = c", and using tolower means you only have ONE comparison.

Code:
`if (isdigit(cTemp2Conv))`
Is incorrect use of isdigit(). the function isdigit() takes a char as input, not a floating point value, and returns "true" if the number is in the range '0'..'9' - which mens 48..57 in integer values - so it would be true when you enter 48..57.9 degrees, and false otherwise.

if you want to check if the input is "ok", you can do:
Code:
```if (cin >> cTemp2Conv) {
... // input is accepted
...
} else {
... // input not correct.
cin.clear();  // Clear the "input had errors flag"
cin.ignore(100, '\n'); // remove any remaining unused input up to 100 chars ending with \n
...
}```
--
Mats

3. I think you will find this much simpler if you just use the 'C', 'c', 'E', 'e', etc, rather than using the ASCII numerical equivalent. Also, check the return of 'isdigit()'. I input a valid numer (32) and it returns an error.

4. hi thanks guys

matsp: i didn't understand what you said

or show me an example of my code ??

I don't see how you check the input with if (cin >> cTemp2Conv)

where's the condition in the if statement ?

5. Can you clarify which part you don't understand, and perhaps TRY to implement a correction along the lines of what I suggested, and then ask further questions (and show what you've done). I don't believe my comments were that unclear.

--
Mats

6. hmmm well I didn't see where you check the input of variable cTemp2Conv
what this line does ??

if (cin >> cTemp2Conv)

what does tolower() do ?

7. Originally Posted by manzoor
hmmm well I didn't see where you check the input of variable cTemp2Conv
what this line does ??

if (cin >> cTemp2Conv)

what does tolower() do ?
Code like this
Code:
```int x;
bool b = cin >> x;```
will produce a value in b that reflects the success of the "cin >> x" operaton. So if what do you think the following does:
Code:
```int x;
bool b = cin >> x;
if (b) cout << "x = " << x << endl;
else cout << "error" << endl;```
??

tolower() takes a char and produces the lower-case version of it. E.g. tolower('A') is 'a', tolower('p') is 'p'. In the usage I suggest, it avoids checking for both the upper and lower case of the same value, since it's already been converted to lower-case, so it CAN'T be upper case.

--
Mats

8. hmm ok i understand the tolower() func

but still can't understand how you check the input

can you add the code to my temperature converter and then explain it step-by-step

im a beginner and don't understand things that much

so please explain the code step-by-step and if you can the add the code of checking the input to my temperature converter then i'll be so kind to you

9. *bump*

Help Please I Need To Show This Program To My School I Have Exam Need Help Asap

10. See Rule 5
Post your latest effort, and drop the "Initial Caps Style" and urgency claims.

We can help you along, but you've got to show what you know and what you've been learning. We're not here just to pass exams on your behalf.

11. I Said Help

12. And we've told you to "try it, then post the code". As Salem says, we're not here to do the job for you.

If anyone else thinks my statements above are unclearn, then please feel free to clarify [sometimes what one person thinks is clear, isn't really clear]

--
Mats

13. I think it's "clearn" (), but I'll try to explain it in more detail.
Code:
`if (cin >> cTemp2Conv)`
cin's >> operator returns a bool, just like this.
Code:
```bool function() {
/* ... */
}```
This boolean value indicates whether the cin operation was a success or not. It returns true if it succeeded. So, you can do something like this.
Code:
```int var;
bool success;

success = (cin >> var);
if(success) {
/* success */
}
else {
/* failure */
}```
if(success) is the same as if(success == true).

But you can also forgo the "success" variable entirely and put the expression directly into an if statement.
Code:
```int var;
bool success;

if(cin >> var) {
/* success */
}
else {
/* failure */
}```

14. Code:
```int var;
bool success;

success = (cin >> var);
if(success) {
/*So you mean here I put my code if the input was correct

Right ?*/
}
else {
/* And if the input was a alphabet instead of the number then show the error message here ? Right ? */
}```

15. Code:
```/* 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 &#176;C (Celsius) degree to convert :";
float cTemp2Conv;
cin >> cTemp2Conv;
cin.ignore();

if (cin >> 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";
}
cin.clear();
cin.ignore(100, '\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');
}```
OK HERE'S the code

But I still have one problem and it is, that now I have to enter two values for example

Look at the output:

Enter C (Celsius) degree to convert: 100
100

Answer: 100 Celsius in Fahrenheit is: 212....
....

Blah blah see ??

you got what i wanna say ?

but when you enter an alphabet then it responds at the first input see the output

OUTPUT:

Enter C (Celsius) degree to convert: c

INVALID INPUT !