Here's the question:
Define a class called Month that is an abstract data type for a month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth). Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to set the month using an integer as an argument (1 for January, 2 for February, and so forth), a default constructor, an input function that reads the month as an integer, an input function that reads the month as the first three letters in the name of the month, an output function that outputs the month as an integer, an output function that outputs the month as the first three letters in the name of the month, and a member function that returns the next month as a value of type Month . Embed your class definition in a test program.
#include <iostream>
using namespace std;
class Month
{
public:
Month (char letter1, char letter2, char letter3);
Month (int numOfMonth);
Month ();
void inputAsNum(); // read month as integer
void inputAsCh(); // read first three chars of month
void outputAsCh() const;
void outputAsNum() const;
Month nextMonth();
private:
int month;
};
int main()
{
Month test(5);
//test.inputAsNum();
cout << "Current month is " << endl;
test.outputAsCh();
test.outputAsNum();
cout << endl;
test.nextMonth();
cout << "Next month is " <<endl;
test.outputAsCh();
test.outputAsNum();
cout << endl;
Month test2('a','p','r');
//test2.inputAsCh();
cout << "Current month is " << endl;
test2.outputAsCh();
test2.outputAsNum();
cout << endl;
test2.nextMonth();
cout << "Next month is " <<endl;
test2.outputAsCh();
test2.outputAsNum();
cout << endl;
Month test3;
test3.inputAsNum();
cout << "Current month is " << endl;
test3.outputAsCh();
test3.outputAsNum();
cout << endl;
test3.nextMonth();
cout << "Next month is " <<endl;
test3.outputAsCh();
test3.outputAsNum();
cout << endl;
Month test4;
test4.inputAsCh();
cout << "Current month is " << endl;
test4.outputAsCh();
test4.outputAsNum();
cout << endl;
test4.nextMonth();
cout << "Next month is " <<endl;
test4.outputAsCh();
test4.outputAsNum();
cout << endl;
return 0;
}
Month::Month (char letter1, char letter2, char letter3)
{
if ((letter1 == 'j')&&(letter2 == 'a')&&(letter3 == 'n'))
month= 1;
else if ((letter1 == 'f')&&(letter2 == 'e')&&(letter3 == 'b'))
month= 2;
else if ((letter1 == 'm')&&(letter2 == 'a')&&(letter3 == 'r'))
month= 3;
else if ((letter1 = 'a')&&(letter2 == 'p')&&(letter3 == 'r'))
month= 4;
else if ((letter1 == 'm')&&(letter2 == 'a')&&(letter3 == 'y'))
month= 5;
else if ((letter1 == 'j')&&(letter2 == 'u')&&(letter3 == 'n'))
month= 6;
else if ((letter1 == 'j')&&(letter2 == 'u')&&(letter3 == 'l'))
month= 7;
else if ((letter1 == 'a')&&(letter2 == 'u')&&(letter3 == 'g'))
month= 8;
else if ((letter1 == 's')&&(letter2 == 'e')&&(letter3 == 'p'))
month= 9;
else if ((letter1 == 'o')&&(letter2 == 'c')&&(letter3 == 't'))
month= 10;
else if ((letter1 == 'n')&&(letter2 == 'o')&&(letter3 == 'v'))
month= 11;
else if ((letter1 == 'd')&&(letter2 == 'e')&&(letter3 == 'c'))
month= 12;
}
Month::Month (int numOfMonth)
:month(numOfMonth)
{ }
Month::Month ()
:month(1)
{ }
void Month::inputAsNum()
{
int num;
cout << "Enter num of month => ";
cin >> num;
month = num;
}
void Month::inputAsCh()
{
char c1,c2,c3;
cout << "Enter three letters of month => ";
cin >> c1 >> c2 >> c3;
Month::Month(c1,c2,c3);
}
void Month::outputAsCh() const
{
if (month == 1)
cout << "Jan ";
else if (month == 2)
cout << "Feb ";
else if (month == 3)
cout << "Mar ";
else if (month == 4)
cout << "Apr ";
else if (month == 5)
cout << "May ";
else if (month == 6)
cout << "Jun ";
else if (month == 7)
cout << "Jul ";
else if (month == 8)
cout << "Aug ";
else if (month == 9)
cout << "Sep ";
else if (month == 10)
cout << "Oct ";
else if (month == 11)
cout << "Nov ";
else if (month == 12)
cout << "Dec ";
}
void Month::outputAsNum() const
{
cout << month;
}
Month Month::nextMonth()
{
if (month < 12)
month++;
else if (month == 12)
month = 1;
return Month(month);
}
The code runs just fine. I try that calling constructor Month::Month (char letter1, char letter2, char letter3) from member function void Month::inputAsCh() . I can solve the problem by changing definition of Month::inputAsCh() and copy paste definitions of the constructor into the function. But, just out of curiosity, can be a constructor called from member function ? If it's okey, Month::inputAsCh() works wrong, I also try on Month test4 variable.
Output Expected Output
------------- -----------------
Current month is Current month is
Nov 11 Nov 11
Next month is Next month is
Dec 12 Dec 12
Current month is Current month is
Apr 4 Apr 4
Next month is Next month is
May 5 May 5
Enter num of month => 2 Enter num of month => 2
Current month is Current month is
Feb 2 Feb 2
Next month is Next month is
Mar 3 Mar 3
Enter three letters of month => apr Enter three letters of month => apr
Current month is Current month is
Jan 1 Apr 4
Next month is Next month is
Feb 2 May 5
Month::Month(c1,c2,c3);
That line doesn't update the current object. It should be an error because Month::Month names the constructor in that context, but some compilers (notably clang) will interpret it as the construction of a temporary object instead (thanks to user657267 for the correction).
You could factor out your month calculation code into a separate function, then call that from both your constructor and your inputAsCh function:
void setMonth(char letter1, char letter2, char letter3)
{
if ((letter1 == 'j')&&(letter2 == 'a')&&(letter3 == 'n'))
month= 1;
else if ((letter1 == 'f')&&(letter2 == 'e')&&(letter3 == 'b'))
month= 2;
else if ((letter1 == 'm')&&(letter2 == 'a')&&(letter3 == 'r'))
month= 3;
else if ((letter1 = 'a')&&(letter2 == 'p')&&(letter3 == 'r'))
month= 4;
else if ((letter1 == 'm')&&(letter2 == 'a')&&(letter3 == 'y'))
month= 5;
else if ((letter1 == 'j')&&(letter2 == 'u')&&(letter3 == 'n'))
month= 6;
else if ((letter1 == 'j')&&(letter2 == 'u')&&(letter3 == 'l'))
month= 7;
else if ((letter1 == 'a')&&(letter2 == 'u')&&(letter3 == 'g'))
month= 8;
else if ((letter1 == 's')&&(letter2 == 'e')&&(letter3 == 'p'))
month= 9;
else if ((letter1 == 'o')&&(letter2 == 'c')&&(letter3 == 't'))
month= 10;
else if ((letter1 == 'n')&&(letter2 == 'o')&&(letter3 == 'v'))
month= 11;
else if ((letter1 == 'd')&&(letter2 == 'e')&&(letter3 == 'c'))
month= 12;
}
Month::Month (char letter1, char letter2, char letter3)
{
setMonth(letter1, letter2, letter3);
}
void Month::inputAsCh()
{
char c1,c2,c3;
cout << "Enter three letters of month => ";
cin >> c1 >> c2 >> c3;
setMonth(c1,c2,c3);
}
Incidentally, your setMonth function could be greatly simplified by creating a std::string from those chars and comparing with that instead.
Related
I am making a calendar program in C++ that determines the days of a month in any given year by counting the number of days between January 1st, 1753 all the way to the first day of the given month in the given year. It then divides this number of days by 7 to determine the "offset" that is used to figure out what day the first day of the month begins (example: since January 1st, 1753 is on a Monday, an offset of 2 means that the first day of the month will be on a Wednesday). I completed my code and ran it through several tests when I noticed a very strange bug. For the year 2000, the offset is one higher than it should be (February starts on a Wednesday instead of a Tuesday, etc). This problem doesn't exist in any other leap year, or year ending with "00". It is the only test I am failing, and for the life of me I can't seem to figure out what the problem is.
Here is the code:
//**********************************************************************
#include <iostream>
#include <iomanip>
using namespace std;
int getYear();
int getMonth();
int computeNumDays(int month, int year);
int computeOffset(int month, int year);
bool isLeapYear(int year);
void displayHeading(int month, int year);
void displayTable(int offset, int numDays);
/********************
* MAIN
*********************/
int main()
{
int month = getMonth();
int year = getYear();
int offset = computeOffset(month, year);
int numDays = computeNumDays(month, year);
displayHeading(month, year);
displayTable(offset, numDays);
return 0;
}
/********************
*GETMONTH
*Prompts the user for a month number
*********************/
int getMonth()
{
int month;
//Month number must be between 1 and 12
cout << "Enter a month number: ";
cin >> month;
//Displays an error message if the month is under 1 or over 12
while (month < 1 || month > 12)
{
cout << "Month must be between 1 and 12.\n";
cout << "Enter a month number: ";
cin >> month;
}
return month;
}
/********************
*GETYEAR
* prompts the user for a year
*********************/
int getYear()
{
int year;
cout << "Enter year: ";
cin >> year;
//Displays an error message if the year is less than 1753
while (year < 1753)
{
cout << "Year must be 1753 or later.\n";
cout << "Enter year: ";
cin >> year;
}
cout << "\n";
return year;
}
/********************
*COMPUTENUMDAYS
* For computing the number of days in a month, so we know where to count to when filling in
* the calendar
*********************/
int computeNumDays(int month, int year)
{
int numDays;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return numDays = 31;
else if (month == 4 || month == 6 || month == 9 || month == 11)
return numDays = 30;
else if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return numDays = 29;
else if (month == 2)
return numDays = 28;
}
/*********************
*COMPUTEOFFSET
*********************/
int computeOffset(int month, int year)
{
int totalYearDays = 0;
int totalMonthDays = 0;
//This counts up all the days between the January 1st of 1753 to January 1st of the users input
//year. Leap years are accounted for with the IF statements and the isLeapYear function
for (int yearCount = 1753; yearCount < year; yearCount++)
{
if (isLeapYear(yearCount))
totalYearDays += 366;
else
totalYearDays += 365;
}
//The days of the month of the user input year are added up here. If the user inputs February(2),
//then it counts the days of each month in between and adds them up.
for (int monthCount = 0; monthCount < month; monthCount++)
{
if (monthCount == 1 || monthCount == 3 || monthCount == 5)
totalMonthDays += 31;
else if (monthCount == 7 || monthCount == 8 || monthCount == 10 || monthCount == 12)
totalMonthDays += 31;
else if (monthCount == 4 || monthCount == 6 || monthCount == 9 || monthCount == 11)
totalMonthDays += 30;
//if the user input year is a leap year, then an extra day to February is added
else if (monthCount == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
totalMonthDays += 29;
else if (monthCount == 2)
totalMonthDays += 28;
}
int offset = (totalYearDays + totalMonthDays) % 7;
return offset;
}
/******************************************
* ISLEAPYEAR
******************************************************/
bool isLeapYear(int yearCount)
{
//Equation for determining if a year is a leap year or not
if ((yearCount % 4 == 0 && yearCount % 100 != 0) || (yearCount % 400 == 0))
return true;
else
return false;
}
/*************************************************
*DISPLAYHEADING
* This is where the Month Name and Year are shown
**************************************************/
void displayHeading(int month, int year)
{
if (month == 1)
cout << "January, " << year << endl;
else if (month == 2)
cout << "February, " << year << endl;
else if (month == 3)
cout << "March, " << year << endl;
else if (month == 4)
cout << "April, " << year << endl;
else if (month == 5)
cout << "May, " << year << endl;
else if (month == 6)
cout << "June, " << year << endl;
else if (month == 7)
cout << "July, " << year << endl;
else if (month == 8)
cout << "August, " << year << endl;
else if (month == 9)
cout << "September, " << year << endl;
else if (month == 10)
cout << "October, " << year << endl;
else if (month == 11)
cout << "November, " << year << endl;
else if (month == 12)
cout << "December, " << year << endl;
return;
}
/********************
*DISPLAYTABLE
*********************/
void displayTable(int offset, int numDays)
{
//days of the week are displayed here
cout << setw(4) << "Su" << setw(4) << "Mo" << setw(4) << "Tu"
<< setw(4) << "We" << setw(4) << "Th" << setw(4) << "Fr" << setw(4)
<< "Sa" << setw(2) << endl;
//WeekBreak counter will be used to add new lines for each week
int weekBreak = 1;
//This IF statement determines the number of days before the first of the month occurs,
// as well as sets the weekBreak counter
if (offset != 6 && offset >= 0)
do
{
cout << setw(4) << " ";
offset--;
weekBreak++;
} while (offset != -1);
//The counter loop here begins putting in the dates, all the way from the first to
//the max number of days in the month
for (int date = 1; date <= numDays; date++)
{
cout << " " << setw(2) << date;
weekBreak++; //weekBreak prevents us from putting more than
//7 dates in a single week
if (weekBreak == 8)
{
cout << "\n"; //once a week hits 7 dates(or 8 spaces), it moves on to a new week
weekBreak = 1;
}
}
//this puts an end to the calander, regardless if weekBreak limit is reached
if (weekBreak >= 2 && weekBreak <= 7)
cout << "\n";
}
It bothers me that it only seems to happen for the year 2000. I'm not sure what the cause could be, so I could really use some feedback.
To debug your code I wrote the following main function:
int main()
{
for (int year = 1753; year <= 2021; year++)
{
for (int month = 1; month <= 12; month++)
{
int offset = computeOffset(month, year);
int numDays = computeNumDays(month, year);
std::chrono::year_month_day date(std::chrono::year(year), std::chrono::month(month), std::chrono::day(1));
std::chrono::weekday day{ std::chrono::sys_days(date) };
int expectedOffset = ((day - std::chrono::Monday).count() + 7) % 7;
if (expectedOffset != offset)
{
std::cout << year << "/" << month << " expected " << expectedOffset << " actual " << offset << "\n";
}
}
}
return 0;
}
This confirms it is indeed only the year 2000 which is incorrect. This suggests its the handling of the leap year every 400 years which is incorrect.
As you have a function for calculating leap years the first thing to try is to use that rather than implementing the same code three times (implementing something 3 times triples the chances of creating a bug). Changing computeNumDays to:
else if (month == 2 && isLeapYear(year))
return numDays = 29;
and computeOffset to:
else if (monthCount == 2 && isLeapYear(year))
totalMonthDays += 29;
fixes the bug.
The cause of the bug is actually the combination of 2 bugs:
in computeOffset monthCount starts from 0 not 1.
the expression monthCount == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) will always be true when year % 400 == 0 is true. The intended expression was monthCount == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
This causes the value of totalMonthDays to be 29 more than it should be for years divisible by 400, after %7 this causes the result of computeOffset to be 1 higher than it should be.
By removing repetition your code can be greatly simplified:
int computeNumDays(int month, int year)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
throw std::invalid_argument("invalid month");
}
}
int computeOffset(int month, int year)
{
int totalYearDays = 0;
int totalMonthDays = 0;
//This counts up all the days between the January 1st of 1753 to January 1st of the users input
//year. Leap years are accounted for with the IF statements and the isLeapYear function
for (int yearCount = 1753; yearCount < year; yearCount++)
{
if (isLeapYear(yearCount))
totalYearDays += 366;
else
totalYearDays += 365;
}
//The days of the month of the user input year are added up here. If the user inputs February(2),
//then it counts the days of each month in between and adds them up.
for (int monthCount = 1; monthCount < month; monthCount++)
{
totalMonthDays += computeNumDays(monthCount, year);
}
int offset = (totalYearDays + totalMonthDays) % 7;
return offset;
}
bool isLeapYear(int yearCount)
{
//Equation for determining if a year is a leap year or not
if ((yearCount % 4 == 0 && yearCount % 100 != 0) || (yearCount % 400 == 0))
return true;
else
return false;
}
This program asks for a number from 1 to 12 and prints out the month and season correspondent to the input number, if the user types anything else than numbers from 1 to 12 an error message is displayed. It looks like the first if statement runs perfectly with its correspondent nested statements but the next else if statements wont work, try with 1, 2, and 12 and the program will work perfectly, but it doesn't with any other value, does anyone knows why this is happening?
Here's the program...
#include <iostream>
#include <string>
using namespace std;
int main() {
int number;
string season, month;
cout << "Welcome! This program will provide the season of the year based on the month you enter, 1 corresponding to January, 2 for February and so on until 12 for December" << endl << endl;
cout << "Enter a number between 1 and 12: ";
cin >> number;
if (number == 1 || 2 || 12)
{
if (number == 1)
{
month = "January";
season = "winter";
}
else if (number == 2)
{
month = "February";
season = "winter";
}
else if (number == 12)
{
month = "December";
season = "winter";
}
}
else if (number == 3 || 4 || 5)
{
if (number == 3)
{
month = "March";
season = "spring";
}
else if (number == 4)
{
month = "April";
season = "spring";
}
else if (number == 5)
{
month = "May";
season = "spring";
}
}
else if (number == 6 || 7 || 8 )
{
if (number == 6)
{
month = "June";
season = "summer";
}
else if (number == 7)
{
month = "July";
season = "summer";
}
else if (number == 8)
{
month = "August";
season = "summer";
}
}
else if (number == 9 || 10 || 11)
{
if (number == 9)
{
month = "September";
season = "fall";
}
else if (number == 10)
{
month = "October";
season = "fall";
}
else if (number == 11)
{
month = "November";
season = "fall";
}
}
else
{
cout << "You entered an ivalid value";
return 0;
}
cout << "-------------------------------------------------------------" << endl;
cout << "You entered " << number << ". " << month << " is the " << season << " season.";
return 0;
}
I believe that the || operator does not work how you are using it. That is, the statement if (number == 1 || 2 || 12) is likely being converted into boolean values:
number == 1
2
12
So imaging you are forcing each of those values into a boolean representation. There's probably a more exact way of explaining this though.
The fix is likely to do this:
if (number == 1 || number == 2 || number == 12)
etc.
I have a c++ assignment that asks to enter one date and second date and find the number of days between the two, including leap years. My code displays the correct values for two dates, however when tested for 1/2/3 to 3/21/12345, it displays 4507994, which is 93 more days than the correct value of 4507901. Why is this happening? I am including cstdlib and iostream
using namespace std;
bool isLeapYear (int year)
{
bool tf;
if (year%4 !=0)
{
tf = true;
}
else if (year%4 == 0)
{
if (year%1000 == 0)
{
if (year %400 == 0)
{
tf = false;
}
else
{
tf = true;
}
}
tf = false;
}
return tf;
}
int last_day(int month, int year)
{
int lday;
if (month == 2)
{
if (isLeapYear (year) == false)
{
lday = 29;
}
else
{
lday = 28;
}
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8|| month == 10 || month ==12)
{
lday = 31;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
lday = 30;
}
return lday;
}
void howlongwait (int &month, int &day, int &year)
{
if (day == last_day(month, year))
{
if (month == 12 && day == 31)
{
month = 1;
day = 1;
year = year+1;
}
else
{
month = month+1;
day = 1;
year = year;
}
}
else
{
month = month;
day = day+1;
year = year;
}
}
int main()
{
int month, month2, day2, day, year, year2;
int days = 0;
char trash, garb, garb2, trash2;
cout << "Enter start date (no spaces): " << endl;
cin >> month >> trash >> day >> garb >> year;
cout << "Enter end date (no spaces): " << endl;
cin >> month2 >> trash2 >> day2 >> garb2 >> year2;
if (year2 < year)
{
cout << "Never..." << endl;
return 0;
}
else
{
while ((month != month2) || (day != day2) || (year != year2))
{
howlongwait (month, day, year);
days=days+1;
}
}
cout << "You have to wait " << days << " days" << endl;
}
`
isLeapYear should be fixed like this:
bool isLeapYear (int y)
{
return y%4 == 0 && (y%400 == 0 || y%100 != 0);
}
and please replace isLeapYear (year) == false to isLeapYear (year) == true.
Your version of isLeapYear is:
year%1000 == 0 should be year%100 == 0
corresponding else is missing
flip true and false, or change the function name if you need
I have an assignment that essentially requires me to use a class to output the day of the week based upon input from the user EX: user enters sat (first three letters) --> output = This day of the week is: 6. We're also required for the program to also ouput the next six days following the day specified by the user, ex:
Input:
tue
Output:
This day of the week is: 2
This day of the week is: 3
This day of the week is: 4
This day of the week is: 5
This day of the week is: 6
This day of the week is: 7
This day of the week is: 1
My program is bit messy as I initially misunderstood the question. I was able to develop a functioning program that allows the user to input a number for the day, but not the first three letters as that part of the code seems not to be functioning whatsoever.
Any suggestions or solutions would be immensely appreciated. Thank you in advance.
#include <iostream>
#include <string>
using namespace std;
//
// Definition of the DayOfWeek class
//
class DayOfWeek
{
public:
DayOfWeek(int dayNum);
// Precondition: The parameter dayNum contains a valid
// day number (1 - 7)
// Postcondition: The member variable day has been set to
// the value of the parameter dayNum.
DayOfWeek();
// Sets the member variable month to 1 (defaults to January).
DayOfWeek(char fL, char sL, char tL);
void outputDayNumber();
// Postcondition: The member variable day has been output
// to the screen if it is valid; otherwise a "not valid"
// message is printed.
void outputDayLetters();
// Postcondition: The first three letters of the name of the
// day has been output to the screen if the day is
// valid (1 - 12); otherwise a message indicating the month
// is not valid is output.
DayOfWeek NextDay();
// Precondition: The month is defined and valid.
// Returns the next day as an object of type Day.
private:
int day;
};
int main()
{
//
// Variable declarations
//
int DayNum;
string DayWord;
char letter1, letter2, letter3; // first 3 letters of the day
char testAgain; // y or n - loop control
//
// Loop to test the next month function
//
do {
cout << endl;
cout << "Enter a day number: ";
cin >> DayNum;
DayOfWeek testDay(DayNum);
cout << endl;
cout << "This day ..." << endl;
testDay.outputDayNumber();
testDay.outputDayLetters();
DayOfWeek next = testDay.NextDay();
cout << endl;
cout << "Next day ..." << endl;
next.outputDayNumber();
next.outputDayLetters();
//
// See if user wants to try another month
//
cout << endl;
cout << "Do you want to test again? (y or n) ";
cin >> testAgain;
} while (testAgain == 'y' || testAgain == 'Y');
return 0;
}
DayOfWeek::DayOfWeek()
{
day = 1;
}
DayOfWeek::DayOfWeek(int dayNum)
{
if (dayNum >= 1 && dayNum <= 7)
day = dayNum;
else {
dayNum = 1;
}
}
void DayOfWeek::outputDayNumber()
{
if (day >= 1 && day <= 7)
cout << "Day: " << day << endl;
else
cout << "Error - The day is not valid!" << endl;
}
void DayOfWeek::outputDayLetters()
{
switch (day)
{
case 1:
cout << "1" << endl;
break;
case 2:
cout << "2" << endl;
break;
case 3:
cout << "3" << endl;
break;
case 4:
cout << "4" << endl;
break;
case 5:
cout << "5" << endl;
break;
case 6:
cout << "6" << endl;
break;
case 7:
cout << "7" << endl;
break;
default:
cout << "Error - the day is not valid!" << endl;
}
}
DayOfWeek::DayOfWeek(char firstL, char secondL, char thirdL)
{
/**
*check to for the first characters or letter of the day
*prints the day based on the characters user input
*check if the characters is valid
*/
if (firstL >= 65 && firstL <= 90) {
firstL += 32;
}
if (secondL >= 65 && secondL <= 90) {
secondL += 32;
}
if (thirdL >= 65 && thirdL <= 90) {
thirdL += 32;
}
if (firstL == 'm' && secondL == 'o' && thirdL == 'n') {
day = 1;
}
else if (firstL == 't' && secondL == 'u' && thirdL == 'e') {
day = 2;
}
else if (firstL == 'w' && secondL == 'e' && thirdL == 'd') {
day = 3;
}
else if (firstL == 't' && secondL == 'h' && thirdL == 'u') {
day = 4;
}
else if (firstL == 'f' && secondL == 'r' && thirdL == 'i') {
day = 5;
}
else if (firstL == 's' && secondL == 'a' && thirdL == 't') {
day = 6;
}
else if (firstL == 's' && secondL == 'u' && thirdL == 'n') {
day = 7;
}
else {
day = 0;
cout << "Invalid Day" << endl;
}
}
DayOfWeek DayOfWeek::NextDay() {
int d = (day % 7) + 1;
return DayOfWeek(d);
}
UPDATE:
Changed my code up entirely but still need to implement the next day and then
the next 6 days function.
NEW CODE :
#include <iostream>
#include <string>
using namespace std;
class DayOfWeek
{
string day;
public:
DayOfWeek();
DayOfWeek(string day_name);
void print_car(ostream& ins);
};
int main() {
string day_name;
cout << "Enter the first three letters of the day :" << endl;
cin >> day_name;
DayOfWeek my_car(day_name);
my_car.print_car(cout);
return 0;
}
DayOfWeek::DayOfWeek(string day_name) {
day = day_name;
}
void DayOfWeek::print_car(ostream& outs) {
if (day == "Mon" || day == "mon") {
outs << "This is day 1 of the week" << endl;
}
if (day == "Tue" || day == "tue") {
outs << "This is day 2 of the week" << endl;
}
if (day == "Wed" || day == "wed") {
outs << "This is day 3 of the week" << endl;
}
if (day == "Thu" || day == "thu") {
outs << "This is day 4 of the week" << endl;
}
if (day == "Fri" || day == "fri") {
outs << "This is day 5 of the week" << endl;
}
if (day == "Sat" || day == "sat") {
outs << "This is day 6 of the week" << endl;
}
if (day == "Sun" || day == "sun") {
outs << "This is day 7 of the week" << endl;
}
}
Final Update:
I am nearly at a final solution but I am having a issues declaring a
new object for the final part of my problem.
The code is here...
#include <iostream>
#include <string>
using namespace std;
class DayOfWeek
{
string day;
// int nday;
public:
DayOfWeek();
DayOfWeek(string day_name);
void print_day(ostream& ins);
};
int main() {
string day_name;
int nday;
cout << "Enter the first three letters of the day :" << endl;
cin >> day_name;
DayOfWeek week_day(day_name);
week_day.print_day(cout);
DayOfWeek next_day;
int d = _____;
int current = (d % 7) + 1;
int count = 0;
while (count < 7) {
next_day = DayOfWeek(current);
next_day.print_day(cout);
count++;
}
return 0;
}
DayOfWeek::DayOfWeek(string day_name) {
day = day_name;
}
void DayOfWeek::print_day(ostream& outs) {
int dayNumber = 0;
if (day == "Mon" || day == "mon") {
dayNumber = 1;
}
if (day == "Tue" || day == "tue") {
dayNumber = 2;
}
if (day == "Wed" || day == "wed") {
dayNumber = 3;
}
if (day == "Thu" || day == "thu") {
dayNumber = 4;
}
if (day == "Fri" || day == "fri") {
dayNumber = 5;
}
if (day == "Sat" || day == "sat") {
dayNumber = 6;
}
if (day == "Sun" || day == "sun") {
dayNumber = 7;
}
outs << "This is day " << dayNumber << " of the week." << endl;
}
I have some issue being able to convert the user inputted name of the day, into an integer that I can use to plug into the next_day function.
I am also having an error in the while loop
while (count < 7) {
next_day = DayOfWeek(current);
next_day.print_day(cout);
count++;
}
specifically line:
next_day = DayOfWeek(current);
The error says that there's no instance of DayOfWeek::DayOfWeek matching the argument list.
I feel like I'm nearly there if not for these couple of bumps.
Code below should do some of what you need.
Instead of using chars, I've used a string. That way you can compare the whole string in one go rather than 1 char at a time.
Also for comparing the input against the day-words, i have created an array of Strings. The index corresponds to the day, i.e 0=sunday, 1=monday etc.
This is neat for 2 reasons, we can compare with a loop easily, and as soon as we find the matching day, we just grab the index and there we have our day number. Store that number, and then we can return that for the day number, or use the day as an index in the array to return the day word instead.
Hopefully this is clear, you still need to make a function to return the next day, but the 3 letter input should work.
#include <iostream>
#include <cctype> // std::tolower
#include <algorithm> // std::transform
class DayOfWeek
{
private:
const static std::string days[7];
private:
int day;
public:
DayOfWeek();
DayOfWeek(int);
DayOfWeek(std::string);
void outputDayNumber();
void outputDayLetters();
};
// if we have an array of strings, we can use these for comparison and also Day value
const std::string DayOfWeek::days[7] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
DayOfWeek::DayOfWeek() {
}
DayOfWeek::DayOfWeek(int dayNum)
{
day = dayNum;
}
DayOfWeek::DayOfWeek(std::string dayWord)
{
day = 0; // set Sunday as default
// truncate the string to 3 letters
if (dayWord.length() > 3)
dayWord = dayWord.substr(0, 3);
// to lower case - makes comparison case insensitive
std::transform(dayWord.begin(), dayWord.end(), dayWord.begin(), [](unsigned char c) { return std::tolower(c);});
// compare with our list of strings, if none found, it will stay as default
for (int i = 0; i < 7; ++i)
if (days[i] == dayWord)
{
day = i;
break;
}
}
void DayOfWeek::outputDayLetters() {
std::cout << std::endl << "Day Letters: " << days[day];
}
void DayOfWeek::outputDayNumber() {
std::cout << std::endl << "Day Numbers: " << day;
}
int main()
{
int dayNum;
std::string dayWord;
std::cout << std::endl << "Enter a weekday: ";
std::cin >> dayWord;
std::cout << std::endl << "You entered " << dayWord;
DayOfWeek dayObject = DayOfWeek(dayWord);
dayObject.outputDayNumber();
dayObject.outputDayLetters();
}
I'm required to write a program that gives a person’s day and month of birth and will then figure out the person’s horoscope sign. The program should prompt the user for a number of persons. When the user responds, the program needs to automatically generate day and month and figure out and display the corresponding horoscope sign.
The program also needs to track number of persons for each sign and display that statistics. After that, the program needs to prompt the user if they want to go again. If the user responds with either a ‘Y’ or a
‘y’ the program needs to repeat the process until the user says that s/he is done.
I have a learning disability and i find it hard to understand sometimes what the questions that are being asked. I do not know how to answer them sometimes. So please be patience with me.
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
int month, date, i, nOfPeople;
i = 0;
cout << "How many people: " << endl;
cout << "Born on Horoscope Sign" << endl;
cout << "-------------------------" << endl;
cin >> nOfPeople;
while (i < nOfPeople) {
// initialize random seed:
srand(time(NULL))
// generate month and date
month = rand() % 12 + 1;
date = rand() % 28 + 1;
if (month == 3 && date >= 21 || month == 4 && date <= 19) {
cout << "4/19 Aries" << endl;
} else if (month == 4 && date >= 20 || month == 5 && date <= 20) {
cout << "5/7 Taurus" << endl;
} else if (month == 5 && date >= 21 || month == 6 && date <= 21) {
cout << "Gemini" << endl;
} else if (month == 6 && date >= 22 || month == 7 && date <= 22) {
cout << "7/8 Cancer" << endl;
} else if (month == 7 && date >= 23 || month == 8 && date <= 22) {
cout << "8/17 Leo" << endl;
} else if (month == 8 && date >= 23 || month == 9 && date <= 22) {
cout << "Virgo" << endl;
} else if (month == 9 && date >= 23 || month == 10 && date <= 22) {
cout << "Libra" << endl;
} else if (month == 10 && date >= 23 || month == 11 && date <= 21) {
cout << "11/3 Scorpio" << endl;
} else if (month == 11 && date >= 22 || month == 12 && date <= 21) {
cout << "12/11 Saguittarius" << endl;
} else if (month == 12 && date >= 22 || month == 1 && date <= 19) {
cout << "12/22 Capricorn" << endl;
} else if (month == 1 && date >= 20 || month == 2 && date <= 18) {
cout << "Aquarius" << endl;
} else if (month == 2 && date >= 19 || month == 3 && date <= 20) {
cout << "2/21 Pieces" << endl;
}
i++;
}
return 0;
}
std::rand and std::srand are defined in <cstdlib>. Include that header then you should be able to use those functions.