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
  • 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,...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • C++ Project Modify the Date Class: Standards Your program must start with comments giving your name...

    C++ Project Modify the Date Class: Standards Your program must start with comments giving your name and the name of the assignment. Your program must use good variable names. All input must have a good prompt so that the user knows what to enter. All output must clearly describe what is output. Using the date class, make the following modifications: Make the thanksgiving function you wrote for project 1 into a method of the Date class. It receive the current...

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
ADVERTISEMENT