Here's my problem. I'm writing a program that has the user input a string. That string is then compared to the values in a string array within a class.
Here's a sample of what I'm trying to do (sorry about the spacing. I use tabs, not spaces):
This is my header file:
#include <string>
#include <iostream>
using namespace std;
// class info
class dayOfTheWeek
{
public:
void setDay();
string getDay() const;
void printDay() const;
dayOfTheWeek();
void plusOneDay();
void minusOneDay();
void addDays();
private:
string dayName[7];
int dayNum;
};
// class implementation
dayOfTheWeek::dayOfTheWeek() // constructor
{
// init attributes
dayName[0] = "Sun";
dayName[1] = "Mon";
dayName[2] = "Tues";
dayName[3] = "Wed";
dayName[4] = "Thurs";
dayName[5] = "Fri";
dayName[6] = "Sat";
dayNum = 0;
} // end constructor
string dayOfTheWeek::getDay() const
{
// get the name of the day of the week, and send it back to the calling method
return dayName[dayNum];
} // end getDay() method
void dayOfTheWeek::printDay() const
{
// show the user the name of the day of the week
cout << "The day of the week is " << getDay() << endl << endl;
} // end printDay() method
void dayOfTheWeek::setDay()
{
// declare vars
string yourDay = "";
// get user input
cout << "Enter the name abbreviation of the day: ";
cin >> yourDay;
cout << endl;
for (int i = 0; yourDay != dayName[i] && i <= 6; i++)
{
// check for day equivalency
if (yourDay == dayName[i])
{
dayNum = i;
} // end if
} // end for
} // end setDay() method
void dayOfTheWeek::plusOneDay()
{
// show what the next day will be
// is today saturday?
if (dayNum != 6)
{
cout << "Since today is " << getDay() << " tomarrow will be "
<< dayName[dayNum + 1] << endl << endl;
}
else
{
cout << "Since today is " << getDay() << " tomarrow will be "
<< dayName[0] << endl << endl;
} // end if
}
void dayOfTheWeek::minusOneDay()
{
// show what the previous day was
// is today Sunday?
if (dayNum != 0)
{
cout << "Since today is " << getDay() << " yesterday was "
<< dayName[dayNum - 1] << endl << endl;
}
else
{
cout << "Since today is " << getDay() << " yesterday was "
<< dayName[6] << endl << endl;
} // end if
} // end minusOneDay() method
void dayOfTheWeek::addDays()
{
// declare var
int addedDays = 0;
cout << "The the number of the days that you would like to add: ";
cin >> addedDays;
// there's seven days in a week, so see how many days we actually have to add
int remainderDays = addedDays % 7;
if (remainderDays + dayNum > 7)
{
remainderDays = (remainderDays + dayNum) - 7;
// show what the day will be
cout << "In " << addedDays << " days, it will be "
<< dayName[dayNum + remainderDays] << endl << endl;
}
else
{
// show what the day will be
cout << "In " << addedDays << " days, it will be "
<< dayName[dayNum + remainderDays] << endl << endl;
} // end if
} // end addDays() method
this is my *.cpp file:
#include "Header.h"
#include <iostream>
using namespace std;
// function prototypes
void menu(dayOfTheWeek, dayOfTheWeek);
void performOp(int, dayOfTheWeek, dayOfTheWeek);
int main()
{
// create objects
dayOfTheWeek day1;
dayOfTheWeek day2;
// show menu items
menu(day1, day2);
return 0;
}
void menu(dayOfTheWeek day1, dayOfTheWeek day2)
{
// declare var
int choice = 0;
// let's do the loop
do
{
// show the user the choices
cout << "1: Change the day of the week" << endl;
cout << "2: Display the day of the week" << endl;
cout << "3: See what tomarrow is" << endl;
cout << "4: See what yesterday was" << endl;
cout << "5: See what day it will be in a given number of days" << endl;
cout << "6: End Program" << endl;
cout << "Choose an option from the list above: ";
// get the user's selection
cin >> choice;
// check user input and perform selected operation
if (choice < 1 || choice > 6)
cout << "Input Error" << endl;
else
// let's perform the option that the user selected
performOperation(choice, day1, day2);
// end if
}
while (choice < 1 || choice > 6);
} // end menu() function
void performOp(int userChoice, dayOfTheWeek day1, dayOfTheWeek day2)
{
// do the operation based on the choice that the user put into the menu()
switch (userChoice)
{
case 1:
// change the day on the second dayOfTheWeek object
day2.setDay();
// send the user back to the menu, keep the values of the objects intact
menu(day1, day2);
break;
case 2:
// show the user the values of both dayOfTheWeek objects
day1.printDay();
day2.printDay();
// send the user back to the menu, keep the values of the objects intact
menu(day1, day2);
break;
case 3:
day1.plusOneDay();
day2.plusOneDay();
menu(day1, day2);
break;
case 4:
day1.minusOneDay();
day2.minusOneDay();
menu(day1, day2);
break;
case 5:
day2.addDays();
menu(day1, day2);
default:
// all done, show a brief message for the switch statement, and end program
cout << "Terminating Program Operations..." << endl;
break;
} // end switch
} // end performOp()
The code compiles, but the printDay() method isn't working right. Anyone have any idea on what I'm doing wrong?
:NOTE:
I'm using Visual C++ 2008 Express