Hello guys I have a problem with my code: I want to print an error message every time the user enters a number lower than -273.15 . My code runs well and does the desired mathematical operation. It even loops back to the input message, but I'm having huge headaches trying to sort out how to best approach that simple task of adding a cout statement to the validation loop. =(

#include <iostream>
using namespace std;

int main ()

{
	double dCel, dFah;
// user inputs temp then it's converted to Fahrenheit
	
	do {
	cout << "Enter the temperature in Celsius: \t \n ";
	cin >> dCel;
	
	}

	while (dCel < -273.15);
			
	dFah = dCel * (9.0/5.0) + 32.0;
	cout<< "\t" << dCel << " \tdegrees Celsius converts to:  " << dFah << " degrees Fahrenheit \n "<<endl ;
	
			
// user inputs temp in Fah and converted to Cel	
	do {
	cout << "Enter the temperature in Fahrenheit: \t \n";
	cin >> dFah;
	
	}
	while ( dFah < -273.15 );
	dCel = (dFah - 32.0) * (5.0/9.0);
	cout << " \t " << dFah << "\t degrees Fahrenheit converts to:  " << dCel << " degrees Celsius \n ";
	
	//system pause only used to copy console output window
	system ("pause");
	
	return 0;

	
}

desired output:
**This part works ok ;

/*
Enter the temperature in Celsius:
100
100 degrees Celsius converts to: 212 degrees Fahrenheit

Enter the temperature in Fahrenheit:
100
100 degrees Fahrenheit converts to: 37.7778 degrees Celsius
Press any key to continue . . .
*/


But, this part not so:
/*
Enter the temperature in Celsius:

-300
Enter the temperature in Celsius:
-500
Enter the temperature in Celsius: */

As you can see it basically does what I want on the math side, but I have no idea how to insert an error message to the do while loop.

Thanks in advance,

Jean

Recommended Answers

All 5 Replies

Hi,

See if this works for you:

const double MIN_CELSIUS = -273.15;
    double dCel = 0, dFah = 0;

    // user inputs temp then it's converted to Fahrenheit
    do 
    {
        if(dCel < MIN_CELSIUS)
        {
            cout << "Invalid temperature: it cannot be lower than " << MIN_CELSIUS;
            _getch();

            system("cls"); // erases the console
        }
        cout << "Enter the temperature in Celsius: \t \n ";
        cin >> dCel;
    }
    while (dCel < MIN_CELSIUS);

Hi,

See if this works for you:

const double MIN_CELSIUS = -273.15;
    double dCel = 0, dFah = 0;

    // user inputs temp then it's converted to Fahrenheit
    do 
    {
        if(dCel < MIN_CELSIUS)
        {
            cout << "Invalid temperature: it cannot be lower than " << MIN_CELSIUS;
            _getch();

            system("cls"); // erases the console
        }
        cout << "Enter the temperature in Celsius: \t \n ";
        cin >> dCel;
    }
    while (dCel < MIN_CELSIUS);

Tyvm for the help;
A few questions:
what does the " _getch()" and the"system ("cls") functions performs?
is the system ("cls"); really necesary?
also would I need to define another const for the MIN_FAHRENHEIT?

I'm trying to solve this as simple as possible with only do/while and if/else statements so my lab instructor doesn't gets mad at me. And if I'm using something that's a bit more structured or complex I need to understand it first so I can explain it later if questioned about it.

Hello guys I have a problem with my code: I want to print an error message every time the user enters a number lower than -273.15 . My code runs well and does the desired mathematical operation. It even loops back to the input message, but I'm having huge headaches trying to sort out how to best approach that simple task of adding a cout statement to the validation loop. =(

<snip>

But, this part not so:
/*
Enter the temperature in Celsius:

-300
Enter the temperature in Celsius:
-500
Enter the temperature in Celsius: */

As you can see it basically does what I want on the math side, but I have no idea how to insert an error message to the do while loop.

Maybe something like this?

#include <iostream>
using namespace std;

int main ()
{
    const double dCelMin = -273.15;
    double dCel, dFah;

    for ( ;; )
    {
        cout << "Enter the temperature in Celsius: ";
        cin >> dCel;
        if ( dCel >= dCelMin )
        {
            break;
        }
        cout << "Enter a value greater than " << dCelMin << "\n";
    }

    dFah = dCel * (9.0/5.0) + 32.0;
    cout << dCel << " degrees Celsius converts to:  " 
         << dFah << " degrees Fahrenheit\n" ;

    return 0;
}  

/* my output
Enter the temperature in Celsius: -500
Enter a value greater than -273.15
Enter the temperature in Celsius: -300
Enter a value greater than -273.15
Enter the temperature in Celsius: -200
-200 degrees Celsius converts to:  -328 degrees Fahrenheit
*/

[edit]D'oh! And then I see this:

I'm trying to solve this as simple as possible with only do/while and if/else statements so my lab instructor doesn't gets mad at me.

:(

do
    {
        cout << "Enter the temperature in Celsius: ";
        cin >> dCel;
        if ( dCel >= dCelMin )
        {
            break;
        }
        cout << "Enter a value greater than " << dCelMin << "\n";
    } while ( 1 );

?

Or even...

do
    {
        cout << "Enter the temperature in Celsius: ";
        cin >> dCel;
        if ( dCel < dCelMin )
        {
            cout << "Enter a value greater than " << dCelMin << "\n";
        }
    } while ( dCel < dCelMin );

?

_getch()

is so that the error message is printed until the user presses a key

system("cls");

is so that the error message gets deleted after the user reads it (and acknowledges it by pressing a key)

if you don't want to delete the error message, neither is necessary.

tyvm!! yes it worked for me // I took out the getchar and the system funcionts / but otherwise it worked excellent tyvm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.