program5

Hello,

I moved to next exercise. The original was:

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.

Add this to your letter:

If your friend is under 12, write "Next year you will be age+1."
If your friend is 17, write "Next year you will be able to vote."
If your friend is over 70, write "I hope you are enjoying retirement."

Now I have code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()

{
      int age;
      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!";

    else


    if (age<12) cout<<"Next year you will be (<< age << +1)";

    if (age==17) cout<<"Next year you will be able to vote";

    if (age>70) cout<<"I hope you are enjoying retirement";

    else
  
    cout<<"You are kidding!";


     } 


And not quite working, any suggestions? I can though compile the program.
Here is your code with hopefully more illustrative format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (age>0 && age<=110)
{
  cout<<"I hear you just had a birthday and you are "<< age << "years old!";
}
else if (age<12)
{
  cout<<"Next year you will be (<< age << +1)";
}

if (age==17)
{
  cout<<"Next year you will be able to vote";
}

if (age>70)
{
  cout<<"I hope you are enjoying retirement";
}
else
{
  cout<<"You are kidding!";
}

Can you now see why the logic of your code is not quite what you do expect?
Ok I changed the code but is still not sufficient:

Enter your age22
I hear you just had a birthday and you are 22years old!You are kidding!

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (age>0 && age<=110) {
    cout<<"I hear you just had a birthday and you are "<< age << "years old!\n";
    if (age<12) {
        cout<<"Next year you will be " << age + 1 << '\n';
    }
    if (age==17) {
        cout<<"Next year you will be able to vote\n";
    }
    if (age>70) {
        cout<<"I hope you are enjoying retirement\n";
    }
} else {
    cout<<"You are kidding!\n";
}
Last edited on
here is the fix code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()

{
      int age;
      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!";
       }
  else if (age<12)
  {
      cout<<"Next year you will be" << age +1 ;
  }
  else   if (age==17)
{
cout<<"Next year you will be able to vote";
}
  else   if (age>70)
{

 cout<<"I hope you are enjoying retirement";
}  else
 {
    cout<<"You are kidding!";
}

}
Last edited on
Still a misunderstanding of the problem. According to you Problem mentioned above. You should not use " else if " since it will be ignored whenever a single "if" statement is true. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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!";

          //Once The Age is valid! Check Additional Information Here
          if (age<12)
          {
              cout<<"Next year you will be" << age +1 ;
          }
          else if (age==17)
          {
              cout<<"Next year you will be able to vote";
          }
          else if (age>70)
          {
             cout<<"I hope you are enjoying retirement";
          } 
          
      }
      //Once The Age is Invalid - No need to check anything now
      else
          cout<<"You are kidding!";

    //Next Line + endl Clears the input buffer too
    cout << endl; 
    return 0;
}
justinelandichoruiz wrote:
here is the fix code

No. Read the task description. MiiNiPaa's code does what is required, but yours doesn't. One of the key things in programming is to learn to see the subtle differences in the code and how they change the logical outcome.

@pacman169: I did not change your code at all. I just used different style.

This thread now has four blocks of code that look different. Two of them have same logic, so only three different logic, but the three are truly different.
Topic archived. No new replies allowed.