3
Contributors
5
Replies
34
Views
2 Years
Discussion Span
Last Post by Moschops
0

I didn't see a link to Dani (in the top links) in that Google search ...

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

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
*/

const string MENU =
      "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;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // 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 ...
    {
        switch( takeInChr( MENU ) )
        {
            case '1': FahrenheitToCelsius() ; break;
            case '2': CelsiusToFahrenheit() ; break;
            default :
                cout << "\nInvalid entry ... Please try again ...\n";
        }
        cout << "\n\n";
    }
    while( more() );

    return 0;
}
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
*/

const string MENU =
      "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;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // 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 ...
    {
        switch( takeInChr( MENU ) )
        {
            case '1': FahrenheitToCelsius() ; break;
            case '2': CelsiusToFahrenheit() ; break;
            default :
                cout << "\nInvalid entry ... Please try again ...\n";
        }
        cout << "\n\n";
    }
    while( more() );

    return 0;
}
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.