-1

I am creating a simple application to keep track how long I am working. A user enters the amount of minutes of work they are doing and the program will keep track of the total until termination. The issue is after the first time the program is run it to prints out its first statement twice. I have it inside an infinite loop and I don't have a great grasp on scanf and printf so I think that is where my failure lies. Any help would be appreciated.

This is what the program looks like:

#include <stdlib.h>
#include <stdio.h>

void logic(int *minutesWorked, int *hoursWorked, int *totalMinutesWorked);

int main(void)
{
    int     minutesWorked = 0;
    int     hoursWorked = 0;
    char    letter = 0;
    int     totalMinutesWorked = 0;

    while (1) {
        printf("Enter I for input or T for total\n");
        scanf("%c", &letter);
        if (letter == 'i' || letter == 'I') {
            printf("Please enter minutes worked\n");
            scanf("%d", &minutesWorked);
            logic(&minutesWorked, &hoursWorked, &totalMinutesWorked);
        }
        else if (letter == 't' || letter == 'T') {
            printf("Hours worked is %d and minutes worked is %d\n", hoursWorked,totalMinutesWorked);
        }
    }
    return EXIT_SUCCESS;
}       /* ----------  end of function main  ---------- */


void    logic(int *minutesWorked, int *hoursWorked, int *totalMinutesWorked)
{
    *totalMinutesWorked += *minutesWorked;
    if (*totalMinutesWorked >= 60) {
        *hoursWorked += *totalMinutesWorked / 60;
        *totalMinutesWorked = *totalMinutesWorked % 60;
    }
}       /* -----  end of function logic  ----- */

And this is an example of what it displays:

Enter I for input or T for total
I
Please enter minutes worked
100
Enter I for input or T for total
Enter I for input or T for total
I
Please enter minutes worked
33
Enter I for input or T for total
Enter I for input or T for total
T
Hours worked is 2 and minutes worked is 13
Enter I for input or T for total
Enter I for input or T for total
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I would guess—though by no means am I certain—that you’re reading the newline character after the number, finding it is neither I nor T, and going to the top of the loop. – Daniel H Jul 31 '17 at 22:45
  • treat the input as a string, with a somewhat regex expression bound to the `scanf` function, i.e. `[^\n]` then parse the string to an integer. – t0mm13b Jul 31 '17 at 22:54
  • Note that the variable `minutesWorked` is separate from the pair `hoursWorked` and `totalMinutesWorked`. You would make life easier all around if you used more systematic names. Probably, use `totalHoursWorked` to match `totalMinutesWorked`. Names do matter to humans; the compiler isn't as worried. – Jonathan Leffler Jul 31 '17 at 23:08

1 Answers1

1

The best solution would be to use fgets() instead of scanf(), but if that is not an option, you can add this line to clear input buffer after each scanf call:

fseek(stdin, 0, SEEK_END);
Devin L.
  • 437
  • 2
  • 12
  • 3
    As I understand it, it is not guaranteed in C that `stdin` should support positioning requests, meaning that the call to `fseek()` here could fail; this is not portable. There are better ways to portably clear the input stream, such as the idiomatic `int c; while ((c = getchar()) != '\n' && c != EOF);`. – ad absurdum Jul 31 '17 at 23:18
  • @DavidBowling agreed – Devin L. Jul 31 '17 at 23:19