PDA

Click to See Complete Forum and Search --> : simple little C++ program not working....


Talith
November 25th, 2003, 12:36 AM
Ive been able to get this working in Windows, but once i go into Linux, it doesnt work =/.

#include <iostream.h>


void main(void)
{
char first_name[64];
char last_name[64];

cout << "Type your first and last name: ";
cin >> first_name >> last_name;

cout << "Hey, " << first_name << " " << last_name;
}


Thats what I have, straight out of the book, It works in windows, but when i do $gcc name name.cpp

it says this

In file included from /usr/include/g++/backward/iostream.h:31,
from name.cpp:1:
/usr/include/g++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
name.cpp:5: error: `main' must return `int'

And i saw the main must return int, so i did return 0; after my last cout statement, but it still did nothing, are there different ways to write C++ in windows/linux? What am i doing wrong?!? >.<

thanks guys!

HTRegz
November 25th, 2003, 01:52 AM
I'm by no means a c++ coder.. but what the error is telling you is that your #include <iostream.h> line is wrong.... the new way of doing things is #include <iostream>
If you would like to prove this you could look in /usr/include/c++/<version>/ and you will see iostream but no iostream.h. I believe you will probably also have to include the line using namespace std; to code this properly .. however I'm not entirely sure on that one. just the way I understand it.. There are many skilled c++ coders.. check the tutorials forum and you'll see the basics of how to code things.

White_Eskimo
November 25th, 2003, 03:29 AM
wow cool you got an error like that!! i have personally never seen one cause i work with a new version of codewarrior, but HTRegz is right...change your code to look like this and it should work:


#include <iostream>
using namespace std;


void main(void)
{
char first_name[64];
char last_name[64];

cout << "Type your first and last name: ";
cin >> first_name >> last_name;

cout << "Hey, " << first_name << " " << last_name;
}

Talith
November 25th, 2003, 03:31 AM
Hmm weird, ok, it just worked for windows, so i got confused when it wouldnt compile for linux, thanks a lot :D

White_Eskimo
November 25th, 2003, 03:50 AM
well your problem is that you were using two different compilers, and the one on the windows machine supports both the new and old standard format and the linux compiler just supports the new format.

echolyean
December 6th, 2003, 02:59 AM
The problem is not the fact you have <iostream.h> (although it should be changed to <iostream>, because that is deprecated); the real problem (and where the error lies) is that you aren't returning int... so void main (void) should be int main (void).

pwaring
December 6th, 2003, 04:09 AM
#include <iostream>

int main()
{
char first_name[64];
char last_name[64];

std::cout << "Type your first and last name: ";
std::cin >> first_name >> last_name;

std::cout << std::endl << "Hey, " << first_name << " " << last_name << std::endl;
}


The above code compiles on both Windows (Dev-C++) and Linux (g++) for me.

White_Eskimo
December 6th, 2003, 05:06 AM
the real problem (and where the error lies) is that you aren't returning int... so void main (void) should be int main (void).

Err...no there is no problem there. if you are not returning anything then you function should be set as void, and if you are returning an integer then it should be set as int. you can however return nothing in a void function just to make it exit the function like this example:


void myFunction (int n)
{
cout << "On a scale of one to ten, how lame is this function?";
cin >> n;
if (n<5)
{
cout << "Wow you are a dumb user...DIE!!!";
while (1)
myfunction(1000);
}
else
{
return;
}
}
/*
there was no need to do allow myFunction to take in an integer n, but i thought it would be more realisitic although it doesnt matter because the user is in control...
*/


Anyways just wanted to show you :)

parz
December 10th, 2003, 11:34 PM
I typed the code as follows:

#include <iostream>
using namespace std;

int main()
{
char first_name[64];
char last_name[64];

cout << "Type your first and last name: ";
cin >> first_name >> last_name;

cout << "Hey, " << first_name << " " << last_name;
return 0;
}
--------------------
Then compiled using g++ as follows:
g++ -o hello hello.cpp
-----------------
and it ran just fine.


By default - main must be of type int.

pwaring
December 11th, 2003, 11:30 AM
If I recall correctly, the ANSI standard states that C such be of type int and either of the following two ways are valid:


int main()
int main(int argc, char **argv)


As far as I know, void main() is illegal in the C++ standard and so shouldn't be used. Your compiler might let you get away with it (I think Dev-C++ does) but it's technically incorrect. The reason for this is that your program should return a value (of type int) to the operating system to signal that it completed successfully or not.

Oh, and my original code should have had the line:

return 0;

at the end of the main() function. :)

latiff
February 21st, 2004, 02:53 AM
Well i allways do it like this.
#include <iostream>
using namespace std;


int main()
{
char first_name[64];
char last_name[64];

cout << "Type your first and last name: ";
cin.getline(first_name, 64, '\n');

cin.getline(last_name, 64, '\n');
cout << "Hey, " << first_name << " " << last_name;

return 0;
}