Apr 5, 2018 at 10:38pm UTC
I just need to see what is causing the segmentation fault (core dump) error and for some reason I can't get the constructor to output the error message when the date is out of range for the month.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#include <string>
#include <iostream>
using namespace std;
class DayOfYear
{
private :
int day;
static string month;
public :
DayOfYear& operator ++();
DayOfYear operator ++(int );
DayOfYear& operator --();
DayOfYear operator --(int );
DayOfYear(string classMonth,int classDay)
{
day = classDay;
month = classMonth;
if (day >= 1 && day <= 31)
{
if (day > 29)
{
if (month == "FEBRUARY" )
{
month = "fail" ;
}
else if (day > 30)
{
if (month == "APRIL" )
{
month = "fail" ;
}
else if (month == "JUNE" )
{
month = "fail" ;
}
else if (month == "SEPTEMBER" )
{
month = "fail" ;
}
else if (month == "NOVEMBER" )
{
month = "fail" ;
}
}
}
}
}
string print()
{
if (month == "fail" )
{
month = "INCORRECT INPUT - PROGRAM FAILED" ;
cout << month;
}
if (month == "JANUARY" )
{
month = "January" ;
cout << month << " " << day << "\n" ;
}
else if (month == "FEBRUARY" )
{
month = "February" ;
cout << month << " " << day << "\n" ;
}
else if (month == "MARCH" )
{
month = "March" ;
cout << month << " " << day << "\n" ;
}
else if (month == "APRIL" )
{
month = "April" ;
cout << month << " " << day << "\n" ;
}
else if (month == "MAY" )
{
month = "May" ;
cout << month << " " << day << "\n" ;
}
else if (month == "JUNE" )
{
month = "June" ;
cout << month << " " << day << "\n" ;
}
else if (month == "JULY" )
{
month = "July" ;
cout << month << " " << day << "\n" ;
}
else if (month == "AUGUST" )
{
month = "August" ;
cout << month << " " << day << "\n" ;
}
else if (month == "SEPTEMBER" )
{
month = "September" ;
cout << month << " " << day << "\n" ;
}
else if (month == "OCTOBER" )
{
month = "October" ;
cout << month << " " << day << "\n" ;
}
else if (month == "NOVEMBER" )
{
month = "November" ;
cout << month << " " << day << "\n" ;
}
else if (month == "DECEMBER" )
{
month = "December" ;
cout << month << " " << day << "\n" ;
}
else
{
month = "INCORRECT INPUT - PROGRAM FAILED" ;
cout << month;
}
}
};
string DayOfYear::month = "" ;
int main()
{
string monthInput;
int dayInput;
cout << "Enter an integer from 1 to 31 for the month: " ;
cin >> dayInput;
cout << "\nEnter a string for a month all uppercase: " ;
cin >> monthInput;
DayOfYear doy(monthInput, dayInput);
doy.print();
cout << "\n" ;
return 0;
}
Last edited on Apr 5, 2018 at 10:38pm UTC
Apr 5, 2018 at 11:12pm UTC
...an integer from 1 to 31 for the month? Thats a strange question to be asked.
Apr 5, 2018 at 11:20pm UTC
It's that final else statement. The one after December that doesn't have an if. Program execution dies right there.
It looks like you should have used switch case there, and instead of a final "else", had the default be there? If you can use switch case that is.
Apr 6, 2018 at 12:19am UTC
In addition, your print function is expecting you to return a string, but you are only printing.
Printing is not the same thing as returning a string.
If you don't want to return anything, change the return type to void .
Last edited on Apr 6, 2018 at 12:20am UTC