The Days of The Week

Using Object Oriented Program Write the following program

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 4843 Views - Last Post: 21 November 2010 - 04:05 AM Rate Topic: -----

#1 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

The Days of The Week

Posted 05 May 2010 - 02:42 PM

I am in a beginning C++ Object Oriented programming class and I am supposed to:
Write a program that contains a class that implements the days of the week. The program should be able to perform the following on an object of the class.

1.Set the day
2.Print the day
3.Return the day
4.Return the next day
5.Return the previous day
6.Calculate and return the day by adding a certain amount of days to the current day. For example if you add 5 days to Saturday, the day to be returned is Thursday. Likewise, if we add 12 days to Wednesday, the the day returned will be Monday.

So far I have 1-3 completed and have created my array holding 7 string values one being each day of the week. From there I am kind lost on how I am supposed to call display the array and use a counter to make the calculations. Any help is appreciated, bellow is my code so far:

/****************************************************************
  Written By: Jeremy Duenas             Date: 5/1/2010
  Program: Sets variable day to a day of the week(Sunday by default)
                Prompts user to enter a day of the week
                                Prints the users data on the screen for the user to read
****************************************************************/


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

class DayOfTheWeek
{
private:
       string day;

public:
       void setDay(string);
       void getDay(string&);
       void plusOneDay();
       void minusOneDay();
       void addDays();
       void printDay();
};

int main()
{
       DayOfTheWeek myDay;      //default constructor, object myDay of class
DayOfTheWeek

       string today;                     //declares vaiable theday
       string gottenDay = " ";      //declares empty varriable gottenday

       DayOfTheWeek normalWeek[7];
               normalWeek[0].setDay("Monday");
               normalWeek[1].setDay("Tuesday");
               normalWeek[2].setDay("Wednesday");
               normalWeek[3].setDay("Thursday");
               normalWeek[4].setDay("Friday");
               normalWeek[5].setDay("Saturday");
               normalWeek[6].setDay("Sunday");



       cout << "Enter what day it is: ";
       getline(cin, today);

       myDay.setDay(today);        //sets the day to the value entered by the user
       myDay.printDay();             //prints to the screen the value
entered by the user

       myDay.getDay(gottenDay);
       myDay.printDay();




       system("pause");

}



void DayOfTheWeek::setDay(string anyDay){day = anyDay;}
//function setDay sets the variable day in the private class to
variable theDay

void DayOfTheWeek::getDay(string& x){x = day;}         //function
getDay gets the variable day from the private section of the class and
assignes it to the variable theDay

void DayOfTheWeek::printDay()
       //function printDay prints the data to the screen for the user
to read
{
       cout << "The day today is, "<< day << endl;
}




Is This A Good Question/Topic? 0
  • +

Replies To: The Days of The Week

#2 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: The Days of The Week

Posted 05 May 2010 - 03:08 PM

Usually when you are asked to return something from a function it means that you have a return statement in which that variable or constant is passed back to the calling function.

All of your functions are prototyped and implemented to return nothing.

Quote

3.Return the day
4.Return the next day
5.Return the previous day


For these you could either return an int or a string. You could return an int for the index of the array or a string for the value of that position in the array.

Assume the day is set to monday. That would be translated as normalWeek[0]. If you wanted to get the next day.... I will leave it at that.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: The Days of The Week

Posted 05 May 2010 - 03:31 PM

The array containing the day-name strings should not be declared in main. It will be much easier to make it a member variable of your DayOfTheWeek class. And it should not be an array of DayOfTheWeek objects -- it should be simply an array of strings.

And the private day member of your class can be an int instead of a string -- making it much easier for doing arithmetic on the day. That int should be the index of the particular day in the dayNames array.

Then, printing the name of the day is as simple as using that index to access the appropriate string in the dayNames array.

And to do the actual arithmetic on the days, use Modular Arithmetic, using the fact that the week has 7 days.
Was This Post Helpful? 2
  • +
  • -

#4 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: The Days of The Week

Posted 05 May 2010 - 06:38 PM

Those are all still prototypes because until I am able to make my array work I can cannot come up with the definition of those functions. Once I get my array working I can then move on to those prototype definitions.




View Postjoesyuh, on 05 May 2010 - 02:08 PM, said:

Usually when you are asked to return something from a function it means that you have a return statement in which that variable or constant is passed back to the calling function.

All of your functions are prototyped and implemented to return nothing.

Quote

3.Return the day
4.Return the next day
5.Return the previous day


For these you could either return an int or a string. You could return an int for the index of the array or a string for the value of that position in the array.

Assume the day is set to monday. That would be translated as normalWeek[0]. If you wanted to get the next day.... I will leave it at that.

Was This Post Helpful? 0
  • +
  • -

#5 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: The Days of The Week

Posted 05 May 2010 - 08:22 PM

I new I was going to come across that problem as all my variables are strings and you can't perform calculations on those, but every wat I try to convert the private string day to an int day, my entire program blows up. Can you show me how I go about making this change? I have also moved my Array as suggested to my class, not in my public portion of my class I have declared:
static string normalWeek[7];

but as a class is a blueprint I can not assign the values of my array. How do I go about accomplishing that? Thank you for all the insight and assistance.


View Postr.stiltskin, on 05 May 2010 - 02:31 PM, said:

The array containing the day-name strings should not be declared in main. It will be much easier to make it a member variable of your DayOfTheWeek class. And it should not be an array of DayOfTheWeek objects -- it should be simply an array of strings.

And the private day member of your class can be an int instead of a string -- making it much easier for doing arithmetic on the day. That int should be the index of the particular day in the dayNames array.

Then, printing the name of the day is as simple as using that index to access the appropriate string in the dayNames array.

And to do the actual arithmetic on the days, use Modular Arithmetic, using the fact that the week has 7 days.

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: The Days of The Week

Posted 05 May 2010 - 10:32 PM

Non-integer static members are initialized outside the class definition. Here's a very simplified example with all public members just to demonstrate the basics:
#include <iostream>
#include <string>
using namespace std;

class DayOfTheWeek {
public:
    static const string names[3];
    int day;
    string display() {
        return names[day];
    }
};

const string DayOfTheWeek::names[3] = {"Sunday", "Monday", "Tuesday"};

int main() {
    DayOfTheWeek dotw;
    dotw.day = 2;
    cout << dotw.display() << endl;
}


Was This Post Helpful? 1
  • +
  • -

#7 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: The Days of The Week

Posted 06 May 2010 - 04:34 AM

I don't know why my book didn't cover that, or maybe it did in a way I didn't understand it.
So now I have the user entering the day of the week, but can't do any calculations to that data because they are strings and not int, so maybe this is a beginning question that I just can't remember, but how am I suppose to input strings into int, or convert the strings to int so I can perform my calculations

This post has been edited by NoLove6: 06 May 2010 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

#8 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: The Days of The Week

Posted 06 May 2010 - 06:59 AM

I am confused why you down voted my response. I guess you dont want people to correct what you are doing wrong?
Was This Post Helpful? 1
  • +
  • -

#9 NoLove6  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 24-March 10

Re: The Days of The Week

Posted 06 May 2010 - 07:11 AM

I just said that it wasn't helpful because I know I am supposed to define those functions, as I explained I just couldn't do that until I have my array working so saying I need to define those functions didn't help in the question that I was asking. I thought that was more for me to keep track of which comments were helping and which ones weren't. If it effected you negatively in any way I apologize as that was not my intent.



View Postjoesyuh, on 06 May 2010 - 05:59 AM, said:

I am confused why you down voted my response. I guess you dont want people to correct what you are doing wrong?

Was This Post Helpful? 0
  • +
  • -

#10 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: The Days of The Week

Posted 06 May 2010 - 02:17 PM

I may be incorrect but I dont think that is how the system was intended to work. My response may not have been helpful. It certainly was not detrimental to what you are doing either. I Was not rude. I was genuinely trying to help you. Did it help you? No. Thats fine, not sure why it should be down voted though. I dont think you are gonna get a lot of help if you just down vote every thing you find does not help you. If people downvoted every unhelpful post no one would have a positive rating.

Whatever.
Was This Post Helpful? 1
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: The Days of The Week

Posted 06 May 2010 - 02:42 PM

I agree with joesyuh in that not helpful is not the same as unhelpful. I don't vote down anything unless I'm convinced the poster wasn't trying to help.

As for functions returning values, I don't think class methods should do that. They should be working on class variables.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: The Days of The Week

Posted 06 May 2010 - 02:51 PM

Quote

As for functions returning values, I don't think class methods should do that.
Nonsense. Here's a perfectly good counterexample. Operators. Operators are simply functions. If your operator function doesn't return a value, you can't create any expressions using that operator.
Was This Post Helpful? 1
  • +
  • -

#13 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: The Days of The Week

Posted 06 May 2010 - 03:09 PM

The reason I think class functions need to return values is because I don't like anything outside of the class accessing the class' variables directly. Yes, I know there are cases where this would be more beneficial, but generally speaking it is better to have a function access the variable. This way you can also implement some type of error checking whenever the variable needs to be changed or retrieved, thus avoiding possibly disastrous value assignment to a variable. If the class internally handles small amounts of error prevention, then the programmer cannot accidentally assign an improper value directly to that variable. Just my opinion.
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: The Days of The Week

Posted 06 May 2010 - 03:21 PM

Quote

but generally speaking it is better to have a function access the variable
I've seen this argument before. Classes are the same as structs. Do you then consider any idiomatic use of structs to be bad? You've created a type with certain variables, and then you want it exposed. So expose it directly.

Quote

This way you can also implement some type of error checking whenever the variable needs to be changed or retrieved, thus avoiding possibly disastrous value assignment to a variable.
Here's the problem. The problem isn't error checking. The problem is bad design. Either expose something or don't. Look at the C++ standard library for example. You don't have functions like set_size for vector and what not. There is no operation on member variables. Instead, there are abstractions: pushint values, random access, emptying, copying.
Was This Post Helpful? 1
  • +
  • -

#15 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: The Days of The Week

Posted 06 May 2010 - 03:37 PM

Quote

I've seen this argument before. Classes are the same as structs. Do you then consider any idiomatic use of structs to be bad? You've created a type with certain variables, and then you want it exposed. So expose it directly.


1. No
2. For simple situations yes, exposing the variable would pose no problems. For more complex examples it might not always be wise to do that. It is really up to the programmer, and whatever they feel comfortable with.

In the above example where dotw.day = 2; let's say I changed that to dotw.day = 6;. Well the array only has 3 elements, so when you try to access the 7th element things won't work out too well. Again, that example is a bit simple, but when you start getting complex and instead of assigning day to 2 you are using a bunch of return values from other functions and maybe a couple of loops or something else, and you are instead assigning day with another variable... who knows what could happen? Yes, a good programmer won't write code that does that, but everyone makes mistakes.

Quote

Here's the problem. The problem isn't error checking. The problem is bad design. Either expose something or don't. Look at the C++ standard library for example. You don't have functions like set_size for vector and what not. There is no operation on member variables. Instead, there are abstractions: pushint values, random access, emptying, copying.


I concede to this point, but the STL was not written by "some guy" without 10000000 other people reviewing it thoroughly.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2