Defining an object using an explicit value constructor - c++

I'm trying to do an exercise using classes to store dates (MM DD YYYY). I have two class constructors, one default that sets the Date values to a default, and one that uses explicit values sent from main() in my driver file.
However, after my declaration statement in main() to the explicit value constructor (to make date2) in my driver file, I'm getting errors that appear to indicate that date2 was never made.
Below are the relevant portions for this constructor. How do I get date2 to become defined and declared to use for other things in main()?
Header
class Date
{
private:
int month, day, year;
public:
Date(); // default date constructor will assign all data members to equivalent of 01/01/2022
Date(int initMonth, int initDay, int initYear); //parametered Date constructor will collectively set all three data members
}
Implementation
Date :: Date(): month(1), day(1), year(2022) {}
Date :: Date(int initMonth, int initDay, int initYear)
{
if (initMonth < 1 || initMonth > 12) { cerr << "Invalid month\n\n"; month = -2; }
if (initDay < 1 || initDay > 30) { cerr << "Invalid day\n\n"; day = -2; }
if (initYear < 1) { cerr << "Invalid year\n\n"; year = -2; }
month = initMonth;
day = initDay;
year = initYear;
}
Driver
Date date1;
do
{
cout << "Please enter a date in this sequence: MM DD YYYY ";
cin >> month >> day >> year;
Date date2(month, day, year);
} while (date2.month == -2 || date2.day == -2 || date2.year == -2);
date1.print();
date2.print();

Related

Visual C++ doesn't recognize my function as a part of a class

I'm making a decent program that will take any date, and find the day before and after it. I have everything finished, except when I debugged it, I got around 13 errors! After reading the error statements, I figured that the function wasn't recognized as part of the class, but I have no idea why.
I feel like I understand what I'm doing, but there's something that's not right. If anyone has any suggestions, please let me know. Thank you!
I have three files:
Main File (Source.cpp):
#include <iostream>
#include <iomanip>
#include <string>
#include "DateType.h"
using namespace std;
int main() {
//Inputs
int month, day, year;
bool flag;
DateType today, tomorrow, yesterday;
do { //Registers input and checks if it can go through program
cout << "Enter the current month (1-12): ";
cin >> month;
cout << "Enter the current year: ";
cin >> year;
cout << "Enter in today's date: ";
cin >> day;
today.SetDate(month, day, year);
flag = today.valid_date();
} while (!flag);
//Equalizing all three objects
tomorrow = today;
yesterday = today;
//Moving ford and back one day
tomorrow.NextDay();
yesterday.PreviousDay();
//Output
cout << "Given Date: " << today.print_Day() << endl;
cout << "Next Date: " << tomorrow.print_Day() << endl;
cout << "Previous Date: " << yesterday.print_Day() << endl;
system("pause");
return 0;
}
Header File (DateType.h)
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
enum months { Jan=31, Feb = 28, March = 31, April = 30, May = 31,
June = 30, July = 31, August = 31, Sept = 30, Oct = 31, Nov = 30, Dec = 31, Feb_Leap = 29 };
class DateType {
private:
int mo;
int day;
int yr;
months DaysInMonth;
void Day_Month_Assign(); //This function does the same thing as DaysinMonth() on the assignment sheet, using the enum
public:
DateType(); //Constructor to set mo=0, day=0, yr=1000
void SetDate(int, int, int); //Date is set according to the incoming parameter
void NextDay(); //Date is increased by one to get NextDay
void PreviousDay(); //Date is decreased by one day to get Previous Day
string print_Day(); //Returns a string value
bool valid_date(); //Checks if an input is a valid date
};
DateType.cpp File
#include <iostream>
#include <cstdlib>
#include "DateType.h"
#include <string>
using namespace std;
DateType::DateType() {
mo = 0;
day = 0;
yr = 1000;
}
void DateType::Day_Month_Assign() {
switch (mo) {
case 1: DaysInMonth = Jan;
case 2: DaysInMonth = Feb;
case 3: DaysInMonth = March;
case 4: DaysInMonth = April;
case 5: DaysInMonth = May;
case 6: DaysInMonth = June;
case 7: DaysInMonth = July;
case 8: DaysInMonth = August;
case 9: DaysInMonth = Sept;
case 10: DaysInMonth = Oct;
case 11: DaysInMonth = Nov;
case 12: DaysInMonth = Dec;
}
if ((yr % 4 == 0 && yr % 100 != 0) ||
(yr % 400 == 0))
DaysInMonth = Feb_Leap;
}
void DateType::SetDate(int month, int days, int year) {
mo = month;
day = days;
yr = year;
Day_Month_Assign();
}
void DateType::NextDay() {
day++;
if (day > DaysInMonth) { //Raises month
day = 1;
mo++;
Day_Month_Assign(); //Assigns new value to enum
}
if (mo > 12) {
mo = 1;
yr++;
Day_Month_Assign();
}
}
void DateType::PreviousDay() {
day--;
//Different order than the last one
//I need to get the days in month from enum first
//That will be the new day value
if (day == 0) {
mo--;
Day_Month_Assign();
day = DaysInMonth;
}
if (mo == 0) {
yr--;
mo = 12;
Day_Month_Assign();
day = DaysInMonth;
}
}
string DateType::print_Day() {
string date;
date = (to_string(mo) + "-" + to_string(day) + "-" + to_string(yr));
return date;
}
bool DateType::valid_date() {
bool flag;
if (day <= DaysInMonth && day >= 1) { //Days in Month Check
if (mo > 0 && mo <= 12) { //Month in Year Check
flag = true;
cout << "Valid Date, Accepted!\n";
}
else {
flag = false;
cout << "Invalid Month!\n";
}
}
else {
flag = false;
cout << "Invalid Date!\n";
}
return flag;
}
Edit: The function in question is the String DateType::print_Day() function. Here are the errors:
Main File:
'print_Day' is not a member of 'DateType' (Called 3 Times), C2039
CPP File:
'print_Day' is not a member of 'DateType' (Line 73, C2039)
'mo': undeclared identifier (Line 75, C2065)
'day': undeclared identifier (Line 75, C2065)
'yr': undeclared identifier (Line 75, C2065)
declaration is incompatible with " DateType::print_Day()" (declared at line 21 of Header File) (Line 73, E0147)
Header File:
'print_Day': unknown override specifier (Line 21, C3646)
syntax error: '(' (Line 21, C2059)
unexpected token(s) preceding ';' (Line 21, C2238)
All three of the header file errors are thrown again later on in the error list. I hope this helps!
In DateType.h, string is not known because the string class belongs to std namespace. To fix the error, change the definition of print_Day to
std::string print_Day()
Do not try to fix it by adding using namespace std; in the header - that will cause no end of problems further down the line.
Small note on programming style: in DateType.cpp, it would be better to include DateType.h before anything else. That way, you will know that DateType.h has no dependencies and can be included in any cpp file.
This doesn’t address the question, but don’t name a variable flag. Give variables names that tell you what they do. In this case, date_is_valid, although a bit long, is much clearer. But you don’t need it. Just make the loop test while(!today.valid_date()).

How do I display the output of my C++ class

Hello guys I am trying to create a date class in C++ where it display the default date, displays a short set date, displays a long set date, just display a set year. However, I am trying this display the contents of the class when I input the data but I am getting an error "Use of undeclared identifier 'setDate'
Here is the header file code:
#ifndef dateClass_h
#define dateClass_h
using namespace std;
class DateObj
{
public:
int setDate(int month, int day, int year);
void shortDate(int month, int day, int year);
void longDate(string month, int day, int year);
//return the year
int getYear(int year){
return year;
}
private:
int month;
int day;
int year;
};
#endif /* dateClass_h */
Here is my implementation file code:
#include <stdio.h>
#include <iostream>
#include <string>
#include "dateClass.h"
using namespace std;
//setDate Method
int DateObj::setDate(int month, int day, int year){
//setMonth = month;
//day = setDay;
//year = setYear;
//check to make sure month is between 1 - 12.
if(month < 1 || month > 12){
cout << "This is an invalid Month. Please enter a month that is between 1 and 12: " << endl;
}
//check to make sure day is between 1 -31.
if(day < 1 || day > 31){
cout << "This is an invalid Day. Please enter a day that is between 1 and 31: " << endl;
}
//check to make sure year is greater than zero.
if(year < 0){
cout << "This is an invalid Year. Please enter a year that is greater than Zero" << endl;
}
return setDate(month,day,year);
}
And here is my main program code:
#include <iostream>
#include <string>
#include "dateClass.h"
using namespace std;
int main() {
DateObj myDate;
myDate.setDate(12, 25, 2016); //set the date to Dec 25, 2016.
cout << "The current date is: " << setDate() << endl;
return 0;
}
I will just like to know why I am getting this error and what I will need to fix in my class or main program.
Edit
I got the program to compile butI got the following error "EXC_BAD_ACCESS (code=2, address=0x7fff5f3fffe8) at the return setDate(month, day, year) break point.
#include <iostream>
#include <string>
#include "dateClass.h"
using namespace std;
int main() {
DateObj myDate;
myDate.setDate(12,25,2016); //set the date to Dec 25, 2016.
cout << "The current date is: " << myDate.setDate(12, 25, 2016) << endl;
return 0;
}
There appears to be no implementation for your shortDate function.
You have it in your header file, but you don't appear to have it in your dateClass.cpp file. You need to implement it in your dateClass.cpp file similar to how you did for setDate.
You're getting the error because it can't find any implementation.
The reason you are getting an error is because setDate() is a void function. When you try to use setDate() in the line:
cout << "The current date is: " << setDate() << endl;
you get an error because setDate() needs to return an std::ostream & object or some other data that is convertible. I recommend using setDate(int month, int day, int year) purely to set the data members of your class and create a separate function to actually print out the values.
As an alternative, you could overload setDate() so that it returns an acceptable data type, like so:
#include <iomanip>
//the parameter 'right' is required for chaining together different << when forming output
std::ostream & DateObj::setDate(std::ostream & right) {
//displays the date in mm/dd/yyyy format
//setfill defines a character to fill empty spaces created by setw
right << std::setfill('0') << std::setw(2) << month
<< '/' << std::setfill('0') << std::setw(2) << day
<< '/' << std::setfill('0') << std::setw(4) << year;
return right;
}
Well, for one thing you're calling setDate() inside setDate() in such a way that it will infinitely just call itself. You'll call setDate(), which will call setDate(), which will call setDate() and so on. I'm kind of a noob too, but I'm pretty sure what you're getting is a stack overflow error (Hey, title drop! :D). A stack overflow happens when you allocate too many variables and run out of memory. So because you're calling setDate() infinitely, you are also making infinite versions of the variables in it, and filling up all your memory.
For another, set date doesn't currently actually do anything. It checks to see if you have valid input, then calls itself again. From the name, I assume it's meant to populate your member variables? If that's what it's supposed to do, it should probably look more like this:
int DateObj::setDate(int month, int day, int year){
//check to make sure month is between 1 - 12.
if(month < 1 || month > 12){
cout << "This is an invalid Month. Please enter a month that is between 1 and 12: " << endl;
}
//check to make sure day is between 1 -31.
if(day < 1 || day > 31){
cout << "This is an invalid Day. Please enter a day that is between 1 and 31: " << endl;
}
//check to make sure year is greater than zero.
if(year < 0){
cout << "This is an invalid Year. Please enter a year that is greater than Zero" << endl;
}
//the part that actually sets the variables. Aka, the important bit.
month_ = month;
day_ = day;
year_ = year
}
The trailing underscores on month, day and year are just style things, I like to use them for member data so that I can use those names again as local variables in my functions, which was useful here if you noticed. Once you've put the data into your variables, it should be pretty easy to display everything. Just do something like:
cout << month_ << "/" << day_ << "/" << year_ << endl;
That will output your date to the console in a format something like this: 3/12/2016.

issue when creating an "if-else" statement

Hi i'm pretty new to java I've been having an issue when I attempt an "if-else" statement. I get syntax errors, which I'm not sure why. Thanks for your input.
import javax.swing.JOptionPane;
public class MagicDate {
public static void main(String [] args){
int month;
int day;
int year;
String input;
String message = ("This program will find a secret date");
JOptionPane.showMessageDialog(null, message);
input = JOptionPane.showInputDialog( " Enter a month of the in numeric "
+ "form");
month= Integer.parseInt(input);
input = JOptionPane.showInputDialog( " Enter a day of the in numeric"
+ " form");
day= Integer.parseInt(input);
input = JOptionPane.showInputDialog( " Enter a two digit year");
year = Integer.parseInt(input);
if(month * day == year);
{
JOptionPane.showMessageDialog(null, "The Date is Magic!");
}
else
{
(month * day != year);
JOptionPane.showMessageDialog(null, "The Date is not Magic");
}
System.exit(0);
}
}
You should remove the semicolon at the end of the line containing the if statement. Basically, the effect of the semicolon is adding an empty statement. So your code reads like this to the compiler:
If (...)
Do nothing;
// can't have an brace here since the if statement has already terminated.
{
...
}
Remove the semicolon:
if(month * day == year)
Remove ; after if condition
if(month * day == year); // remove ;
and missing if
else
{
(month * day != year); // add if before line and remove ;
JOptionPane.showMessageDialog(null, "The Date is not Magic");
}

C++ assigning string variables in if statements

I am new to C++. I am trying to assign a string to a month depending on the number chosen in cin.
This is my code within a function.
cout << "Enter your birth month: ";
cin >> mm; //Birth Month//
int mm;
std::string month;
if(mm == 5 || mm == 05){
std::string month = "May";
}
// If month is equal to 5 or 05, set string variable month to May
cout << "You were born in the month of " << month << endl;
How do i assign the variable "month" to "may" if mm is equal to 5?
Your string variable is actually shadowed by another declaration in the if block scope
if(mm == 5 || mm == 05) {
std::string month = "May"; // That value is gone after the `}`
}
You probably want to write
if(mm == 5) {
month = "May"; // Note the type declaration is omitted here
}
Also note that why I ommitted mm == 05 in my sample above, is bacause
if(mm == 5 || mm == 05)
is wrong. The second comparison expression actually compares with an octal integer literal, and you certainly don't want to do this.
if(mm == 5)
is sufficient.
You already declared your string above, so a simple assignment operation on month would work:
if(mm == 5 || mm == 05){
month = "May";
}
Also, even if the user enters "05" as their integer, it will be stored as '5', so there is no need for the condition
|| mm == 05
Side note, you must declare "int mm" before your cin statement, like this:
int mm;
cout << "Enter your birth month: ";
cin >> mm; //Birth Month//
month = "May";
This is an assignment.
std::string month = "May";
This is not an assignment. This us a declaration. You are declaring a new variable called month, unrelated to any other variable by the same name you might have.

Why am I getting a meesed up cout statement from my function?

Hi I am working on a program that should display the latter of two dates, it needs to accept two Date structures and return the latter of them. Latter being 04/31/2014 is latter then 04/30/2014. Here is my code and I do not understand why I am getting a long weird number... Thanks for helping.
#include <iostream>
#include <iomanip>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
int main()
{
void laterDate(Date, Date);
Date one;
Date two;
Date later;
cout << "Enter a Date starting with the day, then month, then year:\n";
cin >> one.day;
cin >> one.month;
cin >> one.year;
cout << "\n\nEnter another date in the same fashion as above:\n";
cin >> two.day;
cin >> two.month;
cin >> two.year;
cout << "\n\nThank you, I will now tell you which date is later then the other!" << endl;
laterDate(one, two);
system("pause");
return 0;
}
void laterDate(Date o,Date t)
{
Date later;
if (o.year >= t.year)
if (o.month >= t.month)
if (o.day > t.day)
{
later.day= o.day;
later.month = o.month;
later.year = o.year;
}
else
{
later.day = t.day;
later.month = t.month;
later.year = t.year;
}
cout << later.day << "/" << later.month << "/" << later.year << endl;
}
OUTPUT
Enter a Date starting with the day, then month, then year:
04
30
2014
Enter another date in the same fashion as above:
04
31
2014
Thank you, I will now tell you which date is later then the other!
-858993460/-858993460/-858993460
Press any key to continue . . .
END OUTPUT
You should break out the logic of comparing the dates from the code which displays the results. The standard idiom for comparing objects that can be totally ordered (such as dates) is to overload operator<.
// the intent of this function is to return true if lhs precedes rhs,
// and return false otherwise (rhs precedes lhs, or they are equal)
bool operator<(Date const& lhs, Date const& rhs)
{
// first test the year.
// if the years differ, there is no need to test the month or the day
if (lhs.year < rhs.year) return true;
if (lhs.year > rhs.year) return false;
// years are equal, test the month
if (lhs.month < rhs.month) return true;
if (lhs.month > rhs.month) return false;
// months are equal, test the day
return lhs.day < rhs.day;
}
Then your function can be easily written like this:
void laterDate(Date const& lhs, Date const& rhs)
{
Date const& later = (lhs < rhs) ? rhs : lhs;
cout << later.day << "/" << later.month << "/" << later.year << endl;
}
if (o.year > t.year ||
(o.year >= t.year && o.month > t.month) ||
(o.year >= t.year && o.month >= t.month &&
o.day > t.day))
{
later.day= o.day;
later.month = o.month;
later.year = o.year;
}
else
{
later.day = t.day;
later.month = t.month;
later.year = t.year;
}
in this way , else will always be called , in your implementation it is only called when the two first condition where true, resulting in the display of random bits
I think I'd structure things a little differently. First, I'd make outputting a Date a friend function of the Date class and next, I'd overload the > operator for comparing Date class instances.
struct Date
{
int day;
int month;
int year;
friend std::ostream &operator<<(std::ostream& out, const Date &d) {
return out << d.day << "/" << d.month << "/" << d.year;
}
bool operator>(const Date &t) const;
};
The implementation of the > operator would look like this:
bool Date::operator>(const Date &t) const
{
if (year > t.year) return true;
if (year < t.year) return false;
if (month > t.month) return true;
if (month < t.month) return false;
return day > t.day;
}
And then your main routine would be this:
int main()
{
Date one;
Date two;
std::cout << "Enter a Date starting with the day, then month, then year:\n";
std::cin >> one.day >> one.month >> one.year;
std::cout << "\n\nEnter another date in the same fashion as above:\n";
std::cin >> two.day >> two.month >> two.year;
std::cout << "\n\nThank you: " << (one > two ? one : two)
<< " is the later date\n";
}
Note, too, that one can chain the input operator parts as shown here. In fact, I'd be inclined to declare an input operator operator>> and promote the Date struct to a full-fledged class.