Hi
I am making a program that takes in a date (dd mm yyyy) & returns the day of the week(eg monday) of the input date.
My problem is that my fuction day_of_week is not returning the variable dayString into main. So dayString is not being output in main.

Any advice on whats is going wrong?

Below is the function playing up that should return dayString to main then main prints out the value of dayString.

string day_of_week(int day_numeral)
{
	string dayString;

	switch (day_numeral)
	{
	case 0:
		return dayString = "Sunday";
		break;
	case 1:
		return dayString = "Monday";
		break;
	case 2:
		return dayString = "Tuesday";
		break;
	case 3:
		return dayString = "Wednesday";
		break;
	case 4:
		return dayString = "Thursday";
		break;
	case 5:
		return dayString = "Friday";
		break;
	case 6:
		return dayString = "Saturday";
		break;
	default:
		return dayString = "wrong n_sum input";
	}


}

My full program:

#include <iostream>
#include <string>

using namespace std;

struct dateType
{
	int day;
	int month;
	int year;
};

// declare some functions if necessary
// you can refer to some examples introduced in week 12

void display(dateType date);


bool is_leapyear(dateType date);

int day_calculator(dateType date);

string day_of_week(int day_numeral);


int main()
{
	dateType date;
	string dayString;
	int day_numeral;

	// declare other variables if necessary

	cout << "Enter a date in the form of dd mm yyyy: " << flush;  // don't modify
	cin >> date.day >> date.month >> date.year;                   // don't modify

	// write your code here
	// find the corresponding day of week of date2
	// assign the corresponding string such as "Monday", "Tuesday" to dayString

	day_calculator(date);


	cout << dayString << endl;                                    // don't modify

	return 0;                                                     // don't modify
}


bool is_leapyear(dateType date)
{
	bool leap_yr = false;

	if (((date.year % 4) == 0) && ((date.year % 100) != 0) || date.year % 16 == 0)
	{
		return leap_yr = true;
	}

	else
		return leap_yr = false;

}


int day_calculator(dateType date)
{
	int century_numeral, year_numeral, month_numeral;
	int sum;
	int day_numeral;

	int century = date.year / 100;
	int decade = date.year % 100;

	century_numeral = 2*(3 - century % 4);
	year_numeral = (decade / 4);

	if (date.month ==01 && is_leapyear(date)) {
		month_numeral = 6;
	}
	else if (date.month == 02 && is_leapyear(date)) {
		month_numeral = 2;
	}
	else if (date.month == 01 && !is_leapyear(date) || date.month == 10) {
		month_numeral = 0;
	}
	else if (date.month == 02 && !is_leapyear(date)|| date.month == 03 || date.month == 11) {
		month_numeral = 3;
	}
	else if (date.month == 04 || date.month == 07) {
		month_numeral = 6;
	}
	else if (date.month == 9 || date.month == 12) {
		month_numeral = 5;
	}
	else if (date.month == 05) {
		month_numeral = 1;
	}
	else if (date.month == 06) {
		month_numeral = 4;
	}
	else if (date.month == 8) {
		month_numeral = 2;
	}


	sum = century_numeral + decade + year_numeral + month_numeral + date.day;
	day_numeral = sum % 7;

	day_of_week(day_numeral);

}

string day_of_week(int day_numeral)
{
	string dayString;

	switch (day_numeral)
	{
	case 0:
		return dayString = "Sunday";
		break;
	case 1:
		return dayString = "Monday";
		break;
	case 2:
		return dayString = "Tuesday";
		break;
	case 3:
		return dayString = "Wednesday";
		break;
	case 4:
		return dayString = "Thursday";
		break;
	case 5:
		return dayString = "Friday";
		break;
	case 6:
		return dayString = "Saturday";
		break;
	default:
		return dayString = "wrong n_sum input";
	}


}

Recommended Answers

All 2 Replies

The function maybe returning a string, but nothing is actually recieving the string to display it.

Corrected code with comments next to where I changed.

#include <iostream>
#include <string>

using namespace std;

struct dateType
{
	int day;
	int month;
	int year;
};

// declare some functions if necessary
// you can refer to some examples introduced in week 12

void display(dateType date);


bool is_leapyear(dateType date);

int day_calculator(dateType date, string &dayString); //Added string &dayString as parameter passed by reference

string day_of_week(int day_numeral);


int main()
{
	dateType date;
	string dayString;
	int day_numeral;

	// declare other variables if necessary

	cout << "Enter a date in the form of dd mm yyyy: " << flush;  // don't modify
	cin >> date.day >> date.month >> date.year;                   // don't modify

	// write your code here
	// find the corresponding day of week of date2
	// assign the corresponding string such as "Monday", "Tuesday" to dayString

	day_calculator(date, dayString); //send dayString to day_calculator


	cout << dayString << endl;                                    // don't modify

	return 0;                                                     // don't modify
}


bool is_leapyear(dateType date)
{
	bool leap_yr = false;

	if (((date.year % 4) == 0) && ((date.year % 100) != 0) || date.year % 16 == 0)
	{
		return leap_yr = true;
	}

	else
		return leap_yr = false;

}


int day_calculator(dateType date, string &dayString) //Again dayString is passed to here
{
	int century_numeral, year_numeral, month_numeral;
	int sum;
	int day_numeral;

	int century = date.year / 100;
	int decade = date.year % 100;

	century_numeral = 2*(3 - century % 4);
	year_numeral = (decade / 4);

	if (date.month ==01 && is_leapyear(date)) {
		month_numeral = 6;
	}
	else if (date.month == 02 && is_leapyear(date)) {
		month_numeral = 2;
	}
	else if (date.month == 01 && !is_leapyear(date) || date.month == 10) {
		month_numeral = 0;
	}
	else if (date.month == 02 && !is_leapyear(date)|| date.month == 03 || date.month == 11) {
		month_numeral = 3;
	}
	else if (date.month == 04 || date.month == 07) {
		month_numeral = 6;
	}
	else if (date.month == 9 || date.month == 12) {
		month_numeral = 5;
	}
	else if (date.month == 05) {
		month_numeral = 1;
	}
	else if (date.month == 06) {
		month_numeral = 4;
	}
	else if (date.month == 8) {
		month_numeral = 2;
	}


	sum = century_numeral + decade + year_numeral + month_numeral + date.day;
	day_numeral = sum % 7;

	dayString = day_of_week(day_numeral); //day_of_week is called,  return value is assigned to dayString, because passed by reference it modifies the value of dayString in main().

}

string day_of_week(int day_numeral)
{
	string dayString;

	switch (day_numeral)
	{
	case 0:
		return dayString = "Sunday";
		break;
	case 1:
		return dayString = "Monday";
		break;
	case 2:
		return dayString = "Tuesday";
		break;
	case 3:
		return dayString = "Wednesday";
		break;
	case 4:
		return dayString = "Thursday";
		break;
	case 5:
		return dayString = "Friday";
		break;
	case 6:
		return dayString = "Saturday";
		break;
	default:
		return dayString = "wrong n_sum input";
	}


}

Keep it simple and effective:

const std::string& DayOfWeek(int dow)
{
    static const std::string day[] = {
        "Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday", "???"
    };
    return day[dow < 0 || 6 < dow? 7: dow];  
}
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.