Help regarding a drill by Bjarne Stroustrup

Trying to solve a drill in the book Programming Principles and Practice Using C++ by Bjarne Stroustrup:

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're kidding!") using simple_error() from std_lib_facilities.h.

I am trying to solve without bothering about the last part ' simple_error() from std_lib_facilities.h'.

Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include <iostream>
#include <string>
using namespace std;
int main()
{
  int age(0);
  cin>>age;
  if (age<=0 or age>110)
  cout<<"You are kidding";
  else
  cout<<"Your age is now";
  cout<<age; 
 }


Unable to figure out what is wrong with the above code. While one popular compiler is showing compiling and seems to be under so-called infinite loop, another compiler is giving the output:
You are kidding0
The problem lies in the else part. If you would indent your code properly it would be obvious.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;
int main()
{
  int age(0);
  cin>>age;
  if (age<=0 or age>110)
    cout<<"You are kidding";
  else
    cout<<"Your age is now";
  cout<<age; /* will be displayed even when input is wrong - would be skipped when using  simple_error() */
 }

without bothering about the last part ' simple_error() from std_lib_facilities.h'.
Why
@Thomas1965 Thanks! Not sure the utility of 'simple_error() from std_lib_facilities.h'. Perhaps I need to go through the book and should be useful. Help regarding the correct code would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include <iostream>
#include <string>
using namespace std;
int main()
{
  int age(0);
  cout<<"enter age";
  cin>>age;
  if (age<=0 or age>110)
  cout<<"You are kidding";
  else
  cout<<"Your age is now"<<age;
   
 }


Though my purpose seems to be solved with the above code, yet not sure about 'call simple_error("you're kidding!") using simple_error() from std_lib_facilities.h' and why I need to bother about it.
If this version
https://github.com/BjarneStroustrup/Programming-_Principles_and_Practice_Using_Cpp/blob/master/std_lib_facilities.h
of std_lib_facilities.h is reliable, simple_error() is just this:
1
2
3
4
5
6
7
// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)    // write ``error: s and exit program
{
    cerr << "error: " << s << '\n';
    keep_window_open();        // for some Windows environments
    exit(1);
}

And keep_window_open() is just:
1
2
3
4
5
6
7
8
inline void keep_window_open()
{
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
    return;
}


If so, assuming that std_lib_facilities.h is in the same directory of your source file, you could try this way:
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
// 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're kidding!")
// using simple_error() from std_lib_facilities.h.
#include "std_lib_facilities.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Please, enter your age: ";
    int age(0); // <-- did you find this in the Stroustrup's book?
                // I can't believe that.
                // Are you sure about this syntax?
    cin >> age;
    if (age <= 0 || age > 110) {
        simple_error("You are kidding"); // <-- the semicolon must come at the end
    }
    else {
        cout << "I hear you just had a birthday and ... // Do complete the 
                                                        // program according to
                                                        // the assignment.
    }
} 

@Enoizat Thanks for the elaborate help.
Topic archived. No new replies allowed.