In: Computer Science
IN C++
Implement in C++ a game called “Your Lucky Number”. The lucky number is calculated based on an individual’s birth month, day, and year. The algorithm assigns any person a number between 1 and 9. Many people believe that a group of people with the same lucky number share common features. The lucky number is calculated as the sum of digits of the birth month, day, and year numbers. If this sum is not a single-digit then you need to keep adding up its digits. Use the C++ functions when you implement the game. Test your program for corrections. To start the game you should enter your full birthdate in the format: month day year, e.g. 9 21 1985. The month is represented by its corresponding integer: January is 1, February is 2, ..., and December is 12. Be sure that your program will verify the input format and print an error message when the format is incorrect (use exceptions), and will ask to re-enter the birthdate again. The algorithm for calculating the lucky number
(a) Enter the month as an integer from 1 to 12.
(b) Reduce the month, day and year numbers down to a one-digit number by adding all digits in the month, day and year numbers, see the example below.
(c) Map the number to the category listed below and display the message on the screen with at least four features for a person in each category. I provided only one feature (as an adjective) for each category and you should complete the list.
(d) Test your program using at least 10 your friends’ or relatives’ birthdays.
Categories:
1. The Leader: original thinker (add 3 more features)
2. The Mediator: diplomatic (add 3 more features)
3. The Communicator: expressive (add 3 more features)
4. The Teacher: trustworthy (add 3 more features)
5. The Freedom Seeker: adventurous (add 3 more features)
6. The Nurturer: family-oriented (add 3 more features)
7. The Seeker: analytic (add 3 more features)
8. The Power House: authoritative (add 3 more features) 9. The Humanitarian: charitable (add 3 more features)
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
#include<limits> //for clearing input buffer when exception is occurred
using namespace std;
//method to read month, day and year of user's DOB and update the parameters
void getDOB(int &month, int &day, int &year){
cout<<"Enter the Date of Birth (month day year): ";
try{
//reading values
cin>>month>>day>>year;
//throwing exception if non numeric inputs are entered
if(!cin.good()){
throw "please input numeric values only!";
}
//validating month, day and year
if(month<1 || month>12 || day<1 || day>31 || year<0){
cout<<"Invalid date, try again!"<<endl;
}else{
//if everything is valid, returning from function
return;
}
}catch(const char *err){
//displaying the cause of exception
cout<<err<<endl;
//clearing buffer
cin.clear();
//removing characters upto new line from stdin
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
//if the program reach here, it means that the input is invalid, so
//simply re asking the user, using simple recursion
getDOB(month,day,year);
}
//method to convert a positive number to single digit between 0 and 9
int convert_to_single_digit(int value){
//if value is already single digit, simply returning it
if(value<10){
return value;
}else{
int sum=0;
//finding sum of digits
while(value>0){
sum+=value%10;
value=value/10;
}
//if sum is above 10, converting to single digit again, or else, it will be
//simply returned in the next iteration of the recursive call
return convert_to_single_digit(sum);
}
}
//method to find lucky number from date of birth
int find_lucky_number(int month, int day, int year){
//summing digits of month, day and year
int lucky=convert_to_single_digit(month)+convert_to_single_digit(day)+convert_to_single_digit(year);
//summing digits of lucky if lucky>10
lucky=convert_to_single_digit(lucky);
//returning it
return lucky;
}
//accepts a lucky number and two string references, and updates the references with
//category and features of the person with that lucky number
void getCategoryAndFeatures(int lucky, string &category, string &features){
//using switch block, assigning values to category and features
switch(lucky){
case 1:
category="The Leader";
features="original thinker, honesty, confidence, commitment";
break;
case 2:
category="The Mediator";
features="diplomatic, respectful, impartial, perceptiveness";
break;
case 3:
category="The Communicator";
features="expressive, consideration, courtesy, clarity";
break;
case 4:
category="The Teacher";
features="trustworthy, ethical, friendliness, wisdom";
break;
case 5:
category="The Freedom Seeker";
features="adventurous, courage, sheer will, wanderlust";
break;
case 6:
category="The Nurturer";
features="family-oriented, practical, valuable, emotional";
break;
case 7:
category="The Seeker";
features="analytic, curiousity, spiritual, courage";
break;
case 8:
category="The Power House";
features="authoritative, energetic, confidence, athletic";
break;
case 9:
category="The Humanitarian";
features="charitable, kind, empathic, coordinated";
break;
}
}
int main(){
//declaring needed variables
int month, day, year, lucky;
string category="",features="";
//reading date of birth
getDOB(month,day,year);
//finding lucky number
lucky=find_lucky_number(month,day,year);
//finding category and features
getCategoryAndFeatures(lucky, category,features);
//displaying results
cout<<"Your lucky number is "<<lucky<<endl;
cout<<"Category: "<<category<<endl;
cout<<"Features: "<<features<<endl;
return 0;
}
//OUTPUT
Enter the Date of Birth (month day year): jwhjd
please input numeric values only!
Enter the Date of Birth (month day year): 100 2 3
Invalid date, try again!
Enter the Date of Birth (month day year): 12 1 -2
Invalid date, try again!
Enter the Date of Birth (month day year): 10 25 1994
Your lucky number is 4
Category: The Teacher
Features: trustworthy, ethical, friendliness, wisdom