Thread: newbie problem, could someone say what is wrong with this?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    newbie problem, could someone say what is wrong with this?

    Complain:
    lesson2.cpp:9: warning: ISO C++ forbids declaration of ‘main’ with no type
    My Code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct weapons_guns {
      int sniper,assaultrifle,shotgun; // arsenal
    };
    
    main() {
      weapons_guns guns; // activates? the structure weapons_guns as 'guns'
      weapons_guns *ptr; // defines the pointer weapon_guns.ptr or does it define weapons_guns as *ptr?
    
      guns.sniper = 100; // sets the weapons_guns.sniper too 100 ( ment to reflect price )
      ptr = &guns; // sets the pointer(ptr) too weapons_guns aka'guns' structure?
      cout<< ptr->sniper; // takes the output from weapons_guns.sniper with a pointer
                          // does it use 'weapons_guns' as *ptr too call weapons_guns.sniper?
      cin.get();
    }


    Original, compiles fine:
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct xampl {
      int x;
    };
    
    int main()
    {  
      xampl structure;
      xampl *ptr;
      
      structure.x = 12;
      ptr = &structure; // Yes, you need the & when dealing with structures
                        //  and using pointers to them
      cout<< ptr->x;    // The -> acts somewhat like the * when used with pointers
                        //  It says, get whatever is at that memory address
                        //  Not "get what that memory address is"
      cin.get();                    
    }
    as you can see they are very similar, i can't narrow down why I get that error..
    Last edited by Ashii; 11-02-2008 at 04:15 PM. Reason: forgot somethings

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Look at line 9. Notice how it talks about main. Look at the original, and find main there. Notice the difference.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    Whoops!!!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And remember that main shall always return int (explicitly, as well), and never something else like void, like some tend to use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    And remember that main shall always return int (explicitly, as well), and never something else like void, like some tend to use.
    I find this sentence confusing to be honest. It is true that the main function must be declared to return an integer, as stated by the previous poster. It is, however, not required to actually have a return statement in this function.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What I mean is... this is valid:
    Code:
    int main()
    ...this is not...
    Code:
    void main()
    ...nor is this...
    Code:
    main()
    ...or...
    Code:
    char main()
    ...etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Newbie question: Problem with malloc
    By Ikim in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 10:11 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Newbie with minor problem
    By Dell Boy in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2002, 05:07 PM
  5. newbie problem
    By face_master in forum C++ Programming
    Replies: 7
    Last Post: 05-15-2002, 08:56 AM