a. degree fahrenheit to celsius and vice versa
b. centimeter to feet and inches to feet

Maybe it's time to do a nice student example ... using a menu and functions ... also with a function to vaidate the numeric input?

## All 5 Replies

Maybe it's time to do a nice student example ... using a menu and functions ... also with a function to vaidate the numeric input?

Just an example to try to get Dani in the top listing of Google search ... of just part 'a'

(Part 'b' should be easy to add to the menu as choices 3 and 4 with function calls for each.)

``````// FahrenheitToCelsiusAndViceVersa.cpp //

// demo of ...
// 'how a student might do validation of numeric input'

// see "Six Fast Steps to Programming in C++"
// http://developers-heaven.net/forum/index.php/topic,2022.0.html

#include <iostream>
#include <string>
#include <cctype> // re. tolower...

using namespace std;

/*
http://en.wikipedia.org/wiki/Fahrenheit

Fahrenheit to Celsius : (°F - 32) ÷ 1.8 =°C

Celsius to Fahrenheit : (°C × 1.8) + 32 =°F
*/

"1) Fahrenheit To Celsius\n"
"2) Celsius To Fahrenheit\n"
"Your choice (1 or 2) ? ";

// some utilities ...

double takeInDbl( const string& msg,
const string& errMsg =
"\nOnly numbers are a valid entry here!\n" )
{
double val;
while( true )
{
cout << msg << flush;
if( cin >> val && cin.get() == '\n' )
break;
else
{
cout << errMsg;
cin.clear();
cin.sync();
}
}
return val;
}

char takeInChr( const string& msg )
{
cout << msg << flush;
// else ...
return 0;
}

bool more()
{
if( tolower( takeInChr( "More (y/n) ?" ) ) == 'n' )
return false;
// else ...
return true;
}

// ... functions to convert ...

void FahrenheitToCelsius()
{
double f = takeInDbl( "\nEnter Fahrenheit temperature to convert: " );
cout << f << " Fahrenheit degrees = ";
cout << (f - 32) / 1.8 << " Celsius degrees.";
}

void CelsiusToFahrenheit()
{
double c = takeInDbl( "\nEnter Celsius temperature to convert: " );
cout << c << " Celsius degrees = ";
cout << (c * 1.8) + 32  << " Fahrenheit degrees.";
}

int main()
{
do // loop wile more requested ...
{
{
case '1': FahrenheitToCelsius() ; break;
case '2': CelsiusToFahrenheit() ; break;
default :
cout << "\nInvalid entry ... Please try again ...\n";
}
cout << "\n\n";
}
while( more() );

return 0;
}
``````

Just an example to try to get Dani in the top listing of Google search ... of just part 'a'

(Part 'b' should be easy to add to the menu as choices 3 and 4 with function calls for each.)

``````// FahrenheitToCelsiusAndViceVersa.cpp //

// demo of ...
// 'how a student might do validation of numeric input'

// see "Six Fast Steps to Programming in C++"
// http://developers-heaven.net/forum/index.php/topic,2022.0.html

#include <iostream>
#include <string>
#include <cctype> // re. tolower...

using namespace std;

/*
http://en.wikipedia.org/wiki/Fahrenheit

Fahrenheit to Celsius : (°F - 32) ÷ 1.8 =°C

Celsius to Fahrenheit : (°C × 1.8) + 32 =°F
*/

"1) Fahrenheit To Celsius\n"
"2) Celsius To Fahrenheit\n"
"Your choice (1 or 2) ? ";

// some utilities ...

double takeInDbl( const string& msg,
const string& errMsg =
"\nOnly numbers are a valid entry here!\n" )
{
double val;
while( true )
{
cout << msg << flush;
if( cin >> val && cin.get() == '\n' )
break;
else
{
cout << errMsg;
cin.clear();
cin.sync();
}
}
return val;
}

char takeInChr( const string& msg )
{
cout << msg << flush;
// else ...
return 0;
}

bool more()
{
if( tolower( takeInChr( "More (y/n) ?" ) ) == 'n' )
return false;
// else ...
return true;
}

// ... functions to convert ...

void FahrenheitToCelsius()
{
double f = takeInDbl( "\nEnter Fahrenheit temperature to convert: " );
cout << f << " Fahrenheit degrees = ";
cout << (f - 32) / 1.8 << " Celsius degrees.";
}

void CelsiusToFahrenheit()
{
double c = takeInDbl( "\nEnter Celsius temperature to convert: " );
cout << c << " Celsius degrees = ";
cout << (c * 1.8) + 32  << " Fahrenheit degrees.";
}

int main()
{
do // loop wile more requested ...
{
{
case '1': FahrenheitToCelsius() ; break;
case '2': CelsiusToFahrenheit() ; break;
default :
cout << "\nInvalid entry ... Please try again ...\n";
}
cout << "\n\n";
}
while( more() );

return 0;
}
``````

Dani's policy was really more to not just do people's homework for them, rather than provide them with them homework all neatly done.

Be a part of the DaniWeb community

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