Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
    #include<cmath>
    #include<ctime>
    #include<string>
    #include <iomanip>
    #include <fstream>
  
    using namespace std;
    
    string today(int day, string todayName);
    int hourLeft(int hour);
    
    
    int main()
    {
    	int day,hour;
    	cin >> day>>hour;
    
    	string dayName;
    	cout << today(day, dayName) << endl;
    	cout << hourLeft(hour) << endl;
    }
    string today(int day, string todayName)
    {
    	switch (day)
    	{
    	case 1: return todayName = "sunday";
    		break;
    	case 2: return todayName = "monday";
    		break;
    	case 3: return todayName = "tuesday";
    		break;
    	case 4: return todayName = "wednesday";
    		break;
    	case 5:return  todayName = "thursday";
    		break;
    	case 6: return todayName = "friday";
    		break;
    	case 7:return  todayName = "saturday";
    		break;
    	}
    }
    int hourLeft(int hour)
    {
    	int theHourLeft = 24 - hour;
    	return theHourLeft;
    }


What I have tried:

Edit (CHill60): Blank line removed. See above for code
Posted
Updated 11-Nov-20 13:37pm
v4
Comments
Richard MacCutchan 10-Nov-20 15:05pm    
What exactly is the problem?
KarstenK 11-Nov-20 12:50pm    
The question is unclear.
jeron1 11-Nov-20 17:20pm    
In addition to what Greg Utas posted, you should qualify your inputs. If the user types in '10' for the day variable or '19722' for your hour variable your code should handle it somewhere, otherwise you'll get operation you didn't expect or want. In many instances it is also good practice to have a default: statement in your switch statement to deal with data not handled by the case: statements.

1 solution

I can guess what's wrong. I'm not sure what
return todayName = "dayname";
is going to return, but my guess is that it won't be what you want. todayName doesn't need to be a parameter to this function. It already returns a string, so just write
return "dayname";
in all those places. You don't even need the break statements that follow, and your main function will just do
cout << today(day) << endl;
If you actually wanted to update the dayName local variable in main, you would have to declare your today function as
void today(int day, string& todayName)
Note the &, which tells the code to update the argument that is passed into the function instead of working on a copy which is lost when the function returns. With this approach, main would need to do
today(day, dayName);
cout << dayName << endl;
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900