Thread: simple question

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Question simple question

    i have tried on several different occasions to write a program that would ask a question or give a scenario and based on the amount input display different phrase. what happens though is that several phrases will end up being displayed because it fits into both circumstances. for example in this instance i have two phrases being triggered because the input is larger than both 10 and 18. how can i fix this issue? is there a way to have it output something if the variable input is between say 10 and 18 rather than greater than or equal to 10.

    Code:
     //whats's your age?
    
    #include <iostream>
    using namespace std;
    using std::endl;
    
    
    int main()
    
    
    {
        cout<< "how old are you";
        int age;
        cin>> age;
    
    
    
    
        if (age>=10)
        {
            cout<< "1";
        }
        
        if (age<=9)
        {
            cout<< "2";
        }
        if (age>=18)
        {
            cout<< "3";
        }
        cin.ignore();
        cin.get();
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Look into logical operators. These will allow you to phrase the criteria as you're looking for; i.e. "if age is over 10 AND under 18..."

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    8
    Exactly! There are several forms to do it, but you can use something like:

    Code:
    if (age < 10)
         cout << "Age lesser than 10\n";
    else
         cout << "Age bigger than or equal to 10\n";
    The else part is executed if test results in false, that is, age bigger than or equal to 10. You can combine else with if:

    Code:
    if ()
         ...
    else if ()
         ...
    Study relational operators || and &&; they are not strictly necessary in that case, but you can use them if you want. And there are other comparison operators: ==, !=, <, >, <= and >=.

    Correction:

    Relational operators are the last. || and && are logical operators.
    I'm sorry!
    Last edited by hudsonf88; 03-06-2013 at 05:48 PM.

  4. #4
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Code:
        if (age>=10 && age<18)
        {
            cout<< "Your age is between 10 and 17!";
        }

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    thanks a lot for the help. I decided the most logical alterations were as follows
    Code:
    //whats's your age?
    
    
    #include <iostream>
    using namespace std;
    using std::endl;
    
    
    int main()
    
    
    {
    	cout<< "Input your age:";
    	int age;
    	cin>> age;
    
    
    	if (age<=9)
    	{
    		cout<< "you are below the age of 10!";
    	}
    
    
    	if (age>=10 && age<=17)
    	{
    		cout<< "you are between the ages of 10 and 17!";
    	}
    	if (age>=18)
    	{
    		cout<< "You are an adult";
    	}
    	
    	
    	
    	cin.ignore();
    	cin.get();
    
    
    	return 0;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question
    By tat in forum C Programming
    Replies: 7
    Last Post: 03-29-2007, 08:10 PM
  2. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  3. Simple question
    By ZakkWylde969 in forum Tech Board
    Replies: 11
    Last Post: 09-21-2003, 07:16 PM
  4. A simple question
    By Kelvin in forum C Programming
    Replies: 6
    Last Post: 07-08-2002, 02:35 PM
  5. A Simple Question
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 10-26-2001, 05:23 PM