Problems with function prototype

This is a discussion on Problems with function prototype within the C Programming forums, part of the General Programming Boards category; Hi, This program is menu driven with the user choosing the function they would like to run. The function prototype ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Unhappy Problems with function prototype

    Hi,

    This program is menu driven with the user choosing the function they would like to run. The function prototype I am using is running but has a logical error I cannot solve. It is supposed to print a completion timetable: event #, max days for a task within the event and the total days for the project to complete. It is using a txt file to recieve the data.
    when you run the function it is supposed to look like this when it prints to the screen:

    event number max days

    1 6

    2 5

    3 4

    4 7

    5 4

    The maximum number of days to complete the project is 26 days.





    The data it is processing is ( the * is what it should print after processing as above)


    event task number of days
    1 15 3

    1* 27 6*

    1 36 4

    2* 15 5*

    3* 18* 4*

    3 26 1


    4 15 2

    4* 26 7*

    4 27 7

    5* 16 4*
    etc

    the tasks can happen at the same time therefore the program is supposed to recognize the largest amount of days per event and add all the largest ones together* for the maximum time for the project to finish.



    Code:
    void GetTotalDays(FILE *inFile)
    {
      
     int total = 0;
    
     int CEvent, NEvent, TaskNum, NumDays, MaxDays;                  
      {           
    
     fscanf(inFile, "%i %i %i", &CEvent, &TaskNum, &NumDays);
              
              MaxDays=NumDays;              
      }               
       printf("Event #\t Max. Days");                
     
     while(fscanf(inFile, "%i %i %i ", & CEvent,&TaskNum, &NumDays) == 3)
     {                
             if (CEvent == NEvent)
             {
              if (NumDays > MaxDays)
              {
                  MaxDays=NumDays;
              } 
             } 
             else
             {
                  printf("\n%i\t %i\n ",CEvent, MaxDays);
                  CEvent = NEvent;
                  total = total+MaxDays;
                  MaxDays=NumDays;
               }
        
     }
        printf("%i\t %i\n", CEvent,MaxDays);
        printf("The maximum number of days for the project to completed in is %i days.", total);
    }
    I know that the issue is that it is printing the largest value for maxdays right away instead of comparing it to the next line. How do I distinguish the next event from the current event as well as the number of days from one line from the next?
    [\tag]

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Inside your while loop, you need to compare the day you are reading now, with the day that you will read next, and add the larger of those two days, to the sum of days.

    The sum of days will be your answer, when all the input has been read and processed.

    If maxdays is your sum, then you can't be comparing it to numdays, that would make no sense.

    Decide what variable you want to have as your running sum or tally of days, and which two you want to have as your current days numbers to compare.

    Using one variable for both purposes, has your loop logic goofed.


    You want something like this:

    set your sumday variable to 0, before the loop starts

    start your loop:

    read days1, days2

    find which is larger than the other one

    add the larger of those, to the sumday variable

    loop back for another read of data

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    thanks for the input!

    I'm still fighting with this function. I'll figure it out though, stubborness usually pays off ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. call to function without prototype
    By lgbeeb in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 12:20 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21