C++ newbie program error

This is a discussion on C++ newbie program error within the C++ Programming forums, part of the General Programming Boards category; i can' t understand the STUPID mistake! can anyone help? Code: #include <iostream> main(){ int x; cin >> x; cout ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    C++ newbie program error

    i can' t understand the STUPID mistake! can anyone help?

    Code:
    #include <iostream>
    
    main(){
        int x;
            cin >> x;
            cout << "My Second program in C++";
    return 0;
    }
    program.cpp:5: error: ‘cin’ was not declared in this scope
    program.cpp:6: error: ‘cout’ was not declared in this scope

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Code:
    #include <iostream>
    
    int main()
    {
        int x;
        std::cin >> x;
        std::cout << "My Second program in C++";
        return 0;
    }
    Fixed parts in red and indentation.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    ok! thanks elysia...

    Well,
    the first "int" i think is not necessary although i add it!
    Secondly i have a question about the other two...red marks. I have done one or two other programs correctly and i remember now that i put something like "namespace std" after main. But, i have no idea what does this mean...can you help me a bit?

    thank you!!

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    your first integer is not neccesary here no, other than a little practice "declaring a variable"

    Code:
    using namespace std;
    see this thread
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by brack View Post
    ok! thanks elysia...

    Well,
    the first "int" i think is not necessary although i add it!
    Secondly i have a question about the other two...red marks. I have done one or two other programs correctly and i remember now that i put something like "namespace std" after main. But, i have no idea what does this mean...can you help me a bit?

    thank you!!
    If you mean the int before main, then it is absolutely necessary. The standard demands that every function has a return type and that main shall return int.
    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
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by Elysia View Post
    If you mean the int before main, then it is absolutely necessary. The standard demands that every function has a return type and that main shall return int.
    i think you are wrong cause even without "int" the porgram compiles and is running correctly...
    also, the default return type of main is "int"...

    HOWEVER i always write
    Code:
    int main(void){
    }
    :P
    i just forget it in my previous program!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by brack View Post
    i think you are wrong cause even without "int" the porgram compiles and is running correctly...
    Then you are using a non-standard compliant compiler. Time to switch.

    also, the default return type of main is "int"...
    No, it does not. C++ requires that all functions have an explicit return type. There is no implicit main as in C90.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    And i WAS so sure that i was right!! Nevermind, thanks "Elysia"!!!
    So, in C++ programming you need to put "everything"...

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Yes, you do. And thank goodness for that!
    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.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > i think you are wrong cause even without "int" the porgram compiles and is running correctly...
    The first lesson to remember is that "my compiler does..." is no substitute for reading the standards.

    EVERY compiler has idiosyncrasies which fall outside the strict interpretation of the standards. This is fine, so long as you know which rules you are breaking and why. But blindly accepting the "blessing" of your current compiler as being "the truth" will lead to trouble in the long run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Correct !!! a programmer should never say: "My program is correct as the compiler shows no errors..."!

    I know this salem, i just wanted to say another reason(which probably was not so right)...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Libraries in C
    By TheOriginalGame in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 12:19 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  5. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21