| View previous topic :: View next topic |
| Author |
Message |
raigxi Newbie cheater
Reputation: 0
Joined: 16 Feb 2007 Posts: 22
|
Posted: Wed May 07, 2008 1:29 am Post subject: [Help] Regarding C++ (currently wanting to learn) |
|
|
Ok, I started reading through the book C++ for dummies (which tells you all you need to know, and why ) anyway the first bit of code I get to I get an error where I'm not supposed to and so i can't compile ... heres the code.
| Code: |
//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212 - 32)/100 + 32
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// enter the temperature in Celsius
int celsius;
cout << “Enter the temperature in Celsius:”;
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 (followed by a NewLine)
cout << “Fahrenheit value is:”;
cout << fahrenheit << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system(“PAUSE”);
return 0;
} |
I get an error on the line
| Code: | | cout << “Enter the temperature in Celsius:”; |
And I'm meant to change >> to >>> but I don't get an error on that :s, the Thing is I copied straight from the book and read a bit further and thats not meant to be 1 of my problems.
That's what stopped me trying a few weeks back and just want to WANT to get into it again.
Any advice? (friendly advice :p) |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed May 07, 2008 1:40 am Post subject: |
|
|
| replace the “ with " |
|
| Back to top |
|
 |
Typhoon808 Expert Cheater
Reputation: 0
Joined: 27 Mar 2008 Posts: 175 Location: Wales
|
Posted: Wed May 07, 2008 3:28 am Post subject: |
|
|
| Yeah it could be the quotes. What does the error say? |
|
| Back to top |
|
 |
raigxi Newbie cheater
Reputation: 0
Joined: 16 Feb 2007 Posts: 22
|
Posted: Wed May 07, 2008 8:01 am Post subject: |
|
|
| Theres just a little red circle with a cross next to the line, i'll try replacing the quote thing soonish, I'm using Dev C++ |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed May 07, 2008 8:30 am Post subject: |
|
|
In Dev-CPP, there's an error box at the bottom that should tell you what the error is.
The big error looks like those quotes. They shouldn't look like that on the forum lol. _________________
|
|
| Back to top |
|
 |
raigxi Newbie cheater
Reputation: 0
Joined: 16 Feb 2007 Posts: 22
|
Posted: Wed May 07, 2008 9:20 am Post subject: |
|
|
Thats what i get.
I'm considering re-installing from scratch to double check i did everything right. |
|
| Back to top |
|
 |
Psy Grandmaster Cheater Supreme
Reputation: 1
Joined: 27 Mar 2008 Posts: 1366
|
Posted: Wed May 07, 2008 9:24 am Post subject: |
|
|
Yeah I can see a few syntax errors in that, which is why its throwing so many errors.
Re-write it, and pay particular attention to the ends of lines and declarations of variables.
Theres no hidden problem here, and a re-install of Dev won't help.
Just re-do it, and follow the exact method it shows in the book.
I can tell you know, that parts of that code are how dev has automatically pre-structured it, not what you have typed in  |
|
| Back to top |
|
 |
raigxi Newbie cheater
Reputation: 0
Joined: 16 Feb 2007 Posts: 22
|
Posted: Wed May 07, 2008 9:54 am Post subject: |
|
|
Thanks , I'll have another crack and pay more attention shortly  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed May 07, 2008 12:29 pm Post subject: |
|
|
Heres an example for doing this with some code suggestions:
| Code: | #include <cstdio>
#include <cstdlib>
#include <iostream>
//
// Unreferenced Parameter (Define Taken From WinNT.h)
//
#define UNREFERENCED_PARAMETER(P)(P)
int main( int argc, char* argcv[] )
{
//
// These Are Not Used So Unreference Them
//
UNREFERENCED_PARAMETER( argc );
UNREFERENCED_PARAMETER( argcv );
//
// Get Celsius Temp From User
//
int iTempCelsius = 0;
std::cout << "Please enter a degree, in Celsius: ";
std::cin >> iTempCelsius;
//
// Convert To Fahrenheit
// F = C * (212 - 32)/100 + 32
//
int iTempFahrenheit = 0;
iTempFahrenheit = iTempCelsius * (212 - 32) / 100 + 32;
//
// Output To User
//
std::cout << iTempCelsius << " degrees Celsius converts to " << iTempFahrenheit << " degrees Fahrenheit." << std::endl;
//
// Pause Console Before Returning
//
std::cout << "Press enter to close this window...";
std::cin.sync();
std::cin.ignore();
return 0;
} |
My suggestions:
- Don't use 'using namespace std;' There is no point to include the entire namespace when you are only using it for cin and cout. Instead, just add 'std::' to the front of the cin and cout alls.
- When you define a variable, you should always initialize it. Meaning, instead of doing:
int iIntegerVar;
You should do:
int iIntegerVar = 0;
0 or NULL (which is also 0) are the typical things you will see when variables are defined.
- You can do the whole conversion code on 1 line. As your math teacher probably taught you in 2nd or 3rd grade, math follows an order of operations to handle things. Certain symbols have more priority to be handled first then others. The bases would be:
- Handle anything inside parenthesis first.
- Handle multiplication and division, from left to right, next.
- Handle addition and subtraction, from left to right, next.
So this would be handled as:
212 - 32 first
Celsius value * 180 next.
(Celsius value * 180) / 100 next.
That sum, + 32 last.
- Lastly, to pause the console, I highly recommend you do not use system("PAUSE"); along with that, I suggest you avoid calling system() for anything at all. Instead you can achieve the same effect several different ways. My preferred method to do it is:
std::cin.sync();
std::cin.ignore(); _________________
- Retired. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed May 07, 2008 2:49 pm Post subject: |
|
|
..and continues:
- Make sure that user inputs numbers.
- Return EXIT_SUCCESS instead of 0. |
|
| Back to top |
|
 |
GMZorita Grandmaster Cheater Supreme
Reputation: 0
Joined: 21 Mar 2007 Posts: 1361
|
Posted: Wed May 07, 2008 2:53 pm Post subject: |
|
|
Wiccam: Just use using namespace std; instead of adding the std::
Edit: i know you know about that i just want to know why you used std:: instead of using namespace std; |
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Wed May 07, 2008 4:31 pm Post subject: |
|
|
What if you have other namespaces using the same thing but with different functionality? The compiler will be able to tell the difference if you append the namespace, else, it will not be able to and you will get an error.
Example:
| Code: |
#include <iostream>
int main()
{
int cout = 0;
std::cout << "cout is: " << cout << std::endl;
return 0;
}
|
This will compile.
| Code: |
#include <iostream>
using namespace std;
int main()
{
int cout = 0;
cout << "cout is: " << cout << endl;
return 0;
}
|
This will not compile.
If you want to use using, just put what you want to use.
| Code: |
using std::cout;
using std::endl;
|
It's better practice to use what you are using instead of using everything if you are not going to use it. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu May 08, 2008 7:06 am Post subject: |
|
|
| GMZorita wrote: | Wiccam: Just use using namespace std; instead of adding the std::
Edit: i know you know about that i just want to know why you used std:: instead of using namespace std; |
I know what it is and how to use it, I suggest not using though since all it does is creates a lazy programmer. Using a full namespace for the use of two functions inside of it is pointless. As Killer pointed out, it's a bad habbit that you should avoid doing. The std namespace is HUGE. There is no reason to include the full thing if you are using it simply for cout and cin.
| Jani wrote: | ..and continues:
- Make sure that user inputs numbers.
- Return EXIT_SUCCESS instead of 0. |
I didn't check for numbers since I was just converting his code to a bit nicer format. Didn't bother to get into full detail about things like that.
As for returning EXIT_SUCCESS, no point, since it is also 0.
WinError.h:
| Code: | | #define ERROR_SUCCESS 0L |
_________________
- Retired. |
|
| Back to top |
|
 |
raigxi Newbie cheater
Reputation: 0
Joined: 16 Feb 2007 Posts: 22
|
Posted: Thu May 08, 2008 10:34 am Post subject: |
|
|
Thanks everyone:D and thanks wiccaan, I'd preffer not to be a 'lazy programmer', it makes people seem more intellectual  |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri May 09, 2008 5:30 am Post subject: |
|
|
| GMZorita wrote: | | Edit: i know you know about that i just want to know why you used std:: instead of using namespace std; | What if you happen to have a function called eg. (your own) cout function in the global namespace already, and then you want to call the std::cout, not your own? Meh, Iono if you understood, but whatever, I've never been good at explaining stuff :P
| Wiccaan wrote: | | I didn't check for numbers since I was just converting his code to a bit nicer format. Didn't bother to get into full detail about things like that. | The guy was asking hints, not you :P
| Wiccaan wrote: | | As for returning EXIT_SUCCESS, no point, since it is also 0. | Well, it's almost always 0, but not on every single platform (If I recall correctly, Google for "VMS"). There goes your portability... |
|
| Back to top |
|
 |
|