C++ Celsius to Fahrenheit conversion assistance

Write your question here.
I require assistance with my program that converts Fahrenheit to Celsius. The program does not report any errors but will not perform a repeat the conversion when the user enters Y. The program uses do...while I've tried using a if...else also with no errors. Please provide me some valuable assistance in completing my program TY.

// INF 231 Week 5 Assignment
// C++ Program to Perform Fahrenheit to Celsius Conversion
#include <iostream> // Allows the program to perform input and output
using std::cout;
using std::cin;
using std::endl;

double fahrenheitToCelsius(double fahrenheit)
{
double celsius;

celsius = (fahrenheit - 32.0) * 5.0 / 9.0; // Formula to convert fahrenheit to celsius
return celsius;
}
// Function main begins program execution
int main()
{

// Variable declarations
char goagain; // Users input if they'd like to continue
double fahrenheit; // Users temperature input

do {
cout << "Enter temperature in fahrenheit (in degrees) "; // Prompt user for temp in fahrenheit
cin >> fahrenheit; // read temp from user
cout << "Temperature in Celsius (in degrees) = "; // Display temp converted from fahrenheit
cout << fahrenheitToCelsius(fahrenheit) << std::endl; // Display temp; end line
cout << "Do you wish to convert another temperature (Y/N)?"; // Prompts user if they want convert another temp
cin >> goagain
} while (goagain == 'Y' || goagain == 'y');

return 0; // Indicates that the program ended successfully
} // end function main
Last edited on
closed account (iw7Gy60M)
Are you sure there are no errors? This line:

cin >> goagain

is missing something. It should be a compile error.
Yes I've received nor errors the program will take the temp input and convert but not repeat. I've also tired std::cin << goagain; and cin << goagain; still no change. Any suggestions this program is a final project for my basic C++ programming class.
Latest changes

// INF 231 Week 5 Assignment
// C++ Program to Perform Fahrenheit to Celsius Conversion
#include <iostream> // Allows the program to perform input and output
using namespace std;
using std::cout;
using std::cin;
using std::endl;

double fahrenheitToCelsius(double fahrenheit)
{
double celsius;

celsius = (fahrenheit - 32.0) * 5.0 / 9.0; // Formula to convert fahrenheit to celsius
return celsius;
}
// Function main begins program execution
int main()
{

// Variable declarations
char goagain; // Users input if they'd like to continue
double fahrenheit; // Users temperature input

do...while;
do {
std::cout << "Enter fahrenheit temperature to convert (in degrees) " << endl; // Prompt user for temp in fahrenheit
std::cin >> fahrenheit; // read temp from user
std::cout << "Temperature in Celsius (in degrees) = "; // Display temp converted from fahrenheit
std::cout << fahrenheitToCelsius(fahrenheit) << std::endl; // Display temp; end line
std::cout << "Do you wish to convert another temperature (Y/N)?"; // Prompts user if they want convert another temp
std::cin >> goagain;
} while (goagain == 'Y' || goagain == 'y');
cout << endl;

return 0; // Indicates that the program ended successfully
} // end function main
Yes NewGuy2016 I did have a compile error and was missing (;) after #line 30 of the code.
This corrected build and restart of Microsoft Visual Basic 2016 made the difference.
The corrected code

// INF 231 Week 5 Assignment
// C++ Program to Perform Fahrenheit to Celsius Conversion
#include <iostream> // Allows the program to perform input and output
using namespace std;
using std::cout;
using std::cin;
using std::endl;

double fahrenheitToCelsius(double fahrenheit)
{
double celsius;

celsius = (fahrenheit - 32.0) * 5.0 / 9.0; // Formula to convert fahrenheit to celsius
return celsius;
}
// Function main begins program execution
int main()
{

// Variable declarations
char goagain; // Users input if they'd like to continue
double fahrenheit; // Users temperature input

do {
std::cout << "Enter fahrenheit temperature to convert (in degrees) " << endl; // Prompt user for temp in fahrenheit
std::cin >> fahrenheit; // read temp from user
std::cout << "Temperature in Celsius (in degrees) = "; // Display temp converted from fahrenheit
std::cout << fahrenheitToCelsius(fahrenheit) << std::endl; // Display temp; end line
std::cout << "Do you wish to convert another temperature (Y/N)?"; // Prompts user if they want convert another temp
std::cin >> goagain;
} while (goagain == 'Y' || goagain == 'y');
cout << endl;

return 0; // Indicates that the program ended successfully
} // end function main
New problem I now need to check whether the input is the correct one or not. If the user enters the incorrect number, ask it again. To do this I used a if...else within the do...while loop. However when I enter letters the program performs a infinite loop.

The code

// INF 231 Week 5 Assignment
// C++ Program to Perform Fahrenheit to Celsius Conversion
#include <iostream> // Allows the program to perform input and output
using namespace std;
using std::cout;
using std::cin;
using std::endl;

double fahrenheitToCelsius(double fahrenheit)
{
double celsius;

celsius = (fahrenheit - 32.0) * 5.0 / 9.0; // Formula to convert fahrenheit to celsius
return celsius;
}
// Function main begins program execution
int main()
{

// Variable declarations
char goagain; // Users input if they'd like to continue
double fahrenheit; // Users temperature input

do {
std::cout << "Enter fahrenheit temperature to convert (in degrees) " << endl; // Prompt user for temp in fahrenheit
std::cin >> fahrenheit; // read temp from user
{
if (fahrenheit /= double); // Checks user input is correct
cout << " Enter fahrenheit temperature to convert (in degrees)" << endl; // Prompt user for temp in fahrenheit
std::cin >> fahrenheit; // read temp from user
else
continue;
}
std::cout << "Temperature in Celsius (in degrees) = "; // Display temp converted from fahrenheit
std::cout << fahrenheitToCelsius(fahrenheit) << std::endl; // Display temp; end line
std::cout << "Do you wish to convert another temperature (Y/N)?"; // Prompts user if they want convert another temp
std::cin >> goagain;
} while (goagain == 'Y' || goagain == 'y');
cout << endl;

return 0; // Indicates that the program ended successfully
} // end function main
Please take this advice and edit your post :
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.