user2539369
user2539369

Reputation:

C++ celsius to fahrenheit 3 times

I'm learning C++, and to do that I have created myself a problem which is to convert Celsius to Fahrenheit three times in the console. The user will input the Celsius degree. I also want the output to be displayed like this:

Celsius: Fahrenheit: cel1 fahr1 cel2 fahr2 cel3 fahr3

The code I have tried so far is:

double cel1, cel2, cel3;
double fahr1, fahr2, fahr3;

cout << "Celsius degree one: ";
cin >> cel1;

cout << "Celsius degree two: ";
cin >> cel2;

cout << "Celsius degree three: ";
cin >> cel3;

fahr1 = (cel1 * 9) / 5 + 32;
fahr2 = (cel2 * 9) / 5 + 32;
fahr3 = (cel3 * 9) / 5 + 32;

// messy like this to display like I want to 
cout << endl <<
    "Celsius:  " << "Fahrenheit:" << endl <<
    cel1 << "         " << fahr1 << endl <<
    cel2 << "         " << fahr2 << endl <<
    cel3 << "         " << fahr3 << endl << endl;

which will display like I want to, but I feel this could have been achieved in a simpler way, so I tried something like this with a loop, but I couldn't figure out how to do it properly:

double celsius;

for (int times = 0; times != 3; ++times){

    cout << "Celsius degree: ";
    cin >> celsius;

    double fahrenheit = (celsius * 9) / 5 + 32;

    cout << "Fahrenheit degree: " << fahrenheit << endl;

    cin.clear();

}

This code is less then the previous one, gives the correct answer and will convert three times, but I couldn't figure out how to display it like I want to.

My question is what is the best way to do this?

Upvotes: 0

Views: 471

Answers (2)

Jarod42
Jarod42

Reputation: 217145

I suggest to split the code into smaller functions:

The one to compute the conversion

double celsius_to_fahrenheit(double celsius)
{
    return (celsius * 9.0) / 5.0 + 32.0;
}

The one to get the input, I choose to use std::vector as container. you may choose std::array<double, 3> since the array have fixed size, but std::vector is a good default choice.

std::vector<double> get_input_celsius(std::size_t size)
{
    std::vector<double> celsius(size);
    for (std::size_t i = 0; i != celsius.size(); ++i) {
        std::cout << "Celsius degree " << (i + 1) << ": ";
        std::cin >> celsius[i];
    }
    return celsius;
}

The method to display the result. I choose to not store the conversion in a new std::vector since it is not used afterward:

void display_celsius_and_fahrenheit(const std::vector<double>& celsius)
{
    std::cout << std::endl << "Celsius:  " << "Fahrenheit:" << std::endl;
    for (auto c : celsius) { // for range since C++11
        std::cout << c << "         " << celsius_to_fahrenheit(c) << std::endl;
    }
}

And finally the main function:

int main()
{
    std::vector<double> celsius = get_input_celsius(3);

    display_celsius_and_fahrenheit(celsius);
    return 0;
}

Live example

Upvotes: 1

R Sahu
R Sahu

Reputation: 206567

  1. Create arrays to store the temperatues.

    Instead of

    double cel1, cel2, cel3;
    double fahr1, fahr2, fahr3;
    

use

    double celsius[3];
    double fahrenheit[3];
  1. Change the input gathering loop to use the arrays.

    for (int times = 0; times != 3; ++times){
    
        cout << "Celsius degree: ";
        cin >> celsius[times];
    
        fahrenheit[times] = (celsius[times] * 9) / 5 + 32;
    
        cin.clear();
    }
    
  2. Use a loop to create the output:

    cout << endl << "Celsius:  " << "Fahrenheit:" << endl <<
    for (int times = 0; times != 3; ++times){
      cout << celsius[times] << "         " << fahrenheit[times] << endl;
    }
    

You could get away with not creating the array fahrenheit too if you compute it only during output.

In that case, change the input gathering loop to:

    for (int times = 0; times != 3; ++times){

        cout << "Celsius degree: ";
        cin >> celsius[times];

        cin.clear();
    }

Change the output loop to:

    cout << endl << "Celsius:  " << "Fahrenheit:" << endl <<
    for (int times = 0; times != 3; ++times){
      double fahrenheit = (celsius[times] * 9) / 5 + 32;
      cout << celsius[times] << "         " << fahrenheit << endl;
    }

Upvotes: 0

Related Questions