Hi guys,

I have to write a program with a class that will take the number of the day in a year and display the month and day of that number. I'm stuck from the start. Can anyone give me a start that I can run with. I also need some help with the header. I know how to start writing a header but I don't know where to write it in visual basic or how to attach it to my .cpp file. Thanks for all you help.

Dani AI

Generated

A compact, practical pattern follows. The class should hold the day number and a leap-year flag, validate the input, and convert using a month-length table stored in the implementation file. Place the declaration in a header (DayOfYear.h) and the implementation in DayOfYear.cpp, then include the header in the .cpp that uses the class. In Visual Studio add a Header File and a C++ Source File to the same project; the IDE will compile and link both automatically. Following earlier points from and , the code below is intentionally minimal and safe for inclusion in a student project.

#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H

#include <string>
#include <utility>

class DayOfYear {
public:
    explicit DayOfYear(int day, bool isLeap = false);
    std::pair<int,int> toMonthDay() const; // {month (1-12), day (1-31)}
    std::string format() const;            // "MonthName D"
private:
    int dayOfYear_;
    bool isLeap_;
};

#endif // DAYOFYEAR_H
// DayOfYear.cpp
#include "DayOfYear.h"
#include <stdexcept>
#include <string>

static const int md_norm[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
static const int md_leap[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
static const char* monthNames[12] = {
    "January","February","March","April","May","June",
    "July","August","September","October","November","December"
};

DayOfYear::DayOfYear(int day, bool isLeap) : dayOfYear_(day), isLeap_(isLeap) {
    int max = isLeap_ ? 366 : 365;
    if (day < 1 || day > max) throw std::out_of_range("day out of range");
}

std::pair<int,int> DayOfYear::toMonthDay() const {
    const int* months = isLeap_ ? md_leap : md_norm;
    int m = 0;
    int d = dayOfYear_;
    while (m < 12 && d > months[m]) { d -= months[m++]; }
    return {m + 1, d};
}

std::string DayOfYear::format() const {
    auto md = toMonthDay();
    return std::string(monthNames[md.first - 1]) + " " + std::to_string(md.second);
}
// main.cpp (simple test harness)
#include <iostream>
#include "DayOfYear.h"

int main() {
    int day;
    if (!(std::cin >> day)) return 0;
    try {
        DayOfYear d(day); // pass true for leap years if required
        std::cout << d.format() << '\n';
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << '\n';
    }
    return 0;
}

Notes: keep the month arrays in the .cpp to avoid multiple-definition issues. The constructor throws on invalid input; change that behavior to return an error code if exceptions are not desired in the course. For real-world use add an overload that accepts a year and computes leap-year status automatically.

Recommended Answers

All 6 Replies

What you're asking help with makes no sense. VB? What's that have to do with C++? And why do you need a header?

Forget everything but your first line -- "I have to write a program with a class that will take the number of the day in a year and display the month and day of that number." That's your problem, the rest seems meaningless.

What do you know? Arrays? Math? Number of days in a month?

I am in the chapter about classes. Just read it. We us vb in class its the only compiler that I know about that's why I mentioned it. While I was reading about making headers I think I understand the structure but don't know how to tie it into my program or here to enter the code. But the project asks us to prompt the user to enter a day of the year and display the month and day. For example you would enter 6 and the output would be January 6 or enter 360 and the output would be December 26.

VB is not a compiler, it is a language. Your using vc++ i assume. Even then, vc++ is not a compiler, its an IDE. Your compiler is most likely MingW.

I guess. That's just what we use in the class. I've never had any interest in programming before or ever tried. Was just introduced to these things in this semester. It's a lot harder to learn from an online class. I was doing great at first but now I'm getting into things I really don't follow as fast. Classes are really confusing to me. I need to write a class to convert day of the year to the month and day.

I am in the chapter about classes. Just read it. We us vb in class its the only compiler that I know about that's why I mentioned it.

well, you're in C++ now, so forget VB. It's completely different.

While I was reading about making headers I think I understand the structure but don't know how to tie it into my program or here to enter the code.

You don't need to make any headers, just use the ones already defined.

But the project asks us to prompt the user to enter a day of the year and display the month and day. For example you would enter 6 and the output would be January 6 or enter 360 and the output would be December 26.

I already got that. But you didn't bother to answer any of my questions.

vc++ is not a compiler, its an IDE. Your compiler is most likely MingW.

No, Visual Studio/VC++ Express has its own compiler. If you are using an IDE such as code::blocks or Dev-C++ that comes with mingw in some cases then that will likely be your compiler.

@OP I'm presuming you mean Visual Studio. This has the capability to do C#/C++/VB/ASP.NET etc. (a couple others in VS 2010). You are still more than likely using the C++

I need to write a class to convert day of the year to the month and day.

Ok so you're talking about the declaration of the class being in a header? It can be. Write out the declaration and see how far you get and we can look it over for you. Remember it's just the the types and access (public, private and protected) of the member variables and member methods(what you might call functions).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.