Question

Given a number, calculate how many years into the future it is, and what date. Assume...

Given a number, calculate how many years into the future it is, and what date. Assume no leap years.
For example:
Please enter a day of the year (0 to exit): 1
jan 1
Please enter a day of the year (0 to exit): 365
dec 31
Please enter a day of the year (0 to exit): 366
1 year
jan 1
Please enter a day of the year (0 to exit): 0
Thanks for playing!

My current code:

#include <iostream>
#include <string>

using namespace std;

int main() {
    const int MonthDays[]={31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 366};
    const string MonthName[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "nov", "dec"};
    int day, year, index;
    while(1){
    cout<<"Please enter a day of the year (0 to exit): ";
    cin>>day;
    cout<<day;
    cout<<endl;
        if(day==0){
            break;
        }
  
    while(day>0){
        if(day>365){
            year=day/366;
            day=day%365;
            cout<<year<<" Year"<<endl;
        }
        day=day%365;
        if(day==0){
            cout<<"dec 31"<<endl;
        }
        if(day>=1&&day<=31){
            cout<<MonthName[index]<<" "<<day;
            cout<<endl;
        }
        else {
            cout<<MonthName[index]<<" "<<day-MonthDays[index-1];
            cout<<endl;
        }
        cout<<"Please enter a day of the year (0 to exit): ";
        cin>>day;
        cout<<day<<endl;
    }
    }
    cout<<"Thanks for playing!";
  
    return 0;
}

----------------------------------------

my error message:

==================== YOUR OUTPUT =====================                                                                                              

0001: Please~enter~a~day~of~the~year~(0~to~exit):~1                                                                                                 

0002: jan~1                                                                                                                                         

0003: Please~enter~a~day~of~the~year~(0~to~exit):~365                                                                                               

0004: dec~31                                                                                                                                        

0005: jan~0                                                                                                                                         

                                                                                                                                                    

                                                                                                                                                    

=================== MISMATCH FOUND ON LINE 0005: ===================                                                                                

ACTUAL  : jan~0                                                                                                                                     

EXPECTED: Please~enter~a~day~of~the~year~(0~to~exit):~31                                                                                            

======================================================                                                                                              

                                                                                                                                                    

Adjust your program and re-run to test.                                                                                                             

                                         

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>

using namespace std;

int main() {
    const int MonthDays[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const string MonthName[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
    int day, year=0, index;
    while(1){
        cin>>day;
        cout<<endl;
        
        if(day==0){
                break;
        }

        index = 0;
        year = 0;
        while(day > 365) {
                day = day - 365;
                year++;
        }
        if(year != 0) {
                cout<<year<<" Year"<<endl;
        }
        
        index = 0;
        while(day > MonthDays[index]) {
                day = day - MonthDays[index];
                index++;
        }
        cout << MonthName[index] << " " << day << endl;
    }
    cout<<"Thanks for playing!";
  
    return 0;
}

your code did not even have the names of all months.. I have corrected everything and as you can see in screenshot it works well. Please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Given a number, calculate how many years into the future it is, and what date. Assume...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++)- error =================== MISMATCH FOUND ON LINE 0007: ===================                            ACTUAL&nbsp

    (c++)- error =================== MISMATCH FOUND ON LINE 0007: ===================                            ACTUAL  : 2~year                                                                                EXPECTED: 2~years                                                                               ======================================================                                                                                                   #include <iostream> using namespace std; int main() { int daynum, n;    cout<<"Please enter a day of the year (0 to exit): "; cin>>daynum; cout<<daynum<<endl;    while(daynum>0) { L:if(daynum>1&&daynum<=31) cout<<"jan "<<daynum<<endl; else if(daynum>31&&daynum<=59) { cout<<"feb "<<daynum-31<<endl; } else if(daynum>59&&daynum<=90) { cout<<"mar "<<daynum-59<<endl; } else if(daynum>90&&daynum<=120) { cout<<"apr "<<daynum-90<<endl; } else if(daynum>120&&daynum<=151) { cout<<"may "<<daynum-120<<endl; } else if(daynum>151&&daynum<=181) { cout<<"jun "<<daynum-151<<endl; } else if(daynum>181&&daynum<=212) { cout<<"jul...

  • Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int...

    Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int getDay() const; // add any member you need here }; You implementation should enable the usage like this: void f() { Date date = d; cout << "date = " << date << endl; cout << "date+1 = " << date+1 << endl; cout << "date-1 = "...

  • Assuming that a year has 365 days, write a class named DayOfYear that takes an integer...

    Assuming that a year has 365 days, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by day of the month. For example, Day 2 would be January 2. Day 32 would be February 1. Day 365 would be December 31. The constructor for the class should take as parameter an integer representing the day of the year, and the class should have...

  • The output should be "The distance is 0" (The expected). ==================== YOUR OUTPUT =====================                        

    The output should be "The distance is 0" (The expected). ==================== YOUR OUTPUT =====================                                                                                                               0001: Enter~the~x~coordinate~for~point~1:~0                                                                                                                          0002: Enter~the~y~coordinate~for~point~1:~0                                                                                                                          0003: Enter~the~x~coordinate~for~point~2:~0                                                                                                                          0004: Enter~the~y~coordinate~for~point~2:~0                                                                                                                          0005: The~distance~is~-nan    =================== MISMATCH FOUND ON LINE 0005: ===================                                                                                                 ACTUAL  : The~distance~is~-nan                                                                                                                                       EXPECTED: The~distance~is~0                                                                                                                                          ======================================================   #include <iostream> #include <iomanip> #include <string> using namespace std; float squareRoot(float s) { float xn; xn = s / 2.0; int counter = 1; while (counter <= 10) { xn = (xn + (s/xn))/2.0; counter = counter + 1; } return xn; } int...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT