0

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.

Community
  • 1
  • 1
Migos
  • 149
  • 8
  • `cin >> runs` likely leaves an end of line in the stream causing `getline(cin, conv);` to return immediately and empty. – user4581301 Dec 31 '16 at 22:47
  • 1
    aside: `C *(9 / 5) + 32;` won't yield expected result because of integer division: `9/5==1` so it's like you're doing `C + 32` – Jean-François Fabre Dec 31 '16 at 22:49
  • Should the `getline(cin, conv);` and `cout << "Enter conversion...` lines not be swapped around ? You're first reading a line and then informing the user he has to provide input. – Unimportant Dec 31 '16 at 22:51
  • @user4581301 That is a possibility I noticed in the Debugger. Any way to solve it? – Migos Dec 31 '16 at 23:03
  • @user1320881 i tried that at first but instead of fixing the problem it repeated the line with the first line instead of the second ("Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC] Enter conversion type: Celsius to Fahrenheit [CF] or Fahrenheit to Celsius [FC]") – Migos Dec 31 '16 at 23:07
  • Two ways 1. don't mix and match `>>` and `getline`. Use one or the other exclusively. 2. insert `cin.ignore(numeric_limits::max(), '\n');` to eat linefeeds after `>>` where a linefeed is expected. `#include ` to get `max`. – user4581301 Dec 31 '16 at 23:10
  • @user4581301 Thank you, that solved it. (I'm new to stackoverflow) can you submit your comment as an answer so I can mark it solved, or should I just post it as an answer. – Migos Dec 31 '16 at 23:43
  • No need. The tagged duplicate is what you've run up against, with just a slight difference in where the inputs are being taken. The dupe has all of the inputs in one place; in your case they are spread out more. The sequence of events is identical. Off topic, you should strongly consider following the dupe's example of testing the inputs for validity. Your program can be easily broken by providing an invalid value, such as "fubar" for `cin >> runs;` – user4581301 Jan 01 '17 at 00:15

1 Answers1

0

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;
    }

}
Migos
  • 149
  • 8