Classes Help

trouble intiating a class

Page 1 of 1

8 Replies - 779 Views - Last Post: 13 July 2010 - 07:32 AM Rate Topic: -----

#1 Light02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 10

Classes Help

Posted 11 July 2010 - 02:11 AM

ok I'm getting three errors in my program and I'm not sure how to fix them I'm gonna copy what I got down in my code to get another opinion.

though I guess some background is in order it's an assignment that has myself creating a class called DayOfTheWeek with three public functions setDay(), getDay() and printDay() now the output needed is the first day inputted and the next day after the first day. the assignment didn't say how to pull this off so I went with a user input method as shown below my first question deals with the errors I mentioned in lines 53, 58, and 69. The second question is such how should I word the if statement

#include <iostream>
#include <string>

using namespace std;

class DayOfTheWeek
{
public:
	void setDay(int);
	void printDay() const;
	void getDay(int&);

private:
	string Sun;
	string Mon;
	string Tues;
	string Wed;
	string Thurs;
	string Fri;
	string Sat;
};

void DayOfTheWeek::setDay(int day)
{
	if(day=1)
		Sun=day;

	else if(day=2)
		Mon=day;

	else if(day=3)
		Tues=day;

	else if(day=4)
		Wed=day;

	else if(day=5)
		Thurs=day;

	else if(day=6)
		Fri=day;

	else if(day=7)
		Sat=day;
}

void DayOfTheWeek::getDay(int day)
{
	return day;
}

void DayOfTheWeek::printDay()
{
	cout << day;
}

int main()
{
	DayOfTheWeek firstDay;
	DayOfTheWeek secondDay;

	int Day = 0;

	Day = firstDay.getDay();

	cout << "Welcome to the Set Day Program" << endl;
	cout << endl;
	cout << "please input what day it is:" << endl;
	cout << "1 = Sun" << endl;
	cout << "2 = Mon" << endl;
	cout << "3 = Tues" << endl;
	cout << "4 = Wed" << endl;
	cout << "5 = Thurs" << endl;
	cout << "6 = Fri" << endl;
	cout << "7 = Sat" << endl;
	cout << "Make your selection here: ";
	cin >> Day;
	cout << endl;

	cout << "Today is "; firstDay.printDay(); cout << endl;

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Classes Help

#2 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Classes Help

Posted 11 July 2010 - 03:13 AM

First of all, day is not a local variable in your printDay() nor a member of the DayOfTheWeek class.
That's the cause for the first error.

To getDay(), you need to pass a int variable, which you're not doing it in your main function.

Also it's better if you create an enum DAYS to contain all days of a week and use that in your program.

I've cleaned up your code and it looks like this now:

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

enum DAYS
{
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

class DayOfTheWeek
{
public:
	void setDay(DAYS sday) { day = sday; }
	void printDay() const { cout << day; }
	DAYS getDay() { return day; }

private:
    DAYS day;
};

int main()
{
	DayOfTheWeek firstDay;

	int day = 0;
    firstDay.setDay(SUNDAY);

    std::string days[] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
                          "THURSDAY", "FRIDAY", "SATURDAY" };
    
	cout << "Welcome to the Set Day Program" << endl;
	cout << endl;
	cout << "please input what day it is:" << endl;
	cout << "1 = Sun" << endl;
	cout << "2 = Mon" << endl;
	cout << "3 = Tues" << endl;
	cout << "4 = Wed" << endl;
	cout << "5 = Thurs" << endl;
	cout << "6 = Fri" << endl;
	cout << "7 = Sat" << endl;
	cout << "Make your selection here: ";
	cin >> day;
        cout << days[day - 1] << endl;
	cout << endl;

	cout << "Today is " << days[firstDay.getDay()] << endl;

	return 0;
}

Was This Post Helpful? 2
  • +
  • -

#3 Light02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 10

Re: Classes Help

Posted 11 July 2010 - 04:38 PM

ok that did help at that part firstDay needs to be what the user inputted into the command line. also I'm assuming to do the second day I just need to do day = days[day+1];?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_guest*


Reputation:

Re: Classes Help

Posted 12 July 2010 - 02:18 AM

You might like to try something like this (with a static class member):

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

class DayOfTheWeek
{
public:
	void set_day(int s_day) { day = s_day-1; }
	void print_day() const { cout << days[day]; }
	int get_day() { return day+1; }
	int get_next() { return (day+2) > 7 ? (day+1)%7+1 : day+2; }
	int get_plus( int count )
    { return (day+count) > 7 ? (day+count)%7+1 : day+count; }

private:
    int day; // valid values 0..6
    static const string days[];
};

const string DayOfTheWeek::days[] =
{ "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };

const string HEADER =
"Welcome to the Day of the Week Program\n\n"
"1 = Sunday\n"
"2 = Monday\n"
"3 = Tuesday\n"
"4 = Wednesday\n"
"5 = Thursday\n"
"6 = Friday\n"
"7 = Saturday\n"
"What day is it today (enter 1..7) : ";

const string ERROR = "\nInvalid ... only 1..7 allowed here ...";

bool more()
{
    cout << "\nMore (y/n) ? " << flush;
    string reply;
    getline( cin, reply );
    return reply[0] != 'n' && reply[0] != 'N';
}


int main()
{
    do
    {
        cout << HEADER << flush;
    	DayOfTheWeek today, tomorrow, future;
        int day = 0, jump = 9;
    	cin >> day;
    	if( day < 1 || day > 7 )
        { cin.clear(); cin.sync(); cout << ERROR; continue; }
    	//else ...
    	cin.sync();
        today.set_day( day );
        tomorrow.set_day( today.get_next() );
        future.set_day( today.get_plus( jump ) );

    	cout << "\nIf today is ";
    	today.print_day();
    	cout << "\nthen tomorrow is ";
        tomorrow.print_day();
        cout << "\nand " << jump << " days into the future (from today) is ";
        future.print_day();
    }
    while( more() );
}


Was This Post Helpful? 0

#5 Guest_guest*


Reputation:

Re: Classes Help

Posted 12 July 2010 - 02:49 AM

Oops ... "get_plus( ... )" should be as below ...

class DayOfTheWeek
{
public:
	void set_day(int s_day) { day = s_day-1; }
	void print_day() const { cout << days[day]; }
	int get_day() { return day+1; }
	int get_next() { return (day+2) > 7 ? (day+1)%7+1 : day+2; }
	int get_plus( int count )
    { return (day+1+count) > 7 ? (day+count)%7+1 : day+1+count; }

private:
    int day; // valid values 0..6
    static const string days[];
};

Was This Post Helpful? 0

#6 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Classes Help

Posted 12 July 2010 - 04:59 AM

View PostLight02, on 11 July 2010 - 11:38 PM, said:

ok that did help at that part firstDay needs to be what the user inputted into the command line. also I'm assuming to do the second day I just need to do day = days[day+1];?


No, not really. You've to use day to get the next day, because arrays have a zero based indexing system. Also if the user inputs 7 as day, the next valid day is 1(Sunday)
std::string nextDay = days[(day == 7) ? 1 : day];



Once you got the user input, you should also check if the user's input is valid (1 <= day <= 7).
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Classes Help

Posted 12 July 2010 - 06:57 AM

You know I see a lot of conditional logic that just does not need to be there...

Internally to the class day has a value of 0 though 6, to get the user "day" we add 1, to get convert the user's "day" into an internal one you just subtract 1.

Great so lets call "day" the one internal to the class (0-6) and "user_day" the user's version (1-7)

to find the next day:

next user_day = ((day + 1) % 7) + 1
N days in the future: user_day = ((day + N) % 7) + 1

here is one version of a program that does not use a class to solve the problem (which is probably what your assignment requires) but shows how an enum can be used to represent days of the week by overloading a couple operators:
#include <iostream>
#include <limits>

using namespace std;

//An enumeration for days... this counts as a "type"
enum DAYS {
    DAYS_BEGIN,
    SUNDAY = DAYS_BEGIN,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    DAYS_COUNT,
};

//Used for converting DAYS into a name when outputing
const char* DAY_NAMES[]= {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wendesday",
    "Thursday",
    "Friday",
    "Saturday"
};

//Overload the operator  DAYS + int to return a proper day.
DAYS inline operator+(const DAYS& day, const int& v) {
    // note that the % operator might return a negative value if v < 0,
    // so we will correct for that by adding DAYS_COUNT
    int value = (static_cast<int>(day) + v) % DAYS_COUNT;
    return static_cast<DAYS>((value + DAYS_COUNT) % DAYS_COUNT);
}

//Overload the operator  DAYS + int to return a proper day.
DAYS inline operator-(const DAYS& day, const int& v) {
    return day + (-v);
}

//Overload the output operator so that cout << SUNDAY prints "Sunday"
std::ostream& operator<<(std::ostream& out, const DAYS& day) {
    //note the +0 ensures that we get a proper day (not DAYS_COUNT or -1 or something like that).
    return out << DAY_NAMES[day + 0];
}

// Convert a user input day (1-7) into a DAYS value.
// note that SATURDAY + 1 = SUNDAY, so this will use the operator+ above to do our conversion for us.
DAYS inline convertUserDay(int user_day) { return SATURDAY + user_day; }

//Get user input of a day
DAYS getUserDay();

int main() {
    DAYS day = getUserDay();
    cout << endl;
    cout << "Day: " << day << endl;
    cout << "Next Day: " << (day + 1) << endl;
    cout << "Day + 10: " << (day + 10) << endl;
    cout << "Day + 30: " << (day + 30) << endl;
    cout << "Day before: " << (day - 1) << endl;
    cout << "10 Days before: " << (day - 10) << endl;
    cout << "30 Days before: " << (day - 30) << endl;
    return 0;
};

DAYS getUserDay() {
    int user_day = -1;
    while (user_day < 0) {
        for(int i = 1; i <= DAYS_COUNT; i++) {
            // The convertUserDay function will conver from our expected input (1-7) to a DAYS type
            //    which is overloaded on ostream<< to print a day of the week...
            cout << i << ": " << convertUserDay(i) << '\n';
        }
        cout << "Please select 1-7: ";
        cin >> user_day;
        if(cin.bad() || user_day < 1 || user_day > 7) {
            cout << "\nERROR getting input, please enter a number 1-7\n" << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            user_day = -1;
        }
    }
    return convertUserDay(user_day);
}

Was This Post Helpful? 0
  • +
  • -

#8 Guest_guest*


Reputation:

Re: Classes Help

Posted 12 July 2010 - 09:02 PM

View PostNickDMax, on 12 July 2010 - 05:57 AM, said:

You know I see a lot of conditional logic that just does not need to be there...


You are right ... so that class example is cleaned up and upgraded some below ... The OP, if just a beginner, may be somewhat overwhelmed with all the example code, by now. But ... C++ classes do become much more user-friendly with 'mucho' practice.

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

class DayOfTheWeek
{
public:
    friend ostream& operator<< ( ostream& os, const DayOfTheWeek& dw )
    { return os << days[dw.day]; }
    
    DayOfTheWeek( int d = 1 ) : day(d-1) {} // default constructor ...
    
	void set_day(int s_day) { day = s_day-1; }
	void print_day() const { cout << days[day]; }
	
	int get_day() { return day+1; }
	int get_next() { return (day+1)%7+1; }
	int get_plus( int count ) { return (day+count)%7+1; }
	
	string get_strday() { return days[day]; }
	string get_strnext() { return days[(day+1)%7]; }
	string get_strplus( int count ) { return days[(day+count)%7]; }

private:
    int day; // valid values 0..6
    static const string days[];
};

const string DayOfTheWeek::days[] =
{ "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };

const string HEADER =
"Welcome to the Day of the Week Program\n\n"
"1 = Sunday\n"
"2 = Monday\n"
"3 = Tuesday\n"
"4 = Wednesday\n"
"5 = Thursday\n"
"6 = Friday\n"
"7 = Saturday\n"
"What day is it today (enter 1..7) : ";

const string ERROR = "\nInvalid ... only 1..7 allowed here ...";

bool more()
{
    cout << "\nMore (y/n) ? " << flush;
    string reply;
    getline( cin, reply );
    return reply[0] != 'n' && reply[0] != 'N';
}


int main()
{
    DayOfTheWeek default_day;
    cout << "For class DayOfTheWeek, the value of the 'default_day' is "
         << default_day << " ...\n\n";
         
    do
    {
        cout << HEADER << flush;
        int day = 0, jump = 9;
    	cin >> day;
    	if( day < 1 || day > 7 )
        { cin.clear(); cin.sync(); cout << ERROR; continue; }
    	//else ...
    	cin.sync();
    	
        DayOfTheWeek today( day ), tomorrow, future;
        tomorrow.set_day( today.get_next() );
        future.set_day( today.get_plus( jump ) );

    	cout << "\nIf today is ";
    	today.print_day();
    	cout << "\nthen tomorrow is " << tomorrow
             << "\nand " << jump << " days into the future (from today) is "
             << future;
        
    	cout << "\nIf today is " << today
             << "\nthen tomorrow is " << today.get_strnext()
             << "\nand " << jump << " days into the future (from today) is "
             << today.get_strplus( jump );
    }
    while( more() );
}


This post has been edited by sarmanu: 12 July 2010 - 11:57 PM
Reason for edit:: Fixed code tags. Please paste the code in between [code]...[/code].

Was This Post Helpful? 0

#9 Guest_guest*


Reputation:

Re: Classes Help

Posted 13 July 2010 - 07:32 AM

Thank you sarmanu for fixing to [/code] my typo above for the ending code block delimiter mis-typed as [code]
Was This Post Helpful? 0

Page 1 of 1