Hey all, i'm a freshmen in college who is taking Intro to Computer Science with C++ and i'm just a beginner when it comes to programming so i have a problem. This is our assignment: You are to write a program which inputs a Celsius temperature from the user, converts it to Fahrenheit and displays both temperatures on the screen. Allow the user to enter the temperature as a real number such as 56.5. Show the temperatures with 1 digit after the decimal point. This is what i got so far:

// This program is going to convert Celsius temperatures to Fahrenheit temperatures

// The user will provide the celsius degrees temperature.
// The program will show the celsius degree temperature and convert it to fahrenheit 
// temperature while showing both the celsius and fahrenheit temperatures.

#include <iostream>
#include <iomanip.h>

using namespace std;

int main()
    double Celsius;        // Number of Celsius degrees
    double Fahrenheit;     // Number of Fahrenheit degrees
    // Explain the program to the user
    cout << "This program will calculate the Celsius and Fahrenheit temperatures.\n";
	cout << "You must provide the Celsius temperature.\n";
	cout << endl;
    //  Get information
    //  Get numbers for Celsius degrees
   	cout << "Enter the number of Celsius degrees: ";
	cin >> Celsius;

    //  Perform calculations
    //  Convert Celsius to Fahrenheit
    //Celsius to Fahrenheit= F = C * 9 / 5 + 32;

    //  Display results
    //  Display Celsius
	cout << endl;
	cout << "Celsius:  " << Celsius << " degrees" << endl;
    //  Display Fahrenheit
	cout << setiosflags(ios::fixed) << setprecision(2);
    cout << "Fahrenheit:  " << Fahrenheit << " degrees" << endl;
    return 0;

I was wondering what else I need to put in here so i can have fahrenheit answers like 56.5 instead of just 56, thanks alot for helping out a poor freshman.

8 Years
Discussion Span
Last Post by cw1184

First, drop the ".h" from <iomanip>

As written, your code will show two decimal places for the Fahrenheit output (which had no value at this point), but will use C++'s default display for your redisplay of the Celsius. Put the two formatting statements before any output.

To see only one decimal digit, you need to specify precision of 1, not 2. When you've set ios::fixed, precision then refers to how many digits follow the decimal. Otherwise, it controls overall precision of the displayed values.


Thanks alot guys, I really appreciate it. I'm sorry for the stupid questions though.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.