这个C程序有什么问题?(使用scanf从终端进行扫描)
程序只是构建,但提示符就像卡在无限循环中一样挂起 第一个printf语句甚至没有运行 该程序的思想是获取MMSI、名称、位置、航向和速度,并将它们放入结构中以写入文件这个C程序有什么问题?(使用scanf从终端进行扫描),c,scanf,C,Scanf,程序只是构建,但提示符就像卡在无限循环中一样挂起 第一个printf语句甚至没有运行 该程序的思想是获取MMSI、名称、位置、航向和速度,并将它们放入结构中以写入文件 int main(int argc, char** argv) { ship *current_ship; current_ship = getShipInfo(); //writeShip(current_ship); return (EXIT_SUCCESS); } ship * getS
int main(int argc, char** argv) {
ship *current_ship;
current_ship = getShipInfo();
//writeShip(current_ship);
return (EXIT_SUCCESS);
}
ship * getShipInfo() {
ship *current_ship;
current_ship = malloc(sizeof(ship));
int MMSI, course;
char name[51];
float lat, lon, speed;
printf("Enter MMSI (9 digits):\n");
scanf(" %9d", &MMSI);
printf("Enter ship name (upto 50 characters):\n");
scanf(" %51s", name);
printf("Enter ship latitude (real number with upto 3 decimal places):\n");
scanf(" %f", &lat);
printf("Enter ship longitude (real number with upto 3 decimal places):\n");
scanf(" %f", &lon);
printf("Enter course made good (degrees from true north):\n");
scanf(" %3d", &course);
printf("Enter speed over the ground (in knots with exactly one decimal place):\n");
scanf(" %f", &speed);
current_ship->MMSI = MMSI;
strcpy(current_ship->name, name);
current_ship->lat = lat;
current_ship->lon = lon;
current_ship->course = course;
current_ship->speed = speed;
return current_ship;
}
ship*getShipInfo()
是否在main()函数之前声明?
这可能是个问题。您是否分配了ship->name
current\u ship->name=malloc(51)代码>您的问题是scanf格式字符串开头的空格。更大的问题是使用scanf。只需使用fgets即可。它在头文件中声明,这不是问题所在,因此这不是一个完整的程序?终端是否处于防止刷新标准输出的任何特殊模式?尝试在printf()
调用之后插入fflush(stdout)
。您尝试了哪些调试方法?例如,如果返回NULL
作为getShipInfo()
中的第一条语句,程序是否运行其过程?类似的事情。最好发布完整的代码,包括船舶结构声明和功能声明等