Thread: Problem with Boolean.

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Problem with Boolean.

    Okay, well, I copied the script from the tutorial on the site (the enter your age tutorial one), and I added more phrases. The problem was that if you entered, "2", as your age, you would get all the answers for the other ages. Let me post the code so you know my problem.
    Code:
    #include <iostream>    
    
    using namespace std;
            
    int main()
    {
      int age;
      
      cout<<"Please input your age: ";
      cin>> age;
      cin.ignore();
      if ( age <= 25 ) {
         cout<<"You are pretty young!\n";
      }
      if ( age <= 50 ) {
         cout<<"You are middle aged\n";
      }
      if ( age <= 100 ) {
           cout<<"You are old\n";
      }
      else if ( age <= 125 ) {
        cout<<"You are really old\n";
      }
      else {
           cout<<"Stop lying you moron and tell me your age\n";
      }
      cin.get();
    }

    That was what I originally changed, until this...

    Code:
    #include <iostream>    
    
    using namespace std;
            
    int main()
    {
      int age;
      
      cout<<"Please input your age: ";
      cin>> age;
      cin.ignore();
      if ( age <= 25 ) {
         cout<<"You are pretty young!\n";
      }
      if ( age <= 50 && >= 26 ) {
         cout<<"You are middle aged\n";
      }
      if ( age <= 100 && >= 51 ) {
           cout<<"You are old\n";
      }
      else if ( age <= 125 && >= 101 ) {
        cout<<"You are really old\n";
      }
      else {
           cout<<"Stop lying you moron and tell me your age\n";
      }
      cin.get();
    }
    The problem is I don't know boolean, or how to fix this problem so please help me, a lonely n00b, out.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
      if ( age <= 50 && age >= 26 ) {
    You're pretty close; just add age to each check.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh dude, thanks. Now let me try it out.


    EDIT: Awesome, it works. Thanks man.
    Last edited by Asbestos; 02-03-2005 at 01:28 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Either that or you are missing a couple else's:

    Code:
    #include <iostream>    
    
    using namespace std;
            
    int main()
    {
      int age;
      
      cout<<"Please input your age: ";
      cin>> age;
      cin.ignore();
      if ( age <= 25 ) {
         cout<<"You are pretty young!\n";
      }
      else if ( age <= 50 ) {
         cout<<"You are middle aged\n";
      }
      else if ( age <= 100 ) {
           cout<<"You are old\n";
      }
      else if ( age <= 125 ) {
        cout<<"You are really old\n";
      }
      else {
           cout<<"Stop lying you moron and tell me your age\n";
      }
      cin.get();
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh yeah, thanks dude. Also, how would I make it say random things? Well, not random, but randomly pick a phrase for that specific input. So like, if they say a number from 50 to 100, it'll pick a phrase that's meant to be said only for 50 to 100.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Asbestos
    Also, how would I make it say random things? Well, not random, but randomly pick a phrase for that specific input. So like, if they say a number from 50 to 100, it'll pick a phrase that's meant to be said only for 50 to 100.

    You would need an array or other somesuch container which held the strings that you wished to select from. Then you would need to use the srand and rand functions to generate a random number from which you would be able to pick one of the strings to output.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh thanks. Now let me ask my god google for some tuts on that .

  8. #8
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Quote Originally Posted by Asbestos
    Oh thanks. Now let me ask my god google for some tuts on that .
    Or you could just look at the tutorial. The main Cprogramming website is available to look at and learn from.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    How did I miss that? Oh well, thanks.

  10. #10
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    You're welcome!

    Sorry if I sounded grumpy. I was in a bad mood, but then I played with my Hello Kitty dolls... I...I mean ate a bowl of ice cream, yeah, that was it!


    \/\/
    fuh
    /\/\
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    No offense taken, and Hello Kitt... Ice Cream is the best thing ever made. Yes, ice cream.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    "Hello Kitty"?

    *edit*
    Hmm... Rather peculiar.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM