Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: simple little C++ program not working....

  1. #1

    simple little C++ program not working....

    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!
    What do you get when you cross a nun and a penguin?
    An operating system that won\'t go down on you.

  2. #2
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    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.

  3. #3
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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:

    Code:
    #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;
    }
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  4. #4
    Hmm weird, ok, it just worked for windows, so i got confused when it wouldnt compile for linux, thanks a lot
    What do you get when you cross a nun and a penguin?
    An operating system that won\'t go down on you.

  5. #5
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  6. #6
    Junior Member
    Join Date
    Dec 2003
    Posts
    1
    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).

  7. #7
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    Code:
    #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.
    Paul Waring - Web site design and development.

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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:

    Code:
    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
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  9. #9
    Junior Member
    Join Date
    Dec 2003
    Posts
    9
    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.
    There are 10 kinds of people in this world: those who understand binary, and those who don\'t.

  10. #10
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    If I recall correctly, the ANSI standard states that C such be of type int and either of the following two ways are valid:

    Code:
    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:

    Code:
    return 0;
    at the end of the main() function.
    Paul Waring - Web site design and development.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •