Thread: My first program and it doesn't work.

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    My first program and it doesn't work.

    PHP Code:
        #include <iostream>
        
    int main()
        {
            
    std::cout << "Enter two numbers:" << std::endl;
            
    int v1v2;
            
    std::cin >> v1 >> v2;
            
    std::cout << "The sum of " << v1 << " and " << v2
                      
    << " is " << v1 v2 << std::endl;
            return 
    0;
        } 
    Could anyone tell me what's wrong with that? I've bought a book on C++ and all I get are compiler errors, such as;

    "'cout' is not a member of 'std'"
    "'endl' is not a member of 'std'"
    "'cin' is not a member of 'std'"
    "'cout' is not a member of 'std'"

    Obviously this is related to the 'std'. I've seen online tutorials not use the 'std' at all. Basically what I want to know is how I could fix that program, what 'std' is all about and finally why 'std' is used in some tutorials and not others.

    Any help will be much appreciated.

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    what compiler?

    Try this
    Code:
    #include <iostream>
    using namespace std; 
    
    int main()
    {
       cout << "This should display." << endl; //no need for std::
    
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Wow, that was fast.

    I'm using Bloodshed Dev-C++, is that a problem? I've tried your version of the program and it gave me a whole new set of errors.

    See for yourself, screenshot: http://img149.imageshack.us/img149/8...eenshot1cy.jpg
    Last edited by Magneto; 07-16-2005 at 09:15 AM.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    According to Bjarne Stroustrup, you should try this program to see if the compiler's conforming to the ISO standard. Dev-C++ does, so I have no idea why this doesn't work. Have you tried uninstalling it then reinstalling it - worth a try?

    http://www.research.att.com/~bs/compilers.html
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ooo update: Are you using the Dev-C++ 5 beta? My friend who's also learning to program has informed me that it didn't work; he had similar problems. Get the next v down [v4.9.9.3 if I remember].
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... let's see, did you install the full package, or only the IDE?
    If you dont have a compiler set, you cant compile your programs.

    The version of Dev-C++ that you are using comes with an updated compiler (so the example should work), but if you choose to download and install the Dev-C++ IDE only, then there's no compiler to compile with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Well I only installed Dev-C++ today and I'm running 4.9.9.2 (It's been on my desktop a while). I could try reinstalling, but as I said I've only installed it today...

    Do you think I should try a different compiler?
    hmm... let's see, did you install the full package, or only the IDE?
    If you dont have a compiler set, you cant compile your programs.

    The version of Dev-C++ that you are using comes with an updated compiler (so the example should work), but if you choose to download and install the Dev-C++ IDE only, then there's no compiler to compile with.
    Full package.
    Last edited by Magneto; 07-16-2005 at 09:23 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well I only installed Dev-C++ today and I'm running 4.9.9.2 (It's been on my desktop a while). I could try reinstalling, but as I said I've only installed it today...
    On the Dev-C++ download page, did you download "Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2" (devcpp-4.9.9.2_setup.exe) or "Dev-C++ 5.0 beta 9.2 (4.9.9.2), executable only (2.4 MB)" (devcpp-4.9.9.2_nomingw_setup.exe).

    If you downloaded the latter, you should download the former and do a re-install according to the uninstall/delete/reinstall procedure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Full package.
    Okay, now go to the "Compile Log" tab and post your full compile log.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    I'm quite sure I downloaded ("Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2"). I'm going to unistall this version (whatever it is, I'm not sure) and install (devcpp-4.9.9.2_setup.exe). Just to eliminate that.

    This is what I get when I to compile this program:
    Code:
    #include <iostream>
        int main()
        {
            std::cout << "Enter two numbers:" << std::endl;
            int v1, v2;
            std::cin >> v1 >> v2;
            std::cout << "The sum of " << v1 << " and " << v2
                      << " is " << v1 + v2 << std::endl;
            return 0;
        }
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\prog\ghfgjh.cpp" -o "C:\prog\ghfgjh.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
    C:\prog\ghfgjh.cpp:1:20: iostream: No such file or directory
    C:\prog\ghfgjh.cpp: In function `int main()':
    C:\prog\ghfgjh.cpp:4: error: `cout' is not a member of `std'
    C:\prog\ghfgjh.cpp:4: error: `endl' is not a member of `std'
    C:\prog\ghfgjh.cpp:6: error: `cin' is not a member of `std'
    C:\prog\ghfgjh.cpp:7: error: `cout' is not a member of `std'
    C:\prog\ghfgjh.cpp:8: error: `endl' is not a member of `std'

    Execution terminated
    If I try to compile this program:
    Code:
    #include <iostream>
    using namespace std; 
    
    int main()
    {
       cout << "This should display." << endl; //no need for std::
    
    return 0;
    }
    I get:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\prog\ghfgjh.cpp" -o "C:\prog\ghfgjh.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
    C:\prog\ghfgjh.cpp:1:20: iostream: No such file or directory
    C:\prog\ghfgjh.cpp: In function `int main()':
    C:\prog\ghfgjh.cpp:6: error: `cout' undeclared (first use this function)
    C:\prog\ghfgjh.cpp:6: error: (Each undeclared identifier is reported only once for each function it appears in.)
    C:\prog\ghfgjh.cpp:6: error: `endl' undeclared (first use this function)

    Execution terminated

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    It must have been something to do with my compiler. I've reinstalled Dev-C++ using that guide you gave me. The version I have installed now is, Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2.

    Both programs worked. Thanks a lot everyone!

    I'm curious though, what is 'std::'? What's It's purpose? Why is it used if it's not needed?

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    It's a namespace. Think of it as a wrapper around some classes & functions to ensure no conflicts occur. This menas you could write a function named cout, and place it in a different namespace (or leave it in the global namespace) without fear of it being mis-interpreted by your compiler. I strongly reccommend you don't do that.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by Magneto
    It must have been something to do with my compiler. I've reinstalled Dev-C++ using that guide you gave me. The version I have installed now is, Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2.

    Both programs worked. Thanks a lot everyone!

    I'm curious though, what is 'std::'? What's It's purpose? Why is it used if it's not needed?
    Don't worry about it.. when you get to namespaces youll see why its there

  14. #14
    *this
    Join Date
    Mar 2005
    Posts
    498
    Just so you know this was your error
    C:\prog\ghfgjh.cpp:1:20: iostream: No such file or directory
    So apparantly the packages you got didnt install correctly for some reason.

  15. #15
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by JoshR
    Just so you know this was your error

    So apparantly the packages you got didnt install correctly for some reason.
    Im pretty sure when i had Dev-C++ when i installed it there was something about installing the libraries. Maybe he checked that off or something.

Popular pages Recent additions subscribe to a feed