0

I am a complete beginner at c so please give me advice that is as simple as possible. I have two questions. How do you use scanf in a function with pointers because my program is not working at all. Also, how do you write functions that uses values from another function. For example I have to write a function that asks for employee name, hours worked, and hourly rate. And then I have to write another function that uses that information to calculate gross pay and overtime pay. This is the scanf code I wrote so far:

#include <stdio.h>
     #include <stdlib.h>
     int employeedata(char *ch, float *x, float *y)
      {
      printf("Enter your name:\n");
       scanf_s("%s", &ch);
      printf("Enter your hourly rate:\n");
      scanf_s("%f", &x);
       printf("Enter number of hours worked:\n");
       //variables
           scanf_s("%f", &y);
      }
  int main() {

           float rate[5], hours[5];
       float overtimerate = 1.5;
       char name[5][20];

           int loop;

           //loop 5 times
           for (loop = 0; loop < 5; loop++)
           {
            //call function
                employeedata(name[loop], &rate[loop], &hours[loop]);
           //use if to break out of loop after input if -1 is entered
                if (strcmp(name, "-1") == 0)
                 break;

                if (rate[loop] == -1)
                 break;

               if (hours[loop] == -1)
                 break;
            }
        system("pause");
        return 0;

}
1
  • Turn up your compiler warnings to pedantic levels. The warnings you get should be very telling. Commented Jul 23, 2016 at 8:51

2 Answers 2

1

You are passing pointer to a pointer in scanf function in line

scanf_s("%s", &ch);

But scanf requires pointers to buckets where you want the read values to be put. Hence the corrected code should be

scanf_s("%s", ch);

Same error in lines

 scanf_s("%f", &x);
 scanf_s("%f", &y);

which must be

 scanf_s("%f", x);
 scanf_s("%f", y);

Because x, y & ch themselves are pointers

More: Here is the prototype for the scanf()

int scanf(const char *format_string, ...);

where "..." (ellipsis) refer to pointers to the buckets.

Sign up to request clarification or add additional context in comments.

Comments

0

edit: here is an example for everything you wanted

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

char *inputString(FILE* fp, size_t size) {
    //The size is extended by the input with the value of the provisional
    char *str;
    int ch;
    size_t len = 0;
    str = realloc(NULL, sizeof(char)*size);//size is start size
    if (!str)return str;
    while (EOF != (ch = fgetc(fp)) && ch != '\n') {
        str[len++] = ch;
        if (len == size) {
            str = realloc(str, sizeof(char)*(size += 16));
            if (!str)return str;
        }
    }
    str[len++] = '\0';
    return realloc(str, sizeof(char)*len);
}


void CalcFunc(double hours, double rate, double* gross, double* otime);





int main(void) {
    char *name;
    double HoursWorked = 0, RatePerHour = 0, GroosPay =0, OverTime=0;
    printf("input name: ");
    name = inputString(stdin, 10);


    printf("input hours worked: ");
    scanf("%lf", &HoursWorked);

    printf("input rate per hour: ");
    scanf("%lf", &RatePerHour);

    CalcFunc(HoursWorked, RatePerHour, &GroosPay, &OverTime);
    printf("Name:%s\n", name);
    printf("hours worked:%.2f\n", HoursWorked);
    printf("rate per hour:%.2f\n", RatePerHour);
    printf("over time:%.2f\n", OverTime);
    printf("gross pay:%.2f\n", GroosPay);

    free(name);
    return 0;
}



void CalcFunc(double hours, double rate, double* gross, double* otime) {
//just an example for function
    *otime = hours*2;
    *gross = hours*rate*0.3;
    return;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.