0

this window jumps when it gets to receiving a code

hey, i started learing C today and made a program, i got this message on my program so i tested the exercise answer and it was getting the same error, getting the message on the picture whenever it gets to the line : this code is the answer ..

scanf_s("%s", &id);
**thats the full code:**

int main() {
    char id[10];
    int hour;
    double value, salary;
    printf("Input the Employees ID(Max. 10 chars): ");
    scanf_s("%s", &id);
    printf("\nInput the working hrs: ");
    scanf_s("%d", &hour);
    printf("\nSalary amount/hr: ");
    scanf_s("%lf", &value);
    salary = value * hour;
    printf("\nEmployees ID = %s\nSalary = U$ %.2lf\n", id, salary);
    return 0;
}

didnt find any answer to it online , help please.

  • 1
    I would suggest reading the documentation of [`scanf_s`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=vs-2017) before using it. You're missing a parameter, and need to specify the expected buffer count in the format string. – Mgetz Mar 07 '19 at 13:55
  • 2
    `char id[10];` can't hold 10 chars – Support Ukraine Mar 07 '19 at 13:55
  • 1
    Also relevant are the [Uninitialized memory patterns](https://stackoverflow.com/a/370362/332733). – Mgetz Mar 07 '19 at 13:56
  • the code wont compile when using "scanf" , VS17 wont let me because its "not secured" but youre saying that the problem is in the scanf_s ? – omer peretz Mar 07 '19 at 14:02
  • @omerperetz I'm saying that the documentation to `scanf_s` indicates you're not using it correctly. I'd highly suggest looking at the documentation if you intend to use a method. – Mgetz Mar 07 '19 at 14:46

1 Answers1

0

First of all: you using windows library, your program should be portable so change scanf_s -> scanf and make sure you include stdio.h.

Second error is here:

scanf_s("%s", &id);

Should be

scanf_s("%s", id);

Because function want argument char*, in first example you pass &(char[]) => char**.

Igor Galczak
  • 142
  • 6