user8038216
user8038216

Reputation:

c program is not waiting for input

I had made C program. (please, I am beginner and very new in programing).It ask me for first input and then it doesn't wait or ask me to type another input and quits very fastly. I am using eclipse-cdt and Ubuntu.

code

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

int main(){
    float x1,x2,x3,y3,y2,y1,slope12,slope23;
    int exi;

    printf("this program finds that three point are co-linear or not. you just have to enter the value of the x and y's\n");
    printf("now enter the the value of x1 and y1 of point (x1,y2) here  here resplectevely :- ");
    scanf("%f%f",&x1,&y1);
    printf("now enter the value of the x2 and y2 of point (x2,y2) here respectively here :- ");
    scanf("%f%f",&x2,&y2);
    printf("now enter the value of x3 and y3 of point (x3,y3) here respectively :- ");
    scanf("%f%f",&x3,&y3);
    slope12=(y1-y2)/(x1-x2);
    slope23=(y2-y3)/(x2-x3);
    if(slope12==slope23)
        printf("\nthe points are co-liner and are in strait line");
    else
        printf("\nthe points are not in strait line and are not co-linear");
    ///delaying the program ending
    printf("\nenter any digit to exit");
    scanf("%d",&exi);
    printf("you enterd %d good bye",exi);

    return 0;
}

output

 jos@jos-Aspire-5742:/media/jos/D/c progs/eclipse/linux/coliner-points/Debug$ ./coliner-points 
 this program finds that three point are co-linear or not. you just have to enter the value of the x and y's
 now enter the the value of x1 and y1 of point (x1,y2) here  here resplectevely :- 7,8
 now enter the value of the x2 and y2 of point (x2,y2) here respectively here :- now enter the value of x3 and y3 of point (x3,y3) here respectively :- 
 the points are not in strait line and are not co-linear
 enter any digit to exityou enterd 0 good byejos@jos-Aspire-5742:/media/jos/D/c progs/eclipse/linux/coliner-points/Debug$ 

is there any wrong or error in my codes??

Upvotes: 0

Views: 449

Answers (2)

pmg
pmg

Reputation: 108986

Always check the return value of scanf(), eg

if (scanf("%f,%f", &x2, &y2) != 2) {
    fprintf(stderr, "scanf failed at line %d.\n", __LINE__ - 1);
    exit(EXIT_FAILURE);
}

Upvotes: 1

L. Scott Johnson
L. Scott Johnson

Reputation: 4412

Your first scanf is missing the comma, so it matches only the first %f. The rest all therefore have pending input to process, but it begins with a comma, so they all fail to match any input.

You need to check return values to make sure you got the number of values you were expecting.

Upvotes: 4

Related Questions