How to stop program from running a statement twice

Hello all.

I am writing a program that converts Fahrenheit to Celsius, then after it converts, asks the user if they would like convert another temp until they say no. So I feel I'm on the right track with my program, but after it converts Fahrenheit to Celsius, it does it a second time, then asks if I want to convert again. How can I make it stop from converting a second time and just make it convert once then ask if I want to convert again. It has also been requested specifically that I put the code that asks the user if they want to convert again, in main().
Here is my code:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;

double getfahrenheit();
double converttocelsius(double);
void displaytable(double,double);

int main()
double fahren, celsius;
char choice;

fahren = getfahrenheit();

celsius = converttocelsius(fahren);

displaytable(fahren, celsius);

cout<<"\nWould you like to convert another temperature? (Y/N): ";
cin >> choice;
while (choice == 'Y'|| choice == 'y');


double getfahrenheit()
double fahnum;
cout << "\nPlease enter fahrenheit tempurature between -55 to 125: ";
cin >> fahnum;
while(fahnum > 125 || fahnum < -55)
cout << "You entered an invalid number. Please enter a vaild number: ";
cin >> fahnum;
return fahnum;

double converttocelsius(double fahnum)
double celnum;
return celnum = 5 * (fahnum - 32) / 9;

void displaytable(double fahnum,double celsius)
cout << "Fahrenheit: " << fahnum
<< "\nCelsius: " << celsius;

I have tried a lot of different things for hours. Can't quite pin it down. Any help is greatly appreciated!
Last edited on
the logic in main is weird.
here is what it says..

getf, but do not convert it
until no

seems like it should be
convert f to c
prompt to go again
while ()

don't throw code at it to try to fix it. Breaking it down as I did above, into pseudocode of some format that works for you, can make the error stand out so you can see it. Find a way to find the error without changing anything, coding by trial and error will NOT work very often (unfortunately it does work somewhat more frequently for beginner problems, making it tempting early on, but its a bad approach). Other variations on this include rubber duck debugging, playing computer (executing each line on paper by hand as if you were the computer), or taking a fresh piece of paper and stating what you want to do as steps the looking to validate that your code really follows those steps. Find something that works for you, though.

also... I think you are ok for your code, but its a good habit to add decimals to constants that are for doubles, eg /9.0 instead of 9, to avoid accidental integer math.
Last edited on
Okay I see I think. So essentially I need to change the order of my logic in main?
yes, just consolidate it all into the go again or not loop.
Got it. Thanks so much for the help and advice!
Topic archived. No new replies allowed.