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 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...
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...
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 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...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT