So basically I just started to learn c++ and have an assignment which I'm totally stuck on, I googled it for days but just can't find out how to make it work.
The assignment is very simply:
Calculate the age of a person based on the user's input (Birth year, month, day).
The user needs to enter enter the day on which they were born (so for example: thursday)
The Program needs to validate if this is indeed correct
Now I am stuck on point 3;
I made a formula that calculates on which day someone is born. And basically that formula outputs a number 0 - 6 and that references to a day (0 would be sunday, 1 would be monday, etc.)
The problem is that a user would input for example thursday, but the program only recognizes nr 4 as thursday. So the program can't confirm or deny that the user is indeed born on a thursday.
To fix that, I tried the following:
This is the formula that calculates the number 0 to 6 (0 should equal sunday for example)
int fdb = ((jcr + mc + ec + birthday - sjc) % 7); //
This is the full code:
int birthdayfdb;
int sunday;
int monday;
int tuesday;
int wednesday;
int thursday;
int friday;
int saturday;
cout <<"On which day are you born?" << endl;
cin >> birthdayfdb; // Here the users inputs the day where he or she is born on
if ((birthdayfdb == sunday && fdb == 0) || (birthdayfdb == monday && fdb == 1) || (birthdayfdb == tuesday && fdb == 2) || (birthdayfdb == wednesday && fdb == 3) || (birthdayfdb == thursday && fdb == 4) || (birthdayfdb == friday && fdb == 5) || (birthdayfdb == saturday && fdb == 6))
{
cout << "This is Correct" << endl;
}
else
{
cout << "This is incorrect" << endl;
return 0;
}
So this does not work because whatever birthday the user enters, it always says that is is correct.
I looked up all sorts of things on the internet (stackflow, youtube, etc) but I can't find out how to do this.
Complete:
#include <iostream>
using namespace std;
int main ( ) {
int birthyear;
int birthmonth;
int birthday;
cout << “What is your birth year?” << endl;
cin >> birthyear;
if ((birthyear > 2008) || (birthyear < 1928))
{
cout <<“You are too old/young.“<< endl;
return 0;
}
cout << "In which month are you born? (Answer should be in
numbers)“<< endl;
cin >> birthmonth;
if (((birthmonth > 9) && (birthmonth == 2008)) || ((birthmonth < 9)
&& (birthmonth == 1928)))
{
cout <<"You are too old/young.”<< endl;
return 0;
}
cout << “On which day are you born?” << endl;
cin >> birthday;
if (((birthday > 24) && (birthmonth == 9) && (birthyear == 2008)) ||
((birthday < 24) && (birthmonth == 9) && (birthyear == 1928)))
{
cout <<"You are too old/young.”<< endl;
return 0;
}
double ageinyears = (2018 - birthyear);
int agemonths = (9 - birthmonth);
int agedays = (24 - birthday);
int totalmonths = ((ageinyears*12) + agemonths);
cout << “Your age is "<<ageinyears<<" jaar, "<<agemonths<<" months,
and " <<agedays<<" days, or "<<totalmonths<<" months.” << endl;
if ((birthday == 24) && (agemonths != 0))
{
cout <<" "<< endl;
cout << “Congratulations on your month anniversary!“<< endl;
cout <<" "<< endl;
}
else
{
cout << " "<< endl;
}
if ((birthday == 24) && (agemonths == 0))
cout <<" "<< endl;
cout << “Congrationlations with your birthday!”<< endl;
cout <<" "<< endl;
}
int jc = birthyear % 100;
int jcr = ((jc + (jc / 4)) % 7);
int ec;
if ((birthyear >= 1900) && (birthyear <= 1999))
{
ec = 0;
}
if ((birthyear >= 2000) && (birthyear <= 2099))
{
ec = 6;
}
int mc;
if ((birthmonth == 1) || (birthmonth == 10))
{
mc = 0;
}
if (birthmonth == 5)
{
mc = 1;
}
if (birthmonth == 8)
{
mc = 2;
}
if ((birthmonth == 2) || (birthmonth == 3) || (birthmonth == 11))
{
mc = 3;
}
if (birthmonth == 6)
{
mc = 4;
}
if ((birthmonth == 9) || (birthmonth == 12))
{
mc = 5;
}
if ((birthmonth == 4) || (birthmonth == 7))
{
mc = 6;
}
int sjc;
if ((birthmonth == 1 || 2) && (birthyear % 4 == 0 ))
{
sjc = 1;
}
else
{
sjc = 0;
}
int fdb = ((jcr + mc + ec + birthday - sjc) % 7);
cout << fdb << endl;
std::string birthdayfdb;
cout <<"On which day are you born?” << endl;
cin >> birthdayfdb;
bool check = false;
switch(fdb){
case 0:
if(birthdayfdb() == “sunday”) check = true;
case 1:
if(birthdayfdb() == “monday”) check = true;
case 2:
if(birthdayfdb() == “tuesday”) check = true;
case 3:
if(birthdayfdb() == “wednesday”) check = true;
case 4:
if(birthdayfdb() == “thursday”) check = true;
case 5:
if(birthdayfdb() == “friday”) check = true;
case 6:
if(birthdayfdb() == “saturday”) check = true;
}
if(check){
cout << "This is correct" << endl;
}
else{
cout << "This is incorrect" << endl;
}
}