-3

I'm doing this homework where I need to read a line from a file for each iteration.

I got this inputs:

200 38
220 48
230 68
240 48
260 68
280 68
300 48
0 0

What I need is to read the first 2 ints using fscanf.
Then on the next loop I'll read the next 2 ints and so on. Ex. i'll read 200 38
then i'll read 220 48 on the next loop.
Can somebody help me with this?

#include <stdio.h>

int main() {

    int rate, hours;
    float pay;


    // This program will compute the pay rate of a person based on the working hours.

    FILE *inputFileptr;
    FILE *outputFileptr;

    inputFileptr = fopen("input.txt","rt");
    outputFileptr = fopen("pays.txt","at");

    do {
        fscanf(inputFileptr,"%d %d", &rate, &hours);
        if  ( hours <= 40 ) {
            pay = (hours * rate) / 100.00;
            fprintf(outputFileptr,"Pay at %d centavos/hr for %d hours is %.2f pesos \n", rate, hours, pay);
        }
        else if ( hours <= 60 && hours > 40) {
            pay = ((((hours - 40)* rate) * 1.5) + ( rate * 40)) / 100.00;
            fprintf(outputFileptr,"Pay at %d centavos/hr for %d hours is %.2f pesos \n", rate, hours, pay);
        }
        else if ( hours < 60 ) {
            pay = ((((hours - 60) * rate) * 2) + (((hours - 40)* rate) * 1.5) + (rate * 40));
            fprintf(outputFileptr,"Pay at %d centavos/hr for %d hours is %.2f pesos \n", rate, hours, pay);
        }
    } while ( rate == 0 && hours == 0);

    fclose(inputFileptr);
    fclose(outputFileptr);

    return 0;
}
Caroliner
  • 1
  • 6
  • 1
    show your code so far and explain where you are stuck – stark Sep 14 '18 at 01:47
  • 1
    It's best to make an attempt at the problem and state specifically what you're having trouble with. Including the code you use and the errors you are getting. – jdavison Sep 14 '18 at 01:48
  • 2
    the while condition should be "while (rate !=0 && hours !=0)". Your current while condition iterates only if the rate is 0 and the hours is 0. – Tejus Sep 14 '18 at 02:00
  • If you have 0 0 at the end of the record just for stopping the loop. You can as well check for end of file. For more info go to https://stackoverflow.com/questions/12048576/how-to-find-eof-through-fscanf – Tejus Sep 14 '18 at 02:03
  • 1
    What you want to do is read each line into a buffer, then parse the buffer for the values you need. This allows (1) a validation of the read (separate and apart from) (2) a validation of the parse/conversion to `int`. The approach is simple, **don't skimp on buffer size**, for your case, e.g. `#define MAXC 256` then `char buf[MAXC] = ""; int a = 0, b = 0;` followed by `do { if (fgets (buf, MAXC, inputFileptr)) { if (sscanf (buf, "%d %d", &a, &b)) printf ("%4d %d\n", a, b); } else break; } while (1);` (which explains why it should be a `while` instead of a `do while`, but your homework...) – David C. Rankin Sep 14 '18 at 02:19

1 Answers1

0

The expression here in your while loop, means the while loop will continue when rate == 0 && hours == 0 is true, clearly you should change it to rate != 0 && hours != 0, otherwise it will immediately stop soon as it reads the first line '200 38'.

Meanwhile, your if expression else if ( hours < 60 ), it should be else if ( hours > 60 ) .

zangfong
  • 1
  • 2