Question

In: Computer Science

Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...

Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.

Solutions

Expert Solution

#include<iostream>
#include<string>
#include<cstring>
#include <sstream>
#include<cmath>
#include <ctime>

using namespace std;
void date( int a[])
{
time_t t = time(NULL);
tm* timePtr = localtime(&t);
  
int years;
  
// extracting current date
  
int cur_day = timePtr->tm_mday;
int cur_mon = (timePtr->tm_mon)+1;
int cur_year = (timePtr->tm_year)+1900;
  
if(cur_day < a[2])   
{
  
if (cur_mon == 3)
{
  
if ((cur_year % 4 == 0 && cur_year % 100 != 0) || (cur_year % 400 == 0))
{
cur_day += 29;
}
  
else
{
cur_day += 28;
}
}
  
else if (cur_mon == 5 || cur_mon == 7 || cur_mon == 10 || cur_mon == 12)
{
cur_day += 30;
}
  
else
{
cur_day += 31;
}
  
cur_mon = cur_mon - 1;
}
  
if (cur_mon < a[1])
{
cur_mon += 12;
cur_year -= 1;
}   
  
years = cur_year - a[0]; // difference in years of given and current date
  
if(years >= 21)
{
cout <<"Welcome";
}
else
{
cout << "Sorry";
}
}
int main()
{
string str, str1 = "";
  
int a[100];
  
cout << "Please enter the birthday date: ";
getline (cin, str); // read the current date
int j = 0;
  
for(int i = 0; i < str.size(); i++ )
{
  

if(str[i] == '/') // slice the string and convert into int
{
stringstream geek(str1);
int x = 0;
geek >> x;
a[j] = x;
j++;
//cout << x;
str1 = "";
}
else
{
str1 = str1 + str[i];
}
}
stringstream geek(str1);
int x = 0;
geek >> x;
a[j] = x;
j++;
//cout << x;
str1 = "";
  
  
  
  
if(a[0]>=1900 && a[0]<=9999) // checking for validation of year
{
//check month
if(a[1]>=1 && a[1]<=12)
{
//check days
if((a[2]>=1 && a[2]<=31) && (a[1]==1 || a[1]==3 || a[1]==5 || a[1]==7 || a[1]==8 || a[1]==10 || a[1]==12))
date(a);
else if((a[2]>=1 && a[2]<=30) && (a[1]==4 || a[1]==6 || a[1]==9 || a[1]==11))
date(a);
else if((a[2]>=1 && a[2]<=28) && (a[1]==2))
date(a);
else if(a[2]==29 && a[1]==2 && (a[0]%400==0 ||(a[0]%4==0 && a[0]%100!=0)))
date(a);
else
printf("Day is invalid.\n");
}
else
{
printf("Month is not valid.\n");
}
}
else
{
printf("Year is not valid.\n");
}
  
}

OUTPUT :


Related Solutions

Write a program that prompts the user to enter a date in the format mm/dd/yyyy where...
Write a program that prompts the user to enter a date in the format mm/dd/yyyy where mm is the month , dd is the day and yyyy is a 4 digit year Check if the date is valid by seeing month is between 1 and 12, day is between 1 to 31 and year is between 1800 to 3000. Also check that if month is 2, day is between 1 to 29 If the date is valid then display the...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format,...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables. Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Write a program that does the following in order: 1. Ask user to enter a name...
Write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out “Your account...
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
Write a C++ program to ask the user to enter the shape type: square, sphere or...
Write a C++ program to ask the user to enter the shape type: square, sphere or circle and the appropriate dimensions of the shape.. The program should output the following information about the shape: a.for a square. it output the area and perimeter. b. for a sphere, it outputs the area. c. fir a circle, it outputs the volume. if the user input anything else, your program should output: "the program does not recognize this shape". Use const whenever it...
1- Write a c++ program that asks the user to enter his/her name using one variable...
1- Write a c++ program that asks the user to enter his/her name using one variable only. The name must be then printed out in capital letter with the family name first and the last name second.
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT