my n00bish celius converter

Started by
4 comments, last by ontheheap 19 years, 6 months ago
why wont this compile??? im, such a n00b geez....

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
int main()
{
int intemp;
int funcnumber;

cout<<"Welcome to the temperature conveter. \n Press 1 to go to celcius to farenhight \n Press2 to go to Farenhight to celcius. \n";
cin>>funcnumber;
if (funcnumber=1)
{
    cout<<"Thank you, you have chosen 'celcius to farenhight'. Please enter the temperature in celcius you wish to convert \n";
    cin>>intemp;
    cout>>"The temperature in farenhight is" (intemp*1.8)+32;
    system"Pause";
}

cin>>funcnumber;
if (funcnumber=2)
{
    cout<<"Thank you, you have chosen 'farenhight to celcius'. Please enter the temperature in farenhight you wish to convert \n";
    cin>>intemp;
    cout>>"The temperature in farenhight is" (intemp-32)/1.8;
    system"Pause";
}



Advertisement
It would help if you would give us the compiler error - these usually tell you what you're doing wrong. I can tell you that your if statement should be 'if(funcnumber == 1)' instead of 'if(funcnumber = 1)', though it will still compile with this. Also, the line 'cout>>"The temperature in farenhight is" (intemp * 1.8) +32;' should be 'cout << "The temperature in farenheight is: " << inttemp * 1.8 + 32 << endl;', and the following line ('system"Pause";') should be 'system("pause");'. Pretty much everything I just said applies to the second part of the logic (farenheight to celcius). Also, you never closed main's brackets. And it's generally considered nice to have main return 0 at the end (even though there is an implicit return 0 in the standard).

Okay, so here's what I would write for this prog:

#include <iostream>using namespace std;float farenheight_to_celsius(float temp){    return (temp - 32.0f) / 1.8f;}float celsius_to_farenheight(float temp){    return temp * 1.8f + 32.0f;}int main(){    int choice = 0;    while(choice != 1 && choice != 2)    {        cout << "Welcome to temperature converter.\nEnter 1 for C to F or 2 for F to C: ";        cin >> choice;    }    float temp;    cout << "Please enter a temperature to convert: ";    cin >> temp;    if(choice == 1)        cout << temp << " degrees celsius is " << celsius_to_farenheight(temp) << " degrees farenheight." << endl;    else // choice == 2        cout << temp << " degrees farenheight is " << farenheight_to_celsius(temp) << " degrees celsius." << endl;    cin.get();    return 0;}


"There is no dark side of the moon really,
As a matter of fact, its all dark."
well.... it looks like in one spot... you have...
if (funcnumber = 1) I think it should read....

if (funcnumber == 1)

when you convert.... i think you need....

cout << "Thetemperature in farenhight is " << (intemp*1.8)+32;

not sure though. Then.... Maybe you should have...

System("Pause"); //???

same stuff for the second if... make sure the << and >> are going in the right direction. also, = and == are different in that the first is an assignment operator, IE assigning the right variable to the left variable. Whereas, == is the equality operator. Which checks if the two are equal or not.

Good luck!
Hehe, we must think alike! I'm in the very n00b stages of learning C++ too, and I also just attempted to make a Farenheit to Celcius converter for practice :)
First, check your logic. You are trying to read input, check if it's equal to 1, and if so, do the first calculation, then read input a second time, check if it's equal to 2, and if so do the second calculation. That's clearly not what you want. Of course, the compiler doesn't care. (The compiler also doesn't know or care about the correct spelling of "fahrenheit", but it's worth knowing, if only to appear more educated. ;) ) It won't cause a failure to compile.

Next, watch your = vs ==, as mentioned by the other poster. This won't make it fail to compile either, so you have to be careful about these things.

Next, 'system' is a function call, so you need to put parentheses around its argument ('"pause"'). This *will* be flagged as a compiler error. As homework, identify the error(s) this caused, and figure out why the messages say exactly what they say. Use a dictionary if necessary.

Next, you need to put the angle brackets between every item you want to feed to cout or cin. Adjacent *literal strings* get pasted together implicitly, but nothing else does:

cout << "hello" " " "world" << endl;
// "Ok, "hello world" is a single string, and I want to output that, then an endl.

cout << "hello" fnord;
// Ack! Don't you see the fnords? Error! Error!

cout << "hello" << fnord;
// OK, assuming 'fnord' is a variable in your program.

That causes a compiler error as well. Again, try to figure out what it caused, and why it says exactly what it does.

Finally, since you seem to be doing pretty well at sticking to C++ style (so far)... use the blasted 'endl' already. \n is so passe. :)

If you want even more practice (as well as a more useful application) you should have the app take command line arguments. For instance:

> convert -c 212
100

This would convert from f to c.

This topic is closed to new replies.

Advertisement