[Dev-C++] Re: new comer to the world of Dev-C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ray K. <he...@er...> - 2003-12-02 07:42:26
|
I see a couple of problems with that code:
1) <iostream.h> is depreciated in C++. C standard libraries still have th=
e=20
.h appended to the include file, but for C++, just have #include <iostrea=
m>
2) You need to include the line "using namespace std;" after including=20
iostream. I'm not actually sure why (I usually work in pure C rather than=
=20
C++), but it's a necessary line. Maybe someone else here can explain what=
=20
that does.
3) You're missing a main function. All C/C++ programs need to call main()=
=20
before anything else. Rename square() to main() and you'll be fine. main=20
also has to be declared as an int rather than a void.
4) Because main is returning an int now, change "return" to "return 0" (0=
=20
indicates that the program has successfully terminated)
5) This isn't really an error, but it's generally a good idea to declare=20
all (local) variables at the start of the relevant function. This helps=20
make the code more readable. Move int num up a line.
So we end up with this:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "hello, you are now in hadi's program.\n";
cout << "enter a number and I will give you its square.\n";
cin >> num;
cout << "the square of " <<num<<" is : "<<num * num << endl;
return 0;
}
"hmbayati" <hmb...@sb...> wrote:
>
> Hi everyone,
>
> I am a new comer to the world of the Dev-C++ and C++ in general. I am
> trying to understand the GUI but I am not successful. I compiled and ru=
n
> a very simple program like
>
> #include <iostream.h>
>
> void square()
> {
> cout << "hello, you are now in hadi's program.\n";
> int num;
>
> cout << "enter a number and I will give you its square.\n";
>
> cin >> num;
>
> cout << "the square of " <<num<<" is : "<<num * num << endl;
>
> return;
> }
>
> I get a warning but when I run it I do not get any results. Could
> someone tell me what I am doing wrong?
>
> I am on XP home edition and I set the project for WIN 32 console.
>
> Thanks for your help,
>
> HB
|