Thread: Easy noob question (probobly) :)

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    Thumbs down Easy noob question (probobly) :)

    Is there anything wrong with this code, I keep getting error messages when I try to compile it:

    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main;
    
    {
        int n
        
        cout<<"5+1= ";
        cin>> n;
        cin.ignore();
        if ( n = 6 ) {
             cout<<"Good Job! That was fun right?"
             }
        
        cin.get();
    }
    Update: i get an error on line 7, It says

    invalad function direction

    Hmmmmmm
    Last edited by aaronson2012; 04-10-2008 at 05:14 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    There are several problems in your code, but they are all easy to fix.

    First,
    "int n" should be follow by a ";"

    second,
    = means to assign something, while == is to compare.
    Try changing if(n = 6) to if(n==6)

    third,
    int main() should return something, Try adding return 0 at the end of main. Also, main is a function, and should not have ; at the end

    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main
    {
        int n;
        
        cout<<"5+1= ";
        cin>> n;
        cin.ignore();
        if ( n == 6 ) {
             cout<<"Good Job! That was fun right?";
             }
        
        cin.get();
    return 0;
    }
    Last edited by h3ro; 04-10-2008 at 05:09 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> also, int main should return something. Try adding return 0 at the end of main
    That's not actually necessary, there's a special rule with main that says it's ok not to. The code is fine (except for the == thing).

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Three problems, highlighted in bold and red:

    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main;
    
    {
        int n;
        
        cout<<"5+1= ";
        cin>> n;
        cin.ignore();
        if ( n == 6 ) {
             cout<<"Good Job! That was fun right?";
             }
        
        cin.get();
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by h3ro View Post
    There are several problems in your code, but they are all easy to fix.

    First,
    "int n" should be follow by a ";"

    second,
    = means to assign something, while == is to compare.
    Try changing if(n = 6) to if(n==6)

    third,
    int main() should return something, Try adding return 0 at the end of main. Also, main is a function, and should not have ; at the end

    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main()
    {
        int n;
        
        cout<<"5+1= ";
        cin>> n;
        cin.ignore();
        if ( n == 6 ) {
             cout<<"Good Job! That was fun right?";
             }
        
        cin.get();
    return 0;
    }
    That doesn't compile either because you forgot the part in red.
    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.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I personally feel that the standard should ensure main returns a value.

    You explicity say int main(), so in essence, you are telling the function that it will return an integer value when the function terminates. If main is ok to not return a value, then why do some compilers throw an error when you use void main()? seems like it is a bit like double standards.
    Double Helix STL

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    main() must return a value - it's just that the compiler is required to implicitly add a return 0 if you don't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I personally feel main should be void, just like how it is in Java, for the sake of portability. But, of course, there is C compatibility to worry about, too.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    C compatibility would be easily handled by allowing main to be int, but making the canonical form void.

    A more interesting question is the effect that calling exit() would have on local objects with destructors.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cyberfish View Post
    I personally feel main should be void, just like how it is in Java, for the sake of portability. But, of course, there is C compatibility to worry about, too.
    I feel that would be stupid seeing as how it would break 99% of the programs out there written for platforms with CLI in mind or otherwise, especially since they can return a value telling of success or failure or whatever.
    I feel Java is wrong. A return code can tell a thousand words.
    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. Noob here...a simple question.
    By The_Mayor in forum C Programming
    Replies: 4
    Last Post: 06-08-2005, 12:48 AM
  2. Quick Easy Question
    By St0rmTroop3er in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-24-2004, 01:08 AM
  3. Easy Question
    By Bryan in forum C++ Programming
    Replies: 15
    Last Post: 10-17-2002, 04:25 PM
  4. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM