2017-05-19 5 views
0

私はCプログラムを作っていました。 (私は初心者であり、プログラミングでは非常に新しいです)。私は最初の入力を求めてから、それを待たずに別の入力をタイプして、非常に速く終了します。私はeclipse-cdtとUbuntuを使用しています。cプログラムは入力を待っていません

コード

#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; 
} 

出力

[email protected]:/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 [email protected]:/media/jos/D/c progs/eclipse/linux/coliner-points/Debug$ 

私のコードで間違ったか、エラーが発生しています?

+3

は、カンマを入力しないでください。あなたのscanfは数字の間のスペースを探しています。 –

+0

私のために働く?それ以外の点は出力が間違っていることです(これは常にポイントが直線上にないことを示しています)。これはおそらく浮動小数点比較の結果になります。このリンクを使用すると改善できる可能性があります:http://faq.cprogramming。あなたの改行問題の修正として、com/cgi-bin/smartfaq.cgi?answer = 1352443831&id = 1043284392 –

+0

@Scott Hunterありがとうございました –

答えて

5

最初のscanfにカンマがないため、最初の%fにのみ一致します。残りの部分はすべて処理待ちの入力がありますが、コンマで始まるので、すべての入力に一致しません。

戻り値を確認して、期待していた値の数を確認する必要があります。

+0

すべての雹You.program今すぐ実行されます。私は入力値を区切るためにカンマ(、)を使用することはできません? –

+0

あなたはそれを探すことができます。入力はscanf形式の文字列と一致する必要があります。 –

+0

が得意です!ありがとう –

1

は常に、scanf()の戻り値をチェックし例えば

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