Output statement repeats itself before input (C++)

240 views Asked by At

I'm writing a code that converts temperatures from Celsius to Fahrenheit and vice versa. So far everything is working fine, except one output statement repeats itself for no reason. I'm not very experienced (only moved from Python to C++ a week ago) so all help is appreciated.

This is my code:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int ConvertCelsius(int C)
{
    int F;
    F = C *(9 / 5) + 32;
    return F;
}

int ConvertFah(int F)
{
    int C;
    C = (5 * (F - 32)) / 9;
    return C;
}

int Conversion(string conv)
{

    if (conv == "CF") {
        int C;
        cout << "Enter Celsius temperature: ";
        cin >> C;
        int F = ConvertCelsius(C);
        cout << "Equivalent Fahrenheit temperature: " << F << endl;
        return 0;
    }
    if (conv == "FC") {
        int F;
        cout << "Enter Fahrenheit temperature: ";
        cin >> F;
        int C = ConvertFah(F);
        cout << "Equivalent Celsius temperature: " << C << endl;
        return 0;
    }
    else {
        cout << "Invalid conversion type." << endl;
        return 1;
    }

}

int main()
{
    int runs;
    cout << "How many conversions to be done? ";
    cin >> runs;
    cout << " " << endl;

    for (int i = 1; i <= runs; i++) {
        string conv;
        getline(cin, conv);
        cout << "Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] ";

        if (conv != "") {
            int check = Conversion(conv);
            runs = runs + check;
        }
        else {
            runs = runs + 1;
        }

    }

    return 0;
}

When I run the code i get the following output:

How many conversions to be done? 3

Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] CF
Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] Enter Celsius temperature: 12
Equivalent Fahrenheit temperature: 44
Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] FC
Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] Enter Fahrenheit temperature: 56
Equivalent Celsius temperature: 13
Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] CF
Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] Enter Celsius temperature: 67
Equivalent Fahrenheit temperature: 99
Press any key to continue . . .

Here you can see that the line "Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC]" appears twice. It should only appear once before the user inputs CF or FC. After that the second line should only be "Enter Celsius temperature:" or "Enter Fahrenheit temperature:".

I've tried to figure it out myself but have failed so far. Any help is appreciated. Thanks.

edit: I looked at a suggested solution (Why does std::getline() skip input after a formatted extraction?) however that points out problems with getline() when two inputs are taken using the command. In my situation, I'm taking just once input however, for some reason it outputs a statement outside of the for loop and the Conversion(conv) function.

1

There are 1 answers

0
Migos On

I was able to solve it using user4581301 answer. It is also mentioned in the tagged duplicate. My new for loop in the main function looks like this:

for (int i = 1; i <= runs; i++) {
    string conv;
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); //adding this line fixed the problem
    cout << "Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] ";
    getline(cin, conv);


    if (conv != "") {
        int check = Conversion(conv);
        runs = runs + check;
    }
    else {
        runs = runs + 1;
    }

}