NONAME
NONAME

Reputation: 23

Why is my scanf function getting skipped?

The second scanf function in my code is getting skipped, I don't know how to fix it.

void main(void)
{
    
    int x1, y1;
    int x2, y2;
    int dist_x, dist_y;
    
    float angle, dist;
    float sintheta, costheta; 

    printf("Enter a starting x and y position with a comma separating each number (i.e. 0,0)\n");
    scanf("%d %d", &x1, &y1);

    printf("Enter an ending x and y position with a comma separating each number (i.e. 0,0)\n");
    scanf("%d %d", &x2, &y2);

    dist_x = x2 - x1;
    dist_y = y2 - y1;
    
    dist = sqrt(pow(dist_x, 2) + pow(dist_y, 2));
    sintheta = acos(dist_x / dist);
    costheta = asin(dist_y / dist); 

    
} //end of main

I've already tried putting a space in-between the double quote and the %d identifier. Changing scanf("%d %d", &x2, &y2); into scanf(" %d %d", &x2, &y2); but it seemed to not have an effect. Any and all help would be greatly appreciated, Thank you for your time.

Upvotes: 0

Views: 81

Answers (1)

Steven Liang
Steven Liang

Reputation: 375

User input has to get matched with the format string.

Your scanf has "%d %d" while you are inputting something like 0,0.

Either change "%d %d" into "%d,%d" or remove the , in the input, then the program will run without problem.

Tested on Visual Studio 2019.

BTW, if scanf is used in MSVC, then /D_CRT_SECURE_NO_WARNINGS has to been added in the compiler options in order not to get a compilation error. scanf_s is recommended in the later days after learning and practising.

Upvotes: 1

Related Questions