Jump to content

Help with C++


gEEEk
 Share

Recommended Posts

Hey guys.

I've started learning some C++ and I've got a few questions about a small program I'm writing.

It's a Win32 console application.

- The first line, "#include <iostream>" why is that important? What is its purpose? And why iostream?

- Aswell as the the "using namespace std;" and the "int main()" What is their purpose?

And, when I try to run the program, just by simply double clickin it on the desktop, it opens and closes after the user inputs. How can I correct this? I can however run it completly fine if I'm using cmd.

#include &lt;iostream&gt;

using namespace std;

int main()
{

    int UsersAge = 0;
    
    cout &lt;&lt; "What is your age? ";
    cin &gt;&gt; UsersAge;
    
    if ((UsersAge &gt;=0) &amp;&amp; (UsersAge &lt;=4))
    {
    cout &lt;&lt; "You are a baby! HAHAH!";
    }     
    else
        if ((UsersAge &gt;=4) &amp;&amp; (UsersAge &lt;=10))
        {
        cout &lt;&lt; "You are child! HAHAH!";
        }
    else
        if ((UsersAge &gt;=10) &amp;&amp; (UsersAge &lt;=20))
        {
        cout &lt;&lt; "You are youngster!;
        }
    else
        if ((UsersAge &gt;=20) &amp;&amp; (UsersAge &lt;=60))
        {
        cout &lt;&lt; "You are an adult!";
        }
    else
        if ((UsersAge &gt;=60) &amp;&amp; (UsersAge &lt;=140))
        {
        cout &lt;&lt; "You're old!";
        }
    else
        if ((UsersAge &gt;=140))
        {
        cout &lt;&lt; "You're lying!";
        }
                
    cout &lt;&lt; " Because you are " &lt;&lt; UsersAge &lt;&lt; " years old!"; 
              
}

Link to comment
Share on other sites

Hey man, I am also learning C++ myself. I'm farther than you so I can help you out =D. the line "#include iostream" allows you to input and output things in the program. It imports the library for you to do so. Try deleting the line and see what errors you get in your compiler. "int main()" is a function. You need "int main()" to begin your program with, but you can divert to other functions in your code too. Don't worry about it for now. Its way ahead of where you are at. "using namespace std" deals with classes, I think. I haven't gotten to that part yet so I can't really help explain what it is. I'm doing classes next chapter =D. If you want to find out more about them heres a link:

http://forums.devshed.com/c-programming-42...mean-45679.html

The reason your program closes when you double click the desktop is because the program has ended and the command prompt has no reason to stay open anymore. To fix this you simply ask for a variable at the end of your program. "cin" does not see spaces or enter as a character so you must use "cin.get(variable)". I added that to the end of your program as an example.

#include &lt;iostream&gt;
using namespace std;

int main()
{

    int UsersAge = 0;
    
    cout &lt;&lt; "What is your age? ";
    cin &gt;&gt; UsersAge;
    
    if ((UsersAge &gt;=0) &amp;&amp; (UsersAge &lt;=4))
    {
    cout &lt;&lt; "You are a baby! HAHAH!";
    }    
    else
        if ((UsersAge &gt;=4) &amp;&amp; (UsersAge &lt;=10))
        {
        cout &lt;&lt; "You are child! HAHAH!";
        }
    else
        if ((UsersAge &gt;=10) &amp;&amp; (UsersAge &lt;=20))
        {
        cout &lt;&lt; "You are youngster!"; //you had a synatax error here
        }
    else
        if ((UsersAge &gt;=20) &amp;&amp; (UsersAge &lt;=60))
        {
        cout &lt;&lt; "You are an adult!";
        }
    else
        if ((UsersAge &gt;=60) &amp;&amp; (UsersAge &lt;=140))
        {
        cout &lt;&lt; "You're old!";
        }
    else
        if ((UsersAge &gt;=140))
        {
        cout &lt;&lt; "You're lying!";
        }
                
    cout &lt;&lt; " Because you are " &lt;&lt; UsersAge &lt;&lt; " years old!";
    
    char end;// declares the character
    cin.ignore();//ignores last keyboard input, not needed if nothing was typed. 
    cin.get(end); //asks user for the chacter "End"
              
}

Link to comment
Share on other sites

Thanks man.

Also, if you don't want your program to end after the user inputs you can add.

system("pause")

Which gives the classic "Press a key to continue"

I think it initializes a pause.exe which is a part of the C++.. or something like that..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...