dayOfTheWeek

I can't get this program to add or subtract.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1873 Views - Last Post: 12 May 2010 - 10:35 AM Rate Topic: -----

#1 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

dayOfTheWeek

Posted 11 May 2010 - 12:58 PM

I can't get this program to add or subtract.



#include <iostream> 
#include <string> 

using namespace std; 

class DayOfTheWeek  
{ 
public: 
	DayOfTheWeek();
	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. 
	
	string plusOneDay();
	string minusOneDay();
	string addDays();
	

private: 
	string day; // This is where the value of the day attribute is stored. 
	string dayName;
	int dayValue;
int numAdd;
}; 

DayOfTheWeek::DayOfTheWeek()
{


}





string DayOfTheWeek::getDay() const  
{ 
	return day;  
} 
void DayOfTheWeek::setDay(string newDay) 
{ 
	day = newDay;  
} 





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

string DayOfTheWeek::plusOneDay()
{

dayValue++;
return dayName;

}
string DayOfTheWeek::minusOneDay()
{
  if (dayValue == 0) 
                dayValue = 6; 
        else 
                dayValue = (dayValue - 1); 
        return dayName; 

}
string DayOfTheWeek::addDays()
{
 
 dayValue = dayValue ++; 
        if(dayValue < 0) dayValue +=7; 
        return dayName; 
}






int main() 
{ 
int numAdd;
int numSubtract; 

	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(); 
		string nextDay = tuesday.getDay();

DayOfTheWeek week;
week.addDays();
week.minusOneDay();
week.printDay();

	
	cout << "The value of the monday object is " << currentDay << "." << endl; 
	cout << "The day after " << currentDay << " is ";
	tuesday.printDay(); 
	cout << "." << endl;; 
	cout << "The day that comes before " << currentDay << " is ";
		sunday.printDay();
	cout << "." << endl;;
	cout << "Please input a number of days you wish to add to " << currentDay << endl;
	cin >> numAdd ;
	cout << currentDay << " + " << numAdd << " = "<< monday.addDays();
	
	monday.printDay();
	cout << endl;
	cout << "Please input a number you wish to subtract from " << currentDay << endl;
	cin >>numSubtract;
	cout << currentDay << " - " << numSubtract << " = " 
		<< week.minusOneDay();
	week.printDay();
	cout <<endl;
	





	

system("PAUSE");
return 0; 
}



Is This A Good Question/Topic? 0
  • +

Replies To: dayOfTheWeek

#2 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 11 May 2010 - 01:55 PM

The programs compiles but to add to monday is not working. Do I need to convert a int to a string. Something like this string DayOfTheWeek::convert to string(int).
Was This Post Helpful? 0
  • +
  • -

#3 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 11 May 2010 - 03:54 PM

Can anyone see where I went wrong?
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • C++ Programmer
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,710
  • Joined: 18-April 09

Re: dayOfTheWeek

Posted 11 May 2010 - 07:46 PM

Look at your function:
string DayOfTheWeek::addDays()
{
 
 dayValue = dayValue ++; 
        if(dayValue < 0) dayValue +=7; 
        return dayName; 
}


What do you see in that?

How many days are you going to add? You need to pass in an integer.

Your bad design is crippling your program. Think about how your program should work on paper. Let's do a little test case.

Say we start with Monday.

The user wants the day 3 days after Monday.
0 - Sunday
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday

All we have to do is add 3 to the day and we get Thursday.
Was This Post Helpful? 1
  • +
  • -

#5 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 11 May 2010 - 07:53 PM

dayValue ++; 
  
        if(dayValue > 6) 
dayValue =0; 


Should my logic look like this? I want to pass an int,but return a string.
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 494
  • View blog
  • Posts: 1,542
  • Joined: 20-March 10

Re: dayOfTheWeek

Posted 11 May 2010 - 08:59 PM

View Postcntc4 c++, on 11 May 2010 - 06:53 PM, said:

dayValue ++; 
  
        if(dayValue > 6) 
dayValue =0; 


Should my logic look like this? I want to pass an int,but return a string.


I also think your program needs to redesigned.
its not very economical.
you should only have declared one new class not one for each day
have a public member which is an array of type string
like
public:
static const int days[7];
static const string day[7];


then construct them something like this

const string  week::day[7] = {"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
const int week::days[7] = {1,2,3,4,5,6,7};



Then move between the two to marry up what day it is from an int value
Was This Post Helpful? 1
  • +
  • -

#7 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 11 May 2010 - 09:03 PM

How would you call this so it adds the days together? It seems like it would be easier to just add the int instaed of converting a int into a string. Can you tell me if I'm calling the function correctly?
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 494
  • View blog
  • Posts: 1,542
  • Joined: 20-March 10

Re: dayOfTheWeek

Posted 11 May 2010 - 09:16 PM

View Postcntc4 c++, on 11 May 2010 - 08:03 PM, said:

How would you call this so it adds the days together? It seems like it would be easier to just add the int instaed of converting a int into a string. Can you tell me if I'm calling the function correctly?



You dont have to convert an int to string just set up two arrays as shown then the int
will give you both a string and an integer value.

I have written this entire program for someone else who asked the same question maybe he is in your class,
so I know what Im talking about.

Just trying to steer you in the right direction.

The add days part of the program is the hardest to write.

you need to come up with an equation that will do want you want..
for this you will need a pen and paper.

then this equation will return a value of 0 to 6 (preferably)
no matter how many days you add to it.
then you compare that against your string array and get the string day ie monday or wednesday for example.

I cant post the entire code for you as I have gotten into trouble in the past for that.

but thats the way I would approach it.
although I do concede that this program could be written many different ways.

This post has been edited by snoopy11: 11 May 2010 - 09:21 PM

Was This Post Helpful? 1
  • +
  • -

#9 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 11 May 2010 - 09:25 PM

I'm not really sure, I will work at it...Thanks alot for your help!!!.....Maybe if I read some more I will see something that i'm missing.

This post has been edited by cntc4 c++: 11 May 2010 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 11 May 2010 - 09:31 PM

Can you tell me what i wrote in main() is correct

This post has been edited by cntc4 c++: 11 May 2010 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: dayOfTheWeek

Posted 11 May 2010 - 10:19 PM

View Postcntc4 c++, on 12 May 2010 - 12:03 AM, said:

How would you call this so it adds the days together? It seems like it would be easier to just add the int instaed of converting a int into a string. Can you tell me if I'm calling the function correctly?


Absolutely, you should do the arithmetic with the ints, and only convert to string at the very end when the function is ready to return the result. But look at your function:
string DayOfTheWeek::addDays()
{
    dayValue = dayValue ++; 
    if(dayValue < 0) dayValue +=7; 
    return dayName; 
}


The idea of adding days is pretty bizarre to begin with. In all my life I've never been asked "What is Thursday plus Saturday?". But oh, well, if that's what your teacher wants I guess you have to do it.

So what is this function doing? First you're adding 1 to the dayValue, and then if dayValue is LESS THAN 0 you add 7? Where does that idea come from? How can dayValue ever be less than 0? and why add 7? That might make sense if you were SUBTRACTING days, but certainly not for adding them.

Number 1, addDays has to know which day is being added, so you should have a string& (string reference) argument to pass in the day name. Then inside the function you convert that day name to an int, to be added to the intValue of "this" day. And to "wrap around" when you go past Sunday -- modulus arithmetic! Do you know what that is? You know about the mod operator % don't you?

This post has been edited by r.stiltskin: 11 May 2010 - 10:51 PM

Was This Post Helpful? 1
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: dayOfTheWeek

Posted 12 May 2010 - 05:43 AM

View Postcntc4 c++, on 12 May 2010 - 11:53 AM, said:

I want to pass an int,but return a string.


As well as all the other valuable hints you have been given in here I noticed the comment above in one of your posts.

If you want to "pass an int" then you probably need to "pass an int".

At the moment your method returns a string and has no input parameters
string DayOfTheWeek::addDays()



If you want to pass in an int then you are going to have to put in code that allows you to pass in an int.
Was This Post Helpful? 1
  • +
  • -

#13 cntc4 c++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-May 10

Re: dayOfTheWeek

Posted 12 May 2010 - 06:08 AM

string DayOfTheWeek::addDays(int)
{
dayValue+= 1; 
    if (dayValue > 7) { 
        dayValue %= 7; 
    } 
    day = convertToString(dayValue); 
 
    return dayValue; 
} 



Something like this.

This post has been edited by cntc4 c++: 12 May 2010 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: dayOfTheWeek

Posted 12 May 2010 - 06:13 AM

View Postcntc4 c++, on 11 May 2010 - 10:53 PM, said:

Should my logic look like this? I want to pass an int,but return a string.


I disagree with this. The int dayValue is a private implementatation detail that should remain private, i.e., hidden from the user. The specification is to add DAYS, not ints, so you should pass a string to the function, and the function should return a string.

The user should not be burdened with knowing how the class accomplishes its job, and therefore should not have to convert the day string to an int to pass to the function. Converting the input string to int, performing the arithmetic operations, and converting the resulting int back to a string, should all be done inside that function.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: dayOfTheWeek

Posted 12 May 2010 - 06:15 AM

View Postcntc4 c++, on 12 May 2010 - 10:08 PM, said:

Something like this.


I am about to go to bed so I know what it is like to be tired and if that's the reason for this bit of code
string DayOfTheWeek::addDays(int)


then you have my sympathy.

However, you are working on classes so you must have mastered the basics of arguments to a function/method before getting this far.
Does the error in the above jump out at you now that I reflect it back at you?

This post has been edited by janotte: 12 May 2010 - 06:22 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2