Having problem getting code to even compile. Instructor insists array be in addDays and no adding of extra data members or methods to the class.

"Use an array of strings in addDays. It needs to be local to the method since that is the only place that it is needed. The data member needs to remain a string. The parameter for addDays is an integer which is the number of days to add or to subtract (if it is a negative number) from the current day.Use the modulus operator to make sure that the array index is in range. The idea is to "convert" or associate the string to/with the index. add (or subtract) the number of days to the index and then return the value of the string in the array cell . addDays takes one integer parameter. The array of strings should be in addDays. plusOneDay should call addDays(1) and return the value it gets back. minusOneDay should call addDays(-1) and return the value it gets back. The array of strings has 7 elements. "Sunday" should be the first one, not " ". " quote from instructor's postings.

You will want to compare the data member (day) with the array of strings to find the index of the current day. Then add the parameter to that index, mod it by 7 and then return the contents of the cell in the array that goes with the new index.

INSTRUCTIONS:
The class has from last week
1. one data member of type string called day.
2. one default constructor that assigns a string(a day of the week - pick your favorite) to day
3. an accessor method called getDay which returns a string and takes no parameters.
4. a mutator method called setDay that take a string as a parameter and returns void
5. a method called printDay that takes no parameters, returns void and outputs day (only) and nothing else.

Additional class methods
1. addOneDay – returns a string and takes no parameters, calls addDays with 1 as a parameter
2. minusOneDay – returns a string and takes no parameters, calls addDays with -1 as a parameter
3. addDays – returns a string and takes one integer parameter, adds a number of days (parameter) to the current day and returns the new day as a string – uses a local array of strings

Main
1. instantiates two objects.
2. Test all operations in the class: setDay, getDay, printDay, addOneDay, minusOneDay, addDays.

Do not add extra data members or methods to the class. All extra variables must be local.

This lab writes a program that contains a class that implements the days of the week. 
The program is 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, and
Calculate and return the day by adding a certain amount of days to the current day.
Created/Revised by Barbara Williams on September 8, 2010.*/

/* DaysOfTheWeek.h the specification file for the class DayOfTheWeek
   Class "DaysOfTheWeek" contains three public member functions
   and one private data member variable */

#include <iostream>		// Required by cout command
#include <string>		// Required by string data type	  

using namespace std;	// Default namespace defining library functions (e.g. cout, endl)

class DaysOfTheWeek
{
public:
	void setDay(string wkDay);	
	/* A mutator method called setDay that take a string as a parameter and returns void.
	   Function is to set the day.
	   The day is set according to the parameter.
	   Postconditions:	day   wkDay
						The function checks whether the value of wkDay is valid.
						If value is invalid the default value is assigned. */

	string getDay();		
	/* An accessor method called getDay which returns a string and takes no parameters.
	   Function is to return the day.
	   Postconditions: day  wkDay */

	void printDay();		
	/* A method called printDay that takes no parameters, returns void and outputs day (only) and nothing else.
	   Function is to print the day.
	   Postconditions: The day is printed in the form of a string.*/
	
	void plusOneDay();		
	/* An accessor method called plusOneDay that returns a string and takes no parameters, 
	   calls addDays with 1 as a parameter, and increments day plus one.
	   Function is to add one day to the current day. 
	   Postconditions: day++; */
	
	void minusOneDay();		
	/* An accessor method called minusOneDay returns a string and takes no parameters, 
	   calls addDays with -1 as a parameter.
	   Function is to subtract one day from the current day.
	   Postconditions: day--; */
	
	void addDays();			
	/* A method called addDays returns a string and takes one integer parameter,  adds a number of days (parameter) 
	   to the current day and returns the new day as a string – uses a local array of strings.
	   Function is to add days to the current day.
	   Postconditions:    */

	DaysOfTheWeek();			
	/* Default constructor
	   Function is to assign a string(a day of the week)to day.
	   The day is set to "Sunday"
	   Postconditions: day = "Sunday" */
	DaysOfTheWeek(int);
	~DaysOfTheWeek();

private:
	string day;  // Data member (variable) to store the day.
};/* Ch 12 Lab 2
This lab writes a program that contains a class that implements the days of the week. 
The program is 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, and
Calculate and return the day by adding a certain amount of days to the current day.
Created/Revised by Barbara Williams on September 8, 2010.*/

#include <iostream>				// Required by cout command
#include <string>				// Required by string data type
#include "TheDaysOfTheWeek.h"	//Required by class

using namespace std;			// Default namespace defining library functions (e.g. cout, endl)

/* DayOfTheWeekImp.cpp, the implementation file
   The user program that uses DayOfTheWeek class */

//Definitions of the member functions of the class DayOfTheWeek.
DaysOfTheWeek::DaysOfTheWeek()
{
	// sets the data member by default to the first day of the week.
	day = "Sunday";

}	// end default constructor

DaysOfTheWeek::DaysOfTheWeek(int currentDay)
{
	day = currentDay;
	currentDay = 2;
}

DaysOfTheWeek::~DaysOfTheWeek()
{

}	// end destructor

void DaysOfTheWeek::setDay(string wkDay)
{
	//sets the data member literally from what is passed as a parameter
	day = wkDay;
	
}	// end setDay mutator method

string DaysOfTheWeek::getDay()
{
	// returns the value stored in the data member
	return day;

}	// end getDay accessor method

void DaysOfTheWeek::printDay()
{
	cout << day;

}	// end printDay method

void DaysOfTheWeek::plusOneDay()
{
	
}	//end plusOneDay method

void DaysOfTheWeek::minusOneDay()
{
	
}	//end minusOneDay method

void DaysOfTheWeek::addDays()
{
		// an array of months
	string weekDays [] = {"Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday",  "Friday", "Saturday"};
	string newDay = weekDays[toDay-1]; //subtract 1 to match the array
	day = newDay;

}	//end addDays method
/* Ch 12 Lab 2
This lab writes a program that contains a class that implements the days of the week. 
The program is 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, and
Calculate and return the day by adding a certain amount of days to the current day.
Created/Revised by Barbara Williams on September 8, 2010.*/

#include <iostream>				// Required by cout command
#include <string>				// Required by string data type
#include "TheDaysOfTheWeek.h"	//Required by class

using namespace std;			// Default namespace defining library functions (e.g. cout, endl)

int main()
{
	cout << "Days of the week" << endl << endl;  

	// Instantiate two objects of class DaysOfTheWeek
	DaysOfTheWeek dayOne;            
	DaysOfTheWeek dayTwo;
	DaysOfTheWeek dayThree;

	// Local variables to hold object value
	string myDay = " ";
	string day = " ";
	
	// Ask the user for a day of the week
	cout << "Enter a day of the week such as Monday, Tuesday etc..." << endl;
	cin >> myDay;
	cout << endl;

	// Set firs day for one object using setDay from user entry.
	dayOne.setDay(myDay);
	
	// Set second day using the overloaded setDay using an integer
	dayTwo.setDay(3);
	cout << "The second day, using an integer, is set as " << dayTwo.getDay() << endl;
	cout << endl;

	// Set third day using a string variable with setDay 
	dayThree.setDay("Saturday");
	cout << "The third day, using a string variable, is set as " << dayThree.getDay() << endl;
	cout << endl;
	
	//Displays for Day 1  
	cout<<"** Day 1 **"<<endl;
	cout << "Today is: ";  
	dayOne.printDay();  

	// Displaying the value of minusOneDay.
	cout << "Yesterday was: ";  
	DaysOfTheWeek yesterday = dayOne.minusOneDay();  
	yesterday.printDay();  

	// Displaying the value of plusOneDay.
	cout << "Tomorrow will be: ";  
	DaysOfTheWeek tomorrow = dayOne.plusOneDay();  
	tomorrow.printDay();  
	
	// Using the addDays function to display value
	cout << "Five Days from today will be: ";  
	DaysOfTheWeek fiveFurther = dayOne.addDays(5);  
	fiveFurther.printDay();  
	cout << endl;  

	//Displays for Day 2  
	cout << "** Day 2 **" << endl;  
	cout << "Today is: ";  
	dayTwo.printDay();  

	// Displaying the value of minusOneDay.
	cout  << "Yesterday was: ";  
	yesterday = dayTwo.minusOneDay();  
	yesterday.printDay();  
	
	// Displaying the value of plusOneDay.
	cout << "Tomorrow will be: ";  
	tomorrow = dayTwo.plusOneDay();  
	tomorrow.printDay();  

	// Using the addDays function to display value 
	cout << "Two Days from today will be: ";  
	DaysOfTheWeek twoDaysFurther = dayTwo.addDays(2);  
	twoDaysFurther.printDay();  
	cout << endl;  

	//Displays for Day 3  
	cout << "** Day 3 **" << endl;  
	cout << "Today is: ";  
	dayThree.printDay();  

	// Displaying the value of minusOneDay.
	cout  << "Yesterday was: ";  
	yesterday = dayThree.minusOneDay();  
	yesterday.printDay();  

	// Displaying the value of plusOneDay.
	cout << "Tomorrow will be: ";  
	tomorrow = dayThree.plusOneDay();  
	tomorrow.printDay();  

	// Using the addDays function to display prior day's (minus) value
	cout << "Three Days prior from today will be: ";  
	DaysOfTheWeek priorDays = dayThree.addDays(-3);  
	priorDays.printDay();  
	cout << endl;  

	return 0;  
}	//end main

Recommended Answers

All 7 Replies

What are the errors you are getting? Also your addDay function is not right. it should accept an integer parameter and add that to the current day to get the new day.

What are the errors you are getting? Also your addDay function is not right. it should accept an integer parameter and add that to the current day to get the new day.

Could you give me an example on how to code addDay?
The errors are:
error C2664: 'DaysOfTheWeek::setDay' : cannot convert parameter 1 from 'int' to 'std::string'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
error C2440: 'initializing' : cannot convert from 'void' to 'DaysOfTheWeek'
1> Expressions of type void cannot be converted to other types
error C2440: 'initializing' : cannot convert from 'void' to 'DaysOfTheWeek'
1> Expressions of type void cannot be converted to other types
error C2660: 'DaysOfTheWeek::addDays' : function does not take 1 arguments
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
could be 'DaysOfTheWeek &DaysOfTheWeek::operator =(const DaysOfTheWeek &)'
1> while trying to match the argument list '(DaysOfTheWeek, void)'
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
could be 'DaysOfTheWeek &DaysOfTheWeek::operator =(const DaysOfTheWeek &)'
1> while trying to match the argument list '(DaysOfTheWeek, void)'
error C2660: 'DaysOfTheWeek::addDays' : function does not take 1 arguments
1>
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
1> while trying to match the argument list '(DaysOfTheWeek, void)'
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
1>
error C2660: 'DaysOfTheWeek::addDays' : function does not take 1 arguments
1>TheDaysOfTheWeekImp.cpp
error C2065: 'toDay' : undeclared identifier
- 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What are the errors you are getting? Also your addDay function is not right. it should accept an integer parameter and add that to the current day to get the new day.

Hoping that I just have to figure out addDay coding to get program to work right. Any help is greatly appreciated.

Okay, I've updated my addDays module to this:

const int size = 7;
	string weekDays [] = {"Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday", "Friday", "Saturday"};
	int found = -1;

	for(int i = 0; i < size; i++)
	{
		if (weekDays[i] == day)
			found = i;
	}
		return found + 1; // add 1 to return the usual equivalent numberical value 
		return day;

Now the error messages read:

error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'

error C2664: 'DaysOfTheWeek::setDay' : cannot convert parameter 1 from 'int' to 'std::string'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

error C2440: 'initializing' : cannot convert from 'std::string' to 'DaysOfTheWeek'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

error C2440: 'initializing' : cannot convert from 'std::string' to 'DaysOfTheWeek'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

error C2440: 'initializing' : cannot convert from 'std::string' to 'DaysOfTheWeek'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

error C2440: 'initializing' : cannot convert from 'std::string' to 'DaysOfTheWeek'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

error C2440: 'initializing' : cannot convert from 'std::string' to 'DaysOfTheWeek'
1> No constructor could take the source type, or constructor overload resolution was ambiguous


This is suppose be an easy assignment. However, I still need some help - Can anyone give me any examples of what I need to do - perhaps using months instead of days??

I could really use some help here.
The conversion is suppose to goes as follows
*Create an array of strings of size 7.
*Assign the days of the week from "Sunday" to "Saturday" in cells 0-6
*Do a search of the array until you find the content of a cell that is equal to your data member.
*When you find the cell, add the index of that cell to the parameter.
*If the result is negative then add 7 to it
*If the result is greater than 7 do a modulus of 7 on it.
*Finally return the value of the cell that is indicated by the calculation you just did.

This is what I have - can someone, please help.

string DaysOfTheWeek::addDays(int numDays) 
{
	string dayNames[7];
	
	const int size = 7;
	string weekDays [] = {"Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday", "Friday", "Saturday"};

	int found = -1;

	for(int i = 0; i < size; i++)
	{
		if (weekDays[i] == day)
			found = i;
	}
	return found + 1; // add 1 to return the usual equivalent numberical value
	return day;

	// setToday
	if (dayNames > 0 && dayNames < 8) 
	{
        today = dayNames - 1;
    }
	
	// setDay
	int i = 0;
    while (i < 7 && (dayNames[i].find(name) == -1)) i++;
    if (i < 7) {
        today = i;
    } else {
        cout << "No day is named '" << name << "'" << endl;
    } 
	// getToday
	return today + 1;

	// plusOneDay
	int tomorrow = today + 1;
    if (tomorrow > 6) tomorrow = 0;
    return dayNames[tomorrow];

	// minusOneDay
	int yesterday = today - 1;
    if (yesterday < 0) yesterday = 6;
    return dayNames[yesterday];
    return dayNames[(today + numDays) % 7];
	
	// getDay
	return dayNames[today];
}

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

Error codes are:

error C2446: '<' : no conversion from 'int' to 'std::string *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

error C2040: '<' : 'std::string [7]' differs in levels of indirection from 'int'

error C2065: 'today' : undeclared identifier

error C2065: 'name' : undeclared identifier

fatal error C1903: unable to recover from previous error(s); stopping compilation

1>TestProgram - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

what you need to do is take the integer supplied to the function and figure out what day of the week it should be. lets say i passed 13 to the function and the current day is Tuesday. i would mod 13 by 7 and get 6. so now i have to find out if 6 plus the index of Tuesday is past the end of the array. Tuesday is in index 2 and 6 + 2 is 8 which is past the end of the array. so now we subtract 7 from 8 and get 1. at index 1 in are array we get Monday. that is how you do it. i cant really give you the code for it since this is what you are supposed to be doing.

What does the code look like? I don't know where to begin. Is there a site that shows this code so I have an idea how to write it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.