n0de
n0de

Reputation: 155

C++ Fahrenheit To Celsius (vice versa) Loops Issue

I have two issues.

1.When I use decimals to convert, it always comes out as 0.

When I don't use decimals it comes out fine.

2.I want to loop my results so that if I put let's say 32 (start_temp) Fahrenheit I can increment it until my "Ending temperature" (end_temp) & for it to also convert the incremented, let's say 42 if the increment was 10 from 32, then it would convert 42 Fahrenheit to it's Celsius.

I tried doing a for loop but with no luck.

Please help?

int main()
{
    int choice;
    cout << "Please Select A Temperature Scale Conversion:\n"<< endl;
    cout << "1. Convert F to C" << endl;
    cout << "2. Convert C to F\n" << endl;
    cout << "Your Choice: ";
    cin >> choice;

    cout << "Starting Temperature: ";
    cin >> start_temp;
    cout << "Ending Temperature: ";
    cin >> end_temp;
    cout << "Temperature Increase: ";
    cin >> temp_incr;

    if (choice == 1) {
        cout << fixed << setprecision(2) << calcFahrenheit(start_temp) << endl;
    } else if (choice == 2) {
        cout << fixed << setprecision(2) << calcCelsius(start_temp) << endl;
    } else {
        cout << "You have entered an invalid option." << endl;
    }

    system("PAUSE");
    return 0;
}

float calcFahrenheit(float start_temp) {
    float f2c;
    f2c = (5.0/9.0) * (start_temp - 32.0);
    return f2c;
}

float calcCelsius(float start_temp) {
    float c2f;
    c2f = (9.0/5.0) * (start_temp + 32.0);
    return c2f;
}

Upvotes: 0

Views: 4003

Answers (1)

Omkant
Omkant

Reputation: 9204

You can do it as keep start_temp in c2f and then calculate c2f again with that value inside the loop till the end_temp and increase c2f with temp_incr

for (c2f=start_temp ; c2f<=end_temp ; c2f+=temp_incr)
 {
   // Do your calculation 
 }

Same thing applies on the other formula also

Upvotes: 1

Related Questions