C++ for Dummies
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
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
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
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]
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
Also, I was just wondering, how come a well known book gives you code that doesnt work? :S
Thanks
Ahsan
Change...
#include <stdio.h>
#include <iostream>
to...
#include <iostream>
using namespace std;
Beginners and experts will find answers here.
#include <stdio.h>
#include <iostream>
to...
#include <iostream>
using namespace std;
Beginners and experts will find answers here.
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 .
Because I noticed you spelled celsius correctly everywhere except where you declared it in int celsuis .
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.
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!
Instead of using std:: everyting use the not recommended and lazy approach.
#include <iostream>
using namespace std;
or just use
#include <iostream.h>
#include <iostream>
using namespace std;
or just use
#include <iostream.h>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement