Program4

Hello!

I just compiled a program using these instructions:

Prompt the user to enter the age of the recipient and assign it to an int variable age. Have your program write "I hear you just had a birthday and you are age years old." If age is 0 or less or 110 or more, call simple_error("you are kidding!") using simple_error() from std_lib_facilities.h.

Now I have not downloaded std_lib_facilities.h but I assume the header files that I include with the program should be sufficient.

Here is something I put together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()

{
      int age;
      cout<<"Enter your age";
      cin>>age;
      cout<<"I hear you just had a birthday and you are << age << years old :\n";
      
    
 if (age<=0 or age>=110) cout<<"You are kidding!:\n";


} 


Now I can compile the program but it appears like this:

Enter your age-4
I hear you just had a birthday and you are << age << years old :
You are kidding!:


That`s obviously wrong!
cout<<"I hear you just had a birthday and you are << age << years old :\n";
should be
cout<<"I hear you just had a birthday and you are "<< age << " years old :\n";
and you don't have to put those includes, #include <iostream> is enough.
+
Now it works better but still not sufficient.

Enter your age-2
I hear you just had a birthday and you are -2 years old :
You are kidding!:

"I hear you just had a birthday and you are -2 years old :" That line should not be there, I assume there should be just:

"You are kidding!" according to the if-statement. Also, what is meant by simple_error()?
If you only want the "I hear you just had a birthday..." line to print out if they entered between 1 and 109 then you need to put that condition in the code. Right now you print that out regardless of what was entered in age.

You can use an if else statement to do one thing in one condition and something else in the opposite of that condition...

if ((age > 0 or age <110)
output the "I hear you just had a birthday... " line
else
output the "You're kidding!" line



Edit:
simple_error() is a function.
Last edited on
Yep, though not still working.

Enter your age-4
I hear you just had a birthday and you are -4 years old!

Here is the code modified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()

{
      int age;
      cout<<"Enter your age";
      cin>>age;
      
      
    
      if (age>0 or age<=110) cout<<"I hear you just had a birthday and you are "<< age <<"years old!";

      else

      cout<<"You are kidding!";


     }


Translate that or into the && operator in the if condition.
@wildblue what if the OP meant ||? :P
There was a simple Mistake. It will work fine now :) ... Whenever we consider a number in a range we use && operator rather than || .... so try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
      int age = 0;

      cout << "Enter your age";
      cin >> age;
    
      if (age>0 && age<=110)
          cout<<"I hear you just had a birthday and you are "<< age <<"years old!" << endl;
      else
          cout<<"You are kidding!"<<endl;

    return 0;
}
@wildblue what if the OP meant ||? :P

Original code would have been ||. Restructured example would have been &&. I should have changed the english/pseudocode in my post to say and.

This is from a couple of days ago now, I assume the OP got this to work since they've moved on to other examples since then :)
Topic archived. No new replies allowed.