A popular horoscope kind of thing is to get a person’s lucky number from his/her
ID: 3770242 • Letter: A
Question
A popular horoscope kind of thing is to get a person’s lucky number from his/her date of birth. This is accomplished by adding together all the digits in the day, month, and year of the birthdate, then repeat the process until there is a single digit, which will be in the range of 1 to 9, inclusive. For example, if the date is 21-Nov-1995, the process is as follows:
21: 2 + 1 = 3
11: 1 + 1 = 2
1995: 1 + 9 + 9 + 5 = 24: 2 + 4 = 6
and then,
3 + 2 + 6 = 11: 1 + 1 = 2
Thus, the lucky number of this date is 2.
Write a function called ‘sumDigits’ that has one parameter of type int, and returns the sum of the digits in its parameter. For example, sumDigits(35) will return 3+5 which is 8.
Write a program that asks the user for an input file name containing dates specified in the following format dd-mmm-yyyy. The program will read the dates one at a time, breaks-up the date into three parts (day, month, and year), converts the month into a numeric value(1 through 12), and then calls the method ‘sumDigits’ at least four times to compute the lucky number corresponding to that date.
Include a loop in the program that allows the user to repeat this process as often as he/she wishes.
Sample input and output: The output of your program must exactly match the format shown below.
Programmer: Name of the programmer(s)
Course: COSC 111, Fall 2015
Lab#: Take Home Project 2
Due date: Dec. 10, 2015
Enter an input file name: hp2A.txt
Lucky number for ‘21-Nov-1995’ is: 2
Lucky number for ‘18-Jan-2001’ is: 4
Lucky number for ‘12-Mar-1975’ is: 1
Do it again, yes (or no)? YES
Enter an input file name: hp2B.txt
Lucky number for ‘11-Apr-2013’ is: 3
Lucky number for ‘23-Apr-2013’ is: 6
Lucky number for ‘1-Jan-2014’ is: 9
Lucky number for ‘9-Dec-1999’ is: 4
Do it again, yes (or no)? YES
Enter an input file name: hp2C.txt
Sorry, ‘hp2C.txt’ doesn’t exist
Do it again, yes (or no)? no
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <cstring>
#define MAX 1000
using namespace std;
int sum(int i)
{
int result=0;
while(i>0)
{
result += i%10;
i /= 10;
}
if(result>9)
return sum(result);
else
return result;
}
int Lucky_number(string date)
{
string month = date.substr(3,3);
string day = date.substr(0,2);
string year = date.substr(7,4);
int m,d,y,total;
if(month == "Jan")
m = 1;
else if(month == "Feb")
m = 2;
else if(month == "Mar")
m = 3;
else if(month == "Apr")
m = 4;
else if(month == "May")
m = 5;
else if(month == "Jun")
m = 6;
else if(month == "Jul")
m = 7;
else if(month == "Aug")
m = 8;
else if(month == "Sep")
m = 9;
else if(month == "Oct")
m = 10;
else if(month == "Nov")
m = 11;
else if(month == "Dec")
m = 12;
m = sum(m);
d = sum(atoi(day.c_str()));
y = sum(atoi(year.c_str()));
total = m+d+y;
return sum(total);
}
int main()
{
cout<<"Programmer: XYZ ";
cout<<"Course: COSC 111,Fall 2015 ";
cout<<"Lab# Take Home Project2 ";
cout<<"Due date: Dec.10 2015 ";
ifstream infile;
string input_file,row,response = "yes";
while(response!="no")
{
cout<<"Enter the input file name::";
cin>>input_file;
infile.open(input_file.c_str());
if (!infile.is_open())
cout<<"Sorry, "<<input_file<<" doesn't exist ";
while(infile>>row)
cout<<"Lucky number for "<<row<<" is :: "<<Lucky_number(row)<<endl;
cout<<"Do it again, (yes or no)? ";
cin>>response;
infile.close();
}
return 1;
}
/*
Input file should of this format
21-Nov-1995
18-Jan-2001
12-Mar-1975
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.