Help with Days of the Week!!!!

This is a discussion on Help with Days of the Week!!!! within the C++ Programming forums, part of the General Programming Boards category; I have an assignment due, and I am completely lost. I haven't taken C++ in years and now I am ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Help with Days of the Week!!!!

    I have an assignment due, and I am completely lost. I haven't taken C++ in years and now I am having to learn it in OOP, and though I remember some things, there is a lot I do not, and I am a total noob so still trying to learn. But what I am trying to do is write a program that has the user enter a day, and then uses that data to add one day to it, subtract one day too it, and then have the user enter a number, and take that number and add it to the day giving you what day it would be. I have gotten this far, but am stuck. I have no idea how I am supposed to add or subtract from a string array to get the days. Please Help!!!!!

    Code:
    #ifndef DOW_H
    #define DOW_H
    #include<string>
    using namespace std; 
    
    class Dow
    {
    private: 
    	string day;
    
    public:
    	Dow(string);
    	
    	void setDay(string); 
    	string getDay( ); 
    	void printDay( ) const; 
    	void plusOneDay( ); 
    	string minusOneDay( ); 
    	string addDay( );
    };
    
    Dow::Dow(string Day)
    { 
    	day = Day;
    };
    
    void Dow::setDay(string Day)
    {
    	day = Day;
    }
    
    string Dow::getDay( )
    {
    	return day;
    }
    
    void Dow::plusOneDay( )
    {
    
    }	
    
    
    
    #endif
    
    _____________________________________________________________________
    
    
    
    #include<iostream>
    #include<string>
    #include "Dow.h"
    using namespace std; 
    
    
    void main( )
    {
    
    	Dow myDay[7] = {"Sunday", "Monday","Tuesday","Wednesday",
    		                         "Thursday","Friday","Saturday"};
    	
    	
    		
    	
     
    
    
    
    
    
    
    
    	system("pause"); 
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't "add or subtract from a string array" at all, let alone to do anything useful, so that approach (such as it was) won't work. You can add or subtract from numbers, so you should explore that approach.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    I suggest using an enum for the days of the week.

    For example:

    Code:
    enum DAY {
      Sunday = 1,
      Monday = 2,
      Tuesday = 3, 
      Wednesday = 4,
      Thursday = 5,
      Friday = 6,
      Saturday = 7
    };
    Now check out an example program which uses this:

    C++ code - 60 lines - codepad
    Last edited by Programmer_P; 01-15-2011 at 05:58 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Smile Hmmm

    I will have to look into the Enum......that is something I was never taught, though looks like it might be the key I am looking for, if I can just figure out how it works. Thanks for the advice.

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    If you do end up using my approach, make sure to #include <map> in your source file. I forgot to include it in the code at codepad.org, and the compiler there is smart enough to include the file automatically anyway, which is why it worked, but I'm sure your compiler wont be, so you will need to add that line to your source file.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 47
    Last Post: 01-04-2008, 05:24 AM
  2. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 02:51 AM
  3. Force end
    By Redattack34 in forum C++ Programming
    Replies: 9
    Last Post: 09-07-2005, 12:16 AM
  4. The day of the week America was discovered
    By Gustaff in forum C Programming
    Replies: 3
    Last Post: 08-12-2005, 10:47 AM
  5. sorting days of the week
    By brodacz in forum C Programming
    Replies: 3
    Last Post: 08-13-2002, 01:36 PM

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