Thread: Problems with function prototype

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

    Unhappy Problems with function prototype


    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*

    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.

    void GetTotalDays(FILE *inFile)
     int total = 0;
     int CEvent, NEvent, TaskNum, NumDays, MaxDays;                  
     fscanf(inFile, "%i %i %i", &CEvent, &TaskNum, &NumDays);
       printf("Event #\t Max. Days");                
     while(fscanf(inFile, "%i %i %i ", & CEvent,&TaskNum, &NumDays) == 3)
             if (CEvent == NEvent)
              if (NumDays > MaxDays)
                  printf("\n%i\t %i\n ",CEvent, MaxDays);
                  CEvent = NEvent;
                  total = total+MaxDays;
        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?

  2. #2
    Registered User
    Join Date
    Sep 2006
    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
    British Columbia, B.C.
    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, 11:45 AM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06: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