0

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:

  1. Calculate the age of a person based on the user's input (Birth year, month, day).

  2. The user needs to enter enter the day on which they were born (so for example: thursday)

  3. 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;
}
}
Frank
  • 13
  • 2
  • Why is `birthdayfdb` an `int` when you are asking for text? – NathanOliver Sep 13 '18 at 13:02
  • Yes, Point taken. etc. my eyes hurt when seeing that condition :( – Adam Ostrožlík Sep 13 '18 at 13:02
  • What do you suggest? Keep in mind I never really did programming before and I have been trown into the deep with this assignment, I do not really know a lot. – Frank Sep 13 '18 at 13:09
  • Do you know what a `std::string` is? – NathanOliver Sep 13 '18 at 13:11
  • No, I do not : ( – Frank Sep 13 '18 at 13:13
  • You're going to need to read a basic text, rather than relying on guesswork. The name of a variable as `sunday` does not mean the test `birthdayfdb == sunday` will check if the user entered something representing a day called Sunday. – Peter Sep 13 '18 at 13:13
  • 5
    @Frank "_Keep in mind I never really did programming before and I have been trown into the deep with this assignment_" If someone gave you such assignment, that means, that you should have all of the necessary knowledge to do it, or have the resources, that contain such knowledge. Here is the SO recommended list of books, from which one can learn C++: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Algirdas Preidžius Sep 13 '18 at 13:20
  • Why is this question downvoted? Only because he clearly doesn't know how to code isn't a good reason to downvote. He provide us with code, He tried and the post is well formatted and well asked. – Simo Sep 13 '18 at 14:13

1 Answers1

1

You need to use a Switch Statement in order to achieve what you are looking for.

This is your full code fixed:

#include <iostream>
#include <string>
using namespace std;

int main ( ){
    int birthyear;
    int birthmonth;
    int birthday;
    cout << "What is your birth year?" << endl;
    try{
        cin >> birthyear;
    }
    catch(...){
        cout << "A number is the only accepted input" << endl;
        return 0;
    }
    cout << "In which month are you born? (Answer should be in numbers from 1 to 12)"<< endl;
    try{
        cin >> birthmonth;
    }
    catch(...){
        cout << "A number is the only accepted input" << endl;
        return 0;
    }
    cout << "On which day are you born?" << endl;
    try{
        cin >> birthday;
    }
    catch(...){
        cout << "A number is the only accepted input" << endl;
        return 0;
    }
    if ((birthday > 24 || birthday < 1) && (birthmonth > 9 || birthmonth < 1) && (birthyear > 2008 || birthyear < 1918)) {
        cout <<"You are too old/young."<< endl;
        return 0;
    }
    double ageinyears = (2018 - birthyear); 
    int agemonths = (9 - birthmonth); 
    int agedays;
       switch (birthmonth){
        case 2:
            if((birthyear%100 == 0) && (birthyear%400 == 0)){
                agedays = (29 - birthday);
            }
            else if(birthyear%4 == 0 && birthyear%100 != 0){
                agedays = (29 - birthday);
            }
            else{
                agedays = (28 - birthday);
            }
            break;
        case 4:
            agedays = (30 - birthday);
            break;
        case 11:
            agedays = (30 - birthday);
            break;
        case 6:
            agedays = (30 - birthday);
            break;
        case 9:
            agedays = (30 - birthday);
            break;  
        default:
            agedays = (31 - birthday);
            break;
    }
    int totalmonths = ((ageinyears*12) + agemonths);
    cout << "Your age is " <<ageinyears<< " year, "<<agemonths<<" months, and " <<agedays<<" days, or "<<totalmonths<<"   total months." << endl;

    if ((birthday == 24) && (agemonths == 0)){

        cout <<" "<< endl;
        cout << "Congrationlations with your birthday!"<< endl;
        cout <<" "<< endl;
    }
    else if ((agedays == 24) && (agemonths != 0)) {
    cout <<" "<< endl;
    cout << "Congratulations on your month anniversary!"<< endl;
    cout <<" "<< endl;
    }
    int jc = birthyear % 100; 
    int jcr = ((jc + (jc / 4)) % 7); 
    int ec; 
    if ((birthyear >= 1900) && (birthyear <= 1999)){
        ec = 0;
    }
    else if ((birthyear >= 2000) && (birthyear <= 2099)){
        ec = 6;
    }
    int mc;
    switch(birthmonth){
        case 1: 
            mc = 0; 
            break;
        case 2: 
            mc = 3; 
            break;
        case 3: 
            mc = 3; 
            break;
        case 4:
            mc = 6;
            break;
        case 5: 
            mc = 1; 
            break;
        case 6: 
            mc = 4;
            break;
        case 7:
            mc=6;
            break;
        case 8: 
            mc = 2; 
            break;
        case 9:
            mc = 5;
        case 10: 
            mc = 0; 
            break;  
        case 11: 
            mc = 3; 
            break;  
        case 12:
            mc = 5;
            break;
    }   
    int sjc;  
    if ((birthmonth == 1 || 2) && (birthyear % 4 == 0 )){
        sjc = 1;
    }
    else{
       sjc = 0;
    }
    int fdb = ((jcr + mc + ec + birthday - sjc) % 7); 
    std::string birthdayfdb;
    cout <<"On which day are you born? E.G. monday" << endl;
    try{
        cin >> birthdayfdb;
    }
    catch(...){
        cout << "only english days name are accepted!";
        return 0;
    }
    cin >> birthdayfdb;
    bool check = false;
    switch(fdb){
        case 0:
            if(birthdayfdb == "sunday") check = true;
            break;

        case 1:
            if(birthdayfdb == "monday") check = true;
            break;

        case 2:
            if(birthdayfdb == "tuesday") check = true;
            break;

        case 3:
            if(birthdayfdb == "wednesday") check = true;
            break;

        case 4:
            if(birthdayfdb == "thursday") check = true;
            break;

        case 5:
            if(birthdayfdb == "friday") check = true;
            break;

        case 6:
            if(birthdayfdb == "saturday") check = true;
            break;
    }
    if(check){
        cout << "This is correct" << endl;
    }
    else{
        cout << "This is incorrect" << endl;
    }
}

As you can see, we used a boolean variable (bool check;) to save if the user input is validated in order to print (later) the messagge on the screen.

Of course you could just print inside the Switch Case Statement like this:

case 0:
    if(birthdayfdb == "sunday"){
        cout << "This is correct" << endl;
    }
    else{
        cout << "This is incorrect"<< endl;
    }

But this would make it longer and a bit slower. Also, in the above case there are alot of repetition (pretty much the same if statement on every case) while is preferred to just code one single print while saving on a bool variable if the validation of the input is OK.

FIXES:

  • A little mistake that causes ALOT of problem: you were using insted of " to denote strings
  • You were using alot of If statement instead of using else if
  • For the birthday calculation is better to use a switch statement
  • I removed some useless blank output e.g. cout << " " <<endl;
  • You were declaring some string variable as int e.g. int birthdayfdb
  • You were checking the user input 3 times while most of the check was a repetition. I change it with only one input validation at the end of the inputs
  • Another one little mistake that causes alot of problems. You were refering to birthdayfdb as a function and not a string. E.G. if(birthdayfdb() == "sunday") check = true;
  • A very BIG MISTAKE is the absence of errors handling and a well-done input validation! e.g. If the user type "foo" instead of his birthyear

for further information about String, Data Types, Switch Case statement and Try/Catch statement read the linked documentation!

Simo
  • 925
  • 7
  • 18