Thread: "Help, Please!" or "Can't get any more newbie than me."

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    "Help, Please!" or "Can't get any more newbie than me."

    I'm just trying to learn a little programming for fun. It's not for school or anything else. I'm just bored. Anyway, having never EVER had any instruction in this stuff, I'm trying to pick it up. So, I purchased the software I needed and tried to code my first program.

    My first string of code worked just fine:

    #include <iostream>

    int main()

    {

    std::cout<<"Hello World!";

    return 0;

    }


    Now, the console wouldn't stay open, so I changed the code, after reading this page to read like this:

    #include <iostream>

    int main()

    {

    std::cout<<"Does this compiler work? If so, press any key.";

    cin.get();

    return 0;

    }


    But, I get the following errors:

    Compiling...
    secondprogram.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\secondprogram\secondprogram.cpp( 7) : error C2065: 'cin' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\secondprogram\secondprogram.cpp( 7) : error C2228: left of '.get' must have class/struct/union type
    Error executing cl.exe.

    secondprogram.obj - 2 error(s), 0 warning(s)


    Since I am a total newbie, I have no idea what those errors mean or how to fix them. I even checked these message boards to find a solution and couldn't find anything (I'll blame it on operator error).

    So, can you help me and tell me why it isn't working? Is it me, or are the instructions on that web page wrong? THANKS!

    /me puts on flame proof suit

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    try adding

    #include <stdlib.h>

    i use system("pause") or getch() //getch() is in conio.h...so i dunno try that std header file.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You need to put std:: in front of the cin.get() like you do for the cout.

    OR, if you don't want to put std:: in front of all of your keywords, place "using namespace std;" right after the include iostream.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You're using std:: with the cout but why not with the cin?
    cin is a part of the std namespace as well.

    [edit]

    Pah! damn slowfolk i am!

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Originally posted by PJYelton
    You need to put std:: in front of the cin.get() like you do for the cout.
    AH HA! That worked. Thank you so much!

    Now, can I ask the deeper question: why do you need to put the "std::" in front of stuff? What is that all about?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    It's called a namespace, It's used to seperate things like
    functions and variables with the same name like:

    Code:
    #include <iostream>
    
    using namespace std;
    
    namespace tom
    {
    int age=13;
    }
    
    namespace peter
    {
    int age=14;
    }
    
    int main()
    {
    
    cout << tom::age << endl;
    cout << peter::age << endl;
    
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Originally posted by PJYelton
    OR, if you don't want to put std:: in front of all of your keywords, place "using namespace std;" right after the include iostream.
    Tried this, too, and it also works and seems more elegant to me. You guys are a great help. Thanks.

    I hope you don't mind if a stop by every now and then with my newbie questions.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by animi

    I hope you don't mind if a stop by every now and then with my newbie questions.
    Not at all.

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>I hope you don't mind if a stop by every now and then with my newbie questions.

    Thats what were here for!

Popular pages Recent additions subscribe to a feed