c++ create a program please somebody helpp..

Feb 1, 2012 at 1:39am
closed account (9N7koG1T)
Part 0

Output a prompt using the cerr object that instructs the user to type in a temperature in °F (i.e. Fahrenheit).

Define a variable named tempF of type int.

Read the input into the tempF variable using the cin object.
cin >> tempF;

Define a variable named tempK of type double.

Convert the °F value to Kelvin using the following arithmetic expression storing the result of the expression in the tempK variable.
(tempF + 459.67) * 5 ÷ 9

Use the cout object to print the following output.
tempF degree Fahrenheit in Kelvin is tempK

Example output when the input is 72.
72 degree Fahrenheit in Kelvin is 295.372

Be sure to end the output of this part with a newline.

Part I

Insert the following statement into your program.
cout << 5/9 << endl;

Using only one cout statement, print a short explanation as to why the previous cout statement printed the value 0 (zero).
cout << "5/9 prints 0 because ..." << endl;
Last edited on Feb 1, 2012 at 1:40am
Feb 1, 2012 at 1:49am
What's wrong with your code? Can you tell us what compile errors you're getting, show us the entire code, and tell us which lines are giving you the errors?
Feb 1, 2012 at 2:24am
closed account (9N7koG1T)
I just started to learn this program, I dont want to write here because I'm sure there are lots of errors in code..
Feb 1, 2012 at 2:55am
closed account (9N7koG1T)
If you know the answer please write, I really need to know :( I tkink,it takes a couple minutes for programmer.. please :(
Feb 1, 2012 at 4:26am
We don't give direct answers to homework problems.
http://www.cplusplus.com/articles/DjGEy60M/
Feb 1, 2012 at 4:38am
closed account (9N7koG1T)
ok, I will try to solve then I will write.
Feb 1, 2012 at 5:09am
part 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main(){
    int tempF;
    double tempK;

    cout << "Type a temperature in Fahrenheit and press ENTER: ";
    cin >> tempF;

    tempK = (tempF + 459.67) * 5 / 9;

    cout << tempF << " degree Fahrenheit in Kelvin is " << tempK << endl;

    cin.get();
    return 0;
}

i think you can do part 1
Last edited on Feb 1, 2012 at 5:10am
Feb 1, 2012 at 5:27am
Part 1 is just understanding integer division, which you actually got lucky with in your program due to order of operations. ;)
Feb 1, 2012 at 6:07am
closed account (9N7koG1T)
...
..
.
cout << tempF << " degree Fahrenheit in Kelvin is " << tempK << endl;
cout <<"5/9 prints 0 because in C++ the result of dividing an integer by an integer is truncated to an integer <<

endl;

}

Is that enough? what is these for--cin.get(); and return 0; Do I have to write?
Feb 1, 2012 at 6:36am
It's generally a bad idea to mix cin.get with cin so you should just leave it out and have the program end with "return 0;", which basically ends the main function as false - ending the program.

Your answer to Part 1 looks fine so long as you're formatting it correctly:
1
2
cout << tempF << " degree Fahrenheit in Kelvin is " << tempK << endl;
cout << "5/9 prints 0 because in C++ the result of dividing an integer by an integer is truncated to an integer" << endl;
Last edited on Feb 1, 2012 at 6:38am
Feb 1, 2012 at 4:20pm
@whitewind do you think he's using windows or linux?
Feb 2, 2012 at 12:55am
closed account (9N7koG1T)
I am using windows, if I write this way, any problem?
Feb 2, 2012 at 1:36am
what compiler are you using, dev c++ ?
Feb 2, 2012 at 1:51am
I sure hope neither of you is using Dev-C++:
http://www.cplusplus.com/articles/36vU7k9E/
Feb 2, 2012 at 2:05am
ii usually run linux and when ii use windows ii run qt
but it seems a lot of people use dev when they begin programming
Feb 2, 2012 at 5:15am
closed account (9N7koG1T)
I am using Dev c++
Feb 2, 2012 at 7:07am
Wait. Why is "tempK" double? Surely it should be int?
Feb 2, 2012 at 8:56am
Vortex, degrees K is not an integer. It's 273.15 for the freezing point and conversion from F to K uses a fraction and a decimal real.

Since it's also an SI unit, it should be taken to degree of precision.
Last edited on Feb 2, 2012 at 8:57am
Feb 2, 2012 at 7:20pm
I am using Dev c++


mmhm so you're gonna need to use cin.get() or system("PAUSE") in order to keep the cmd from closing.
Feb 2, 2012 at 9:29pm
cin.clear(); cin.sync(); cin.ignore(unsigned(-1), '\n');
Topic archived. No new replies allowed.