Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

So I just started learning how code, currently learning C++. There's a question in my assignment that's confusing me. It goes like this: "Write a program, which creates both a typedef data type named date of original data type int and
an enumerated data type named Month, consisting of the 12 months with JANUARY starting at 1.
Next create a variable of data type date, with a starting value of 15, and a variable of data type
Month, with its initial value to be APRIL." I do understand the "enumerated data named Month" but for the rest, I don't unfortunately. If there's someone can help, I would be appreciated. Thank you for your time.

What I have tried:

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

int main() 
{
    int Date;
    enum Month 
    {
        January = 1,
        February,
        March,
        April,
        May,
        June,
        July,
        August,
        September,
        October,
        November,
        December,
    };
}
Posted
Updated 23-Nov-21 4:13am
v2
Comments
Greg Utas 24-Nov-21 7:27am    
Although the assignment didn't ask for it, the reason for defining a Date class, as suggested in Solution 2, is so that its constructor can check for illegal dates like April 31. Exactly what to do if such a date is specified depends on the program, but usually the constructor would use the throw operator to raise an exception.

You can look at typedef specifier - cppreference.com[^], for the first part.
You create variables by specifying the type, then the name you will use to refer to it. And you can also initialise its value at the same time:
C++
date today = 15;
 
Share this answer
 
You better visit some Learn C++ tutorial to understand the language better.

I think your task is a bit unusual, but some code could look like
C++
int day = 15;
Month month = April;
But I would create a class or struct Date like this:
C++
class CDate {
  int day;
  Month month;
  int year;

  // benefit are contructors and function
  CDate(int day, Month month, int year);
  print(); // some output
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900