Click to See Complete Forum and Search --> : "C++ for dummies" error in source code??


bit4bit
July 4th, 2007, 11:14 AM
Hello all, I'm very new to C++ and just started reading "C++ for dummies". Which is quite good but I have got a small problem with:

Basically the book provides you with little bits of source code as you go along, which you compile with 'Dev C++' to see how it works. The first example is a program to convert degrees celcius to degrees farenheit, and here is the source code for it:


//
// 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;
}

My problem is, when I compile it, this appears on my screen:

[img=http://img262.imageshack.us/img262/1086/devcerrormb3.th.jpg] (http://img262.imageshack.us/my.php?image=devcerrormb3.jpg)

Is this an error in the code? - Have they published bad code in their book?? Or maybe there is some Dev C++ setting that I have set incorrectly? (though it certainly looks like an error even to me)

The accompanying CD provides all the .cpp files for each bit of source code, but I have lost it, and haven't been able to find another.
Is this code really incorrect?

Thanks alot for any help.
-bit4bit

P.S Sorry if this is in the wrong forum, as I said I'm a complete beginner and I didn't know the difference between them :P
EDIT 2: I think this should be in the 'Visual C++ forum'?

wildfrog
July 4th, 2007, 11:51 AM
Based on the error list it seems that the is an invalid 'double quote' in your code.

The are more than one 'double quote' in your character table, and I assume your compiler only recognize one of them (the one with ASCII code 34). Maybe you should try to reenter the double quotes...

- petter

manchukuo
July 4th, 2007, 11:53 AM
just change this line

int main(int nNumberofArgs, char* pszArgs[])

to

void main()
_________

the int main means that the function returns a value and the (int nNumberofArgs, char* pszArgs[]) means that your giving him arguments, this is like when you do a dir /p on command line the "/p" is the argument

bit4bit
July 4th, 2007, 12:02 PM
Thanks alot, I got it working now by re-entering the quotes. Lol - how weird that doing that should make a difference. You can probably tell I have alot to learn about this hence 'C++ for dummies' ;)

Manchukuo - Thanks, but I want to keep the original source code, because now I have to read about what it all does... :)

STLDude
July 4th, 2007, 12:10 PM
just change this line

int main(int nNumberofArgs, char* pszArgs[])

to

void main()
_________

the int main means that the function returns a value and the (int nNumberofArgs, char* pszArgs[]) means that your giving him arguments, this is like when you do a dir /p on command line the "/p" is the argument

Except, it's still should be int void()

wildfrog
July 4th, 2007, 04:59 PM
just change this line

int main(int nNumberofArgs, char* pszArgs[])

to

void main()
The origianl is perfectly correct c++. The one you mentioned is not. main should always return int.

- petter

bit4bit
July 4th, 2007, 06:56 PM
The origianl is perfectly correct c++.

I can say that the program compiled and ran correctly after I corrected those quotes.

I'm now reading through the book, learning about different variable types :P , but it's annoying without the cd I must say.

Paul McKenzie
July 7th, 2007, 10:49 PM
the int main means that the function returns a value and the (int nNumberofArgs, char* pszArgs[]) means that your giving him arguments, this is like when you do a dir /p on command line the "/p" is the argumentThis is absolutely false and misleading!!

For a standard C++ program, the correct prototype for main() is returning an int. Not void, or anything else.

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3

Here is the inventor of the C++ language stating the same thing:
http://www.research.att.com/~bs/bs_faq2.html#void-main

Regards,

Paul McKenzie

ne0n82
July 7th, 2007, 11:23 PM
i believe that void main(void) is an old throwback from C language and it will still work because if your compiler is compiling c++ it should be smart enough to change it to int and add in a default return 0; but its still very poor practice

Paul McKenzie
July 8th, 2007, 04:21 PM
i believe that void main(void) is an old throwback from C languageRead the Stroustrup link in my post. It isn't even valid 'C'.

Regards,

Paul McKenzie

NMTop40
July 9th, 2007, 10:39 AM
The OP's signature of main is correct, there is no law that the arguments must be called argc and argv, although that is a common convention. There is also no law that just because the signature is used that accepts command-line arguments, that there have to be any.