# Problem with code on strings

This is a discussion on Problem with code on strings within the C Programming forums, part of the General Programming Boards category; The code below asks for input of working hours and works out the weekly wage. The part about the longest ...

1. ## Problem with code on strings

The code below asks for input of working hours and works out the weekly wage. The part about the longest worked day doesnt work. It gives the correct longest worked hours, but instead of the day's name it gives strange symbols. Any ideas why?

Code:
```#include <stdio.h>
void main()
{
enum week{mon,tue,wed,thu,fri,sat,sun};
int hours [sun+1];	/*2D array*/
char day_name [sun+1][7] = {"mon","tue","wed","thu","fri","sat","sun"};
int rate1, rate2, wage, longest;
week day;

for(day=mon;day<=sun;day=(week)((int)(day)+1))
{
printf("\nInput the number of hours you work on mon, tue, wed, thu, fri, sat & sun\n");
for(day=mon;day<=sun;day=(week)((int)(day)+1))scanf("%d",&hours[day]);
}

printf("\nThe hours you input are:\n"
"mon = %d, tue = %d, wed = %d, thu = %d, fri = %d, sat = %d, sun = %d\n",hours[mon],
hours[tue],hours[wed],hours[thu],hours[fri],hours[sat],hours[sun]);

/*calculates the longest working day*/
longest=hours[mon];
for(day=mon;day<=sun;day=(week)((int)(day)+1))
if(longest<hours[day]) longest=hours[day];
printf("\nThe longest working day is %d hours on %s\n",longest, day_name[day]);

/*calculates money earned in a week*/
for(day=mon;day<=sun;day=(week)((int)(day)+1))
{
rate1=0;
for(day=mon;day<=fri;day=(week)((int)(day)+1))rate1=rate1+(4*hours[day]);

rate2=0;
for(day=sat;day<=sun;day=(week)((int)(day)+1))rate2=rate2+(5*hours[day]);

wage=rate1+rate2;
printf("\nThe total weekly wage is %d pounds\n\n",wage);
}
}```

2. You weren't saving the index of the day with the largest number of hours, because of that and the fact that you used (day+1) in your calculation, you had a classic off by one error reading one index too far in your array.
Code:
```  for(day=mon;day<=sun;day=((int)(day))+1)
if(longest<hours[day])
/* Fixed here by adding a variable
** to hold the index of the longest day
*/
longest=hours[day], lday = day;```
And don't use void main, the result is undefined.

-Prelude