cautomationswitch-statementscanfansi-c

UVa - Longest Nap


well it's been pretty hard find an answer since I don't know how to express this in english to find out! So,

I'm trying to do the Longest Nap problem: https://uva.onlinejudge.org/external/101/10191.pdf

My code is working but I keep receiving wrong answer from judge and I think the problem is when I type two test cases in a row.

I type:

1 
12:00 13:00 schedule A

so:

Day #1: the longest nap starts at 13:00 and will last for 5 hours and 0 minutes.

but if I type:

1
2

the number 2 is ignored as a new test case and I assume that's why I'm getting wrong answer from judge

So I want the second number I type, that is caught by the second scanf in my code be the new testcase. I tried to add in my switch case a case 1: where I force testcase be the initH since the new test case I typed is caught by this variable but with no success

    while(scanf("%d", &testcase) == 1) {

    int result = 0, start;

    if(testcase > MAXVALUE)  continue;
     //here I ignore testcase > 100
    if(testcase == 0) {ret = SCANF; start = STARTIME; result = WORK;}
     //if there's no testcase my longest nap will be the 8 hours! SCANF = 5

    for(i = 0; i < testcase; i++) {
        ret = scanf("%d:%d %d:%d %255[a-zA-Z ]", &initH, &initM, &fintH, &fintM, appoint);
         //variables: H(hour), M(minute), appointment

        switch (ret){
        case 5:
            schedule[i].start = initH*HOUR + initM; //struct here
            schedule[i].endin = fintH*HOUR + fintM; //to keep this data
            break;
        default:
            i = testcase;
            break;
        }

        if((initH < INIT) || (fintM + fintH*HOUR) > ENDTIME) {error++; break;}
         //10:00 < time < 18:00 
        if((initH*HOUR + initM) > (fintH*HOUR + fintM)) {error++; break;}
         //initial hour in a schedule < end time in a schedule

        while(getchar() != '\n');
    }

    if(error != 0) {error = 0; continue;} //if error then ignore everything!

Solution

  • Well, I've been stuck on this all day long yesterday and when I decide to ask for help I figure out a solution! Pretty easy actually!

    if(ret == 5){ //only if I have 5 arguments in my scanf!
        if((initH < INIT) || (fintM + fintH*HOUR) > ENDTIME) {error++; break;}
        //10:00 < time < 18:00 
        if((initH*HOUR + initM) > (fintH*HOUR + fintM)) {error++; break;}
        //initial hour in a schedule < end time in a schedule
    }