Public Group

#### Archived

This topic is now archived and is closed to further replies.

# C++ for Dummies

This topic is 5567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i''ve just bought C++ for dummies, and i''m writing out the first piece of code, but it doesnt seem to be working can someone tell me whats wrong with it please? // // Program to convert temperature from Celsuis dgree // units into Fahrenheit degree units: // Fahrenheit = Celsius * (212 - 32)/100 = 32 // #include <stdio.h> #include <iostream> int main(int nNumberofArgs, char* pszArgs[]) { // enter the temperature in Celsius int celsuis; cout <<"Enter the temperature in Celsuis:"; cin >> celsius; // calculate conversion factor for Celsius // to Fahrenheit int factor; factor = 212 - 32; // use conversion factor to convert Celsius // into Fahrenheit values int fahrenheit; fahrenheit = factor * celsius/100 + 32; // output the results cout << "Fahrenheit value is:"; cout << fahrenheit; return 0; } I''m using Dev C++ (instead of the GNU C++ program given with the book.) Thanks Ahsan

##### Share on other sites
The biggest potential problem I see is that Celsius is defined as int. This means that when you go to this statement:
celsius/100
It will very commonly give you 0. The reason? When two integers are divided, the decimal is truncated (dropped off).
You may also want to check over the math. I don''t remember it off the top of my head though.

Hope that helps,
--Brian

##### Share on other sites
quote:
Original post by Wings Of Icarus
Hi, i''ve just bought C++ for dummies, and i''m writing out the first piece of code, but it doesnt seem to be working can someone tell me whats wrong with it please?

//
// Program to convert temperature from Celsuis dgree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212 - 32)/100 = 32
//
#include <stdio.h>
#include <iostream>

int main(int nNumberofArgs, char* pszArgs[])
{
// enter the temperature in Celsius
int celsuis;
cout <<"Enter the temperature in Celsuis:";
cin >> celsius;

// calculate conversion factor for Celsius
// to Fahrenheit
int factor;
factor = 212 - 32;

// use conversion factor to convert Celsius
// into Fahrenheit values
int fahrenheit;
fahrenheit = factor * celsius/100 + 32;

// output the results
cout << "Fahrenheit value is:";
cout << fahrenheit;

return 0;
}

I''m using Dev C++ (instead of the GNU C++ program given with the book.)

Thanks

Ahsan

your problem is pretty simple. You''re using the c++ header iostream and cin/cout are defined in the std namespace. So replace all cin and cout statements by: std::cin and std::cout respectively. Notice the std:: in front of the cin and cout, they tell the compiler to use the std namespace. Oh and no, it''s not an int problem. The int is ok.

[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com][ Administrator & WebMaster GuLSE]

##### Share on other sites
thanks for your replies, but i''ve just realised that the code I was given to put into the compiler had (not iostream as I originally put it.) It still doesnt work though. I''ve tried doing the std:: thing but it still doesnt work. Any more help is greatly appreciated.

Also, I was just wondering, how come a well known book gives you code that doesnt work? :S

Thanks

Ahsan

##### Share on other sites
Change...

#include <stdio.h>
#include <iostream>

to...

#include <iostream>
using namespace std;

Beginners and experts will find answers here.

##### Share on other sites
Is your error along the lines of "undefined symbol celsius" ?
Because I noticed you spelled celsius correctly everywhere except where you declared it in int celsuis .

##### Share on other sites
quote:
Original post by Daerax
Is your error along the lines of "undefined symbol celsius" ?
Because I noticed you spelled celsius correctly everywhere except where you declared it in int celsuis .

Those will get you every time.

##### Share on other sites
quote:
Is your error along the lines of "undefined symbol celsius" ?
Because I noticed you spelled celsius correctly everywhere except where you declared it in int celsuis .

...damn...THAT was the problem...i''m embarrassed now...

Thank you all for your help!

##### Share on other sites
Instead of using std:: everyting use the not recommended and lazy approach.

#include <iostream>
using namespace std;

or just use

#include <iostream.h>

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009849
• ### Who's Online (See full list)

There are no registered users currently online

×