C++ Problem

This is a discussion on C++ Problem within the C++ Programming forums, part of the General Programming Boards category; Ok so I am in a programming course and I am working on one of my labs. We are supposed ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    C++ Problem

    Ok so I am in a programming course and I am working on one of my labs. We are supposed to add on to our code from the previous week, this is what I have so far:

    Code:
    // DayOfTheWeek.cpp : main project file.
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class DayOfTheWeek 
    {
    public:
    	void setDay(string );
    		// setDay(string) takes a string parameter 
    		// and stores the value in the day attribute.
    	void printDay() const;
    		// printDay() prints the value of the day attribute
    		// on console output (cout).
    	string getDay() const;
    		// returns the value of the day attribute.
    	void plusOneDay();
    
    	void minusOneDay();
    
    	void addDays();
    	
    private:
    	string day; // This is where the value of the day attribute is stored.
    };
    
    string DayOfTheWeek::getDay() const 
    {
    	return day; 
    }
    void DayOfTheWeek::setDay(string newDay)
    {
    	day = newDay; 
    }
    void DayOfTheWeek::printDay() const 
    {
    	cout << day; 
    }
    void DayOfTheWeek::plusOneDay()
    {
    	
    }
    int main()
    {
        
    	DayOfTheWeek monday;
    	DayOfTheWeek tuesday;
    	DayOfTheWeek wednesday;
    	DayOfTheWeek thursday;
    	DayOfTheWeek friday;
    	DayOfTheWeek saturday;
    	DayOfTheWeek sunday;
    
    
    	// Set the values of the objects
    	monday.setDay("Monday");
    	tuesday.setDay("Tuesday");
    	wednesday.setDay("Wednesday");
    	thursday.setDay("Thursday");
    	friday.setDay("Friday");
    	saturday.setDay("Saturday");
    	sunday.setDay("Sunday");
    
    	// Get the value of the monday object and print it out
    	string currentDay = monday.getDay();
    	cout << "The value of the monday object is " << currentDay << "." << endl;
    
    	
    
    	// Print out the value of the tuesday object
    	cout << "The value of the tuesday object is ";
    	tuesday.printDay();
    	cout << "." << endl;;
    
    	// We're finished
        return 0;
    }
    Now some of this code is from the previous week and I may need to delete some of it, but I am just trying to get started on the task at hand. Basically I need 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.

    Set the day
    Print the day
    Return the day
    Return the next day
    Return the previous day
    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.

    Here is an example of the output:

    The value of the monday object is Monday.
    The day after monday is Tuesday.
    The day before monday is Sunday.
    Monday + 3 = Thursday.
    The value of the monday object is still Monday.
    Monday - 3 = Friday.

    I'm not really sure how to add and subtract days, any information would be very helpful! Thanks!

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I'm not really sure how to add and subtract days, any information would be very helpful! Thanks!
    The easiest way to add or subtract days is using an enum to hold all the days.
    Code:
    enum Days {             
       sunday,          // sunday = 0l
       monday,              // monday = 1
       tuesday,             // tuesday = 2
       wednesday,           // etc.
       thursday,
       friday,
       saturday,           
       
       total,
    };
    You already have most of the code required to complete this program done, now all you have to do is declare a type of Day and add or subtract using modular arithmetic. Also, if you plan on using this method you will need a method to convert from string to the enum an vice-versa. For this, create a function which contains a switch function to return the associated value.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    I see what you are saying, but if for example, the day is Monday, what happens when you add 12 to it? What will the day be then? And how does that work in the code?

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I see what you are saying, but if for example, the day is Monday, what happens when you add 12 to it? What will the day be then? And how does that work in the code?
    Basically each emun value is just an integer which translates to a number starting at 0 unless otherwise stated.
    So, if you add 1 to the enum value tuesday = 2 you will get 3 which is wednesday. Now this would mean nothing to you unless you convert that value into a string, perhaps using a switch statement.

    Also, if you were to add 12 to say monday = 1, you will get 13 which is no present in the enum and will give you and error.

    So, that is why I suggested you use modular arithmetic to wrap the days around.
    For example , the following expression -

    4 % 7 evaluates to 4.

    whereas the following expression

    12 % 7 evaluates to 5;

    hence you are always guaranteed to get a valid day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21