Click here to Skip to main content
15,896,342 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Thank you for helping me out my friends
Happy new year

What I have tried:

I dont know
i havent tried anything
Posted
Updated 4-Jan-19 0:59am
v2

You should use an indexed loop to search the list for the day name that the user inputs. If the name is found then the current index will be its position in the list. If it is not found then you need to post an error message. Remember that the previous index value for Monday will not be -1. Similarly the next value after Sunday will not be 7.

C#
int index = -1;
for int i = 0; i < daysofweek.Length; ++i)
{
    if input.Equals(daysofweek[i])
        break;
}
// at this point index should have a value equal to the day's index (0 to 6)
// if the name was not found then it will be greater than 6.
 
Share this answer
 
v2
Comments
Member 14075859 3-Dec-18 11:02am    
How do i do that
Im a beginner actually so any help?
Richard MacCutchan 3-Dec-18 11:46am    
See my updated code.
Maciej Los 4-Jan-19 7:00am    
Good point, a 5!
See my answer, Richard ;)
The problem with that approach is what happens when the user enters Monday or Sunday?
You need to use the user input to search your array for the entered day name. (Try a for loop)
If it isn't found, tell him that isn't a day and get him to try again.
if it is, then the loop variable is the index to "today".
To calculate the previous days index, add 6 to the index, and then use the modulus operator to take it back to a "proper" value. To calculate the next days, add one, and then use modulus again.
C#
int prevIndex = (index + 6) % 7;
int nextIndex = (index + 1) % 7;
That works because the modulus gives you the remainder, and will always be between 0 and 6 inclusive. (So adding six and taking the modulus 7 effectively gives you a minus one that "wraps" round the end of the array!)

Hint: C# strings are case sensitive: so "Tuesday" will not match "tuesday": I'd compare them by converting them to lowercase before the actual comparison.
 
Share this answer
 
v2
Comments
Member 14075859 3-Dec-18 11:07am    
can you like send me the code please
im getting confused
im just a beginner
OriginalGriff 3-Dec-18 11:19am    
I could, but I'm not going to! This is your homework, not mine - and you will learn a lot more by doing it (and making the mistakes) than you would just looking at my work and handing it in.

Plus your teacher almost certainly knows this site exists and wouldn't be happy with plagiarism ...

Give it a try: do the first bit first. Read the input, and find the current day - it's only a couple of lines of code! Use the debugger to see exactly what is happening while your code runs.

When you have that working, add the next bit.
It's not complicated, if you look at it in little chunks.
Member 14075859 3-Dec-18 11:23am    
okay so i already created the array and its elements
i created a variable to store the input
i asked the user for the current day
then?
OriginalGriff 3-Dec-18 11:29am    
Then use a for loop to find the input in the array.
I'm sure you've done something like that in previous exercises.
Member 14075859 3-Dec-18 11:31am    
well its my second exercise
my first one i used a for loop to generate square numbers thats all
this one is much harder
my lecturer told us to try
Member 14075859 wrote:
i gave up lol


Hey, don't give up!
Please, see this MSDN article: Performing arithmetic operations with dates and times | Microsoft Docs[^]
There's the most information you need to handle your problem.

Note: you can write a programme which operates on dates:
C#
//needs reference to System.Globalization
CultureInfo ci = new CultureInfo("en-us");

DateTime givenDay = new DateTime(2019,01,01);
DateTime prevDay = givenDay.AddDays(-1);
DateTime nextDay = givenDay.AddDays(1);

Console.WriteLine("given day: {0} ({1})", givenDay.ToString("yyyy-MM-dd"), givenDay.ToString("dddd", ci));
Console.WriteLine("prev day: {0} ({1})", prevDay.ToString("yyyy-MM-dd"), prevDay.ToString("dddd", ci));
Console.WriteLine("next day: {0} ({1})", nextDay.ToString("yyyy-MM-dd"), nextDay.ToString("dddd", ci));

Result:
given day: 2019-01-01 (Tuesday)
prev day: 2018-12-31 (Monday)
next day: 2019-01-02 (Wednesday)


or on enums (Enumeration types - C# Programming Guide | Microsoft Docs[^]):
C#
public enum WeekDays { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int givenDay = (int)WeekDays.Sunday; //(int)WeekDays.Tuesday;
int prevDay = givenDay-1;
if(prevDay<0) prevDay = (int)WeekDays.Saturday;
int nextDay = givenDay+1;
if(nextDay>6) nextDay = (int)WeekDays.Sunday;

Console.WriteLine("given day is: {0} (value of: {1})", Enum.GetName(typeof(WeekDays), givenDay), givenDay);
Console.WriteLine("prev day is: {0} (value of: {1})", Enum.GetName(typeof(WeekDays), prevDay), prevDay);
Console.WriteLine("next day is: {0} (value of: {1})", Enum.GetName(typeof(WeekDays), nextDay), nextDay);

Result:
given day is: Sunday (value of: 0)
prev day is: Saturday (value of: 6)
next day is: Monday (value of: 1)


or even on array (as Richard MacCutchan point me out to the original question):

C#
string[] WeekDays = new string[]{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

Console.WriteLine("Days of week:");
for(int i=0; i<WeekDays.Length; i++)
    Console.WriteLine("{0} - {1}", i, WeekDays[i]);
Console.WriteLine("Choose the day (corresponding number):");

string choosenDay = Console.ReadLine();
int givenDay = Int32.Parse(choosenDay);
int prevDay = givenDay-1<0 ? 6 : givenDay-1;
int nextDay = givenDay+1>6 ? 0 : givenDay+1;

Console.WriteLine("given day is: {0} (value of: {1})", WeekDays[givenDay], givenDay);
Console.WriteLine("prev day is: {0} (value of: {1})", WeekDays[prevDay], prevDay);
Console.WriteLine("next day is: {0} (value of: {1})", WeekDays[nextDay], nextDay);

Result:
Days of week:
0 - Sunday
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday
Choose the day (corresponding number):
given day is: Tuesday (value of: 2)
prev day is: Monday (value of: 1)
next day is: Wednesday (value of: 3)
 
Share this answer
 
v2
Comments
Richard MacCutchan 4-Jan-19 7:18am    
+5, very well explained. But my answer referred to the original question (take a look at version 1) which OP has edited out.
Maciej Los 4-Jan-19 7:30am    
I saw it, Richard. I was wondering why you and OriginalGriff are recommending to use for loop. So, i had to see original question.
[EDIT]
I forgot to say: Thank you.

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