我做过C程序。 (请,我是初学者和非常新的编程)。它问我第一次输入然后它没有等待或要求我输入另一个输入并且非常快速地退出。我正在使用eclipse-cdt和Ubuntu。
码
#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;
}
输出
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$
我的代码中有错误或错误吗?
答案 0 :(得分:4)
您的第一个scanf缺少逗号,因此它只匹配第一个%f。因此,其余的都有待处理的待处理输入,但它以逗号开头,因此它们都无法匹配任何输入。
您需要检查返回值,以确保获得了您期望的值数。
答案 1 :(得分:1)
始终检查scanf()
的返回值,例如
if (scanf("%f,%f", &x2, &y2) != 2) {
fprintf(stderr, "scanf failed at line %d.\n", __LINE__ - 1);
exit(EXIT_FAILURE);
}