Thread: Need help learning C++.

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    10

    Need help learning C++.

    Hi, I'm Sarah.

    I need help learning C++. I have been trying to read tutorials and such but I am the type of person that has trouble just reading something and getting it, if it is only put one way. I have been on many forums before, and I am sure this is going to get flamed to hell, but if anyone would be interested in helping me to understand more, I would really appreciate it.

    Thanks for your time.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am the type of person that has trouble just reading something and getting it
    So read it, then try it for yourself.

    It doesn't matter how many tutorials you read, or how many tutors you can talk to, it all boils down to actually getting down and actually writing some stuff for yourself.

    Then when you get stuck, post your code and questions to the forum, and someone will likely give you an answer.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    Code:
    #include <iostream>
    
    using namespace std;
    		
    int main()
    {
      int age;
      
      cout<<"Please input your age: ";
      cin>> age;
      cin.ignore();
      if ( age < 47 ) {
         cout<<"You are pretty young!\n";
      }
      else if ( age == 47 ) {
         cout<<"I love you mom!!!!!\n";
           }
      else if (age > 47 ) {
        cout<<"You are old!!!!\n";     
    }
      cin.get();
    }
    Ok, so what I am trying to do here.. is alter the thing in the tutorial to show my mom.

    I managed to get it this far, but what I want it do is make it so that 47-100 is old, and 100+ is very old.

    I tryed typing it a few different ways, but I can't seem to get it to work right.

    I know this is very newb stuff, but I just started today.
    Last edited by Azereal; 01-03-2006 at 02:54 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you need a condition like
    if ( age > 47 && age < 100 )

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    how would you say "less than or equal too"

    example.... greater than 5 but less than or equal to 25

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Hi Sarah,

    Don't worry we won't flame you...

    less than or equal to would be.... <=
    more than or equal to would be..... >=

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    Heh, I knew that, I just didnt know if I had to use && and such. I know how to do this all mathmatically, just not in C++ form yet.

    ( If that made any sense)

    also what is wrong with this it keeps giving me an error
    Code:
    #include <iostream>
    
    using namespace std;
    		
    int main()
    {
      int age;
      
      cout<<"Please input your age: ";
      cin>> age;
      cin.ignore();
      if ( age < 5 || age == 5 ) {
         cout<<"Are you potty trained yet?!\n";
      }
      else if ( age > 5 && age < 12 || age == 12 ) {
              cout<<"Woo! You're maturing.\n";
              }
      else if ( age > 12 && age < 20 ) {
              cout<<"You're a teenager, welcome to hell.\n";
              }
      else if ( age > 21 && < 30 ) {
              cout<<"Party it up, you can drink, your prolly in college, or just out of it. Have fun.\n";
              }
      else if ( age == 20 ) {
              cout<<" You are the ........tiest age ever. You are in college, alot of work.. and you can't drink yet. Hold int here one more year.\n";
              }
      else if ( age > 30 && age < 40 || age == 30 || age == 40) {
              cout<<"You are getting towards middle aged, start to settle down.\n";
              }
      else if ( age > 40 && age < 47 ) {
              cout<<"You're now middle aged. Get used to it.n\";
              }  
      else if ( age == 47 ) {
              cout<<"I love you mom!!!!!\n";
              }
      else if ( age > 100 || age == 100 ) {
              cout<<"Plan your funeral, make sure your will is correct, etc.\n";     
              }
      else if ( age > 47 && age < 60 ) {
              cout<<"You're getting up there in years!!!\n";
              }
      else if ( age > 60 || age == 60 && age < 100 ) }
               cout<<"You're old.\n";
               }
      cin.get();
    }
    I followed the format of the tutorials one, I just wanted to see if I could elaborate on it.
    Last edited by Azereal; 01-03-2006 at 04:30 AM.

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Hi,

    Something's wrong here
    Code:
    else if ( age > 21 &&???< 30 )
    And here

    Code:
    else if ( age > 40 && age < 47 ) {
              cout<<"You're now middle aged. Get used to it.n\";
    And here

    Code:
     else if ( age > 60 || age == 60 && age < 100 ) }
               cout<<"You're old.\n";



  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    last thing I swear ( i fixed those, and it runs well now thank you.)

    Is there a way to make it go again? like after it tells you your answer, it asks again, and you can enter a new age. Without having to x out of it and run it again every time

    ( Sorry about my punctuation I havn't slept. xD)

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    No probs,

    Use a while loop, do you want me to show you or do you want to read the tutorials and learn for yourself? There's a good tutorial section in the home page.


  11. #11
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    Sure, lookup do...while loops. They look like this:
    Code:
    do{
      //code here - this will always be executed at least once, since we havn't reached a test condition yet
    } while( condition );
    In your case, the condition will need to test whether the user wants to start again or not.

    If you get stuck, post back here! Good luck...

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    I would actually appreciate the help. The only thing I understand on the tutorial is the examples, and that's after I do it over and over and over wrong. I want to show my mom this program soon, so I am hoping to get it done. I have been doing this all night as sad as that is.


    I dont understand what you just told me, I was typing this while you posted that hence me editing this.
    Last edited by Azereal; 01-03-2006 at 05:02 AM.

  13. #13
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Possibly this...

    Code:
    #include <iostream>
    
    using namespace std;
    		
    int main()
    {
      int age;
      
      do
      {
      
      cout<<"Please input your age (0 to quit): ";
      cin>> age;
      cin.ignore();
      
      
       
      if ( age < 5 || age == 5 ) {
         cout<<"Are you potty trained yet?!\n";
      }
      else if ( age > 5 && age < 12 || age == 12 ) {
              cout<<"Woo! You're maturing.\n";
              }
      else if ( age > 12 && age < 20 ) {
              cout<<"You're a teenager, welcome to hell.\n";
              }
      else if ( age > 21 && age< 30 ) {
              cout<<"Party it up, you can drink, your prolly in college, or just out of it. Have fun.\n";
              }
      else if ( age == 20 ) {
              cout<<" You are the ........tiest age ever. You are in college, alot of work.. and you can't drink yet. Hold int here one more year.\n";
              }
      else if ( age > 30 && age < 40 || age == 30 || age == 40) {
              cout<<"You are getting towards middle aged, start to settle down.\n";
              }
      else if ( age > 40 && age < 47 ) {
              cout<<"You're now middle aged. Get used to it.\n";
              }  
      else if ( age == 47 ) {
              cout<<"I love you mom!!!!!\n";
              }
      else if ( age > 100 || age == 100 ) {
              cout<<"Plan your funeral, make sure your will is correct, etc.\n";     
              }
      else if ( age > 47 && age < 60 ) {
              cout<<"You're getting up there in years!!!\n";
              }
      else if ( age > 60 || age == 60 && age < 100 ) {
               cout<<"You're old.\n";
               }
               
      }while(age!=0);  
      cin.get();
    }
    Although it has a flaw... Gotta go now good luck.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    I see the flaw... if you enter 0 as your age it tells you then quits.

  15. #15
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    More importantly, can you see a way around the flaw? Hint: Wouldn't it be a better idea to actually ask the user if he/she would like another go?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  3. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  4. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM
  5. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 01:53 PM