Thread: Problem with code on strings

  1. #1
    Unregistered
    Guest

    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;
    
     /*reads in student's hours*/
     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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my morse code program
    By justin87 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:23 PM
  2. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM