代码跳过驱动程序 2 和 3 的名称输入,但为驱动程序 1 打印并为所有驱动程序打印 km 的输出为 0.00
Code skips the input for the name of driver 2 & 3 but prints for driver 1 as well prints the output of km as 0.00 for all drivers
我正在编写代码来打印三个驱动程序的详细信息,但问题是程序正确地接受了驱动程序 1 的输入,但跳过了驱动程序 2 和 3 的名称输入,而且尽管提供了输入,它还是打印了 distance travelled
作为 0.0
.
#include<stdio.h>
struct data
{
char name;
int dlo;
char route;
float km;
}s1,s2,s3;
void details(struct data *d)
{
printf("Enter your name: ");
scanf(" %s",&d->name);
printf("Enter your driving license no: ");
scanf("%d",&d->dlo);
printf("Enter route taken: ");
scanf("%s",&d->route);
printf("Enter distance travelled: \n");
scanf("%.2f",&d->km);
}
void output(struct data *d)
{
printf("Driver name: %s\n", &d->name);
printf("Driving license no: %d \n", d->dlo);
printf("Route taken: %s\n", &d->route);
printf("Distance travelled: %.2f \n", d->km);
}
int main()
{
printf("\n--------------driver 1---------------\n");
details(&s1);
printf("\n");
output(&s1);
printf("\n--------------driver 2---------------\n");
details(&s2);
printf("\n");
output(&s2);
printf("\n--------------driver 3---------------\n");
details(&s3);
printf("\n");
output(&s3);
return 0;
}
输出是这样的:
--------------driver 1---------------
Enter your name: abhishek
Enter your driving licence no: 123
Enter route taken: abcd
Enter distance travelled:
123
Driver name: abhishek
Driving licence no: 123
Route taken: abcd
Distance travelled: 0.00
--------------driver 2---------------
Enter your name: Enter your driving licence no: 321
Enter route taken: efgh
Enter distance travelled:
45
Driver name: 123
Driving licence no: 321
Route taken: efgh
Distance travelled: 0.00
问题是我在 scanf
中使用了 %.2f
,这导致 运行-时间错误,因此将 scanf("%.2f",&d->km);
更改为 scanf("%f",&d->km);
错误已解决我还必须为 name
和 route
使用数组以避免进一步的 运行-时间错误。
非常感谢大家的建议。
我正在编写代码来打印三个驱动程序的详细信息,但问题是程序正确地接受了驱动程序 1 的输入,但跳过了驱动程序 2 和 3 的名称输入,而且尽管提供了输入,它还是打印了 distance travelled
作为 0.0
.
#include<stdio.h>
struct data
{
char name;
int dlo;
char route;
float km;
}s1,s2,s3;
void details(struct data *d)
{
printf("Enter your name: ");
scanf(" %s",&d->name);
printf("Enter your driving license no: ");
scanf("%d",&d->dlo);
printf("Enter route taken: ");
scanf("%s",&d->route);
printf("Enter distance travelled: \n");
scanf("%.2f",&d->km);
}
void output(struct data *d)
{
printf("Driver name: %s\n", &d->name);
printf("Driving license no: %d \n", d->dlo);
printf("Route taken: %s\n", &d->route);
printf("Distance travelled: %.2f \n", d->km);
}
int main()
{
printf("\n--------------driver 1---------------\n");
details(&s1);
printf("\n");
output(&s1);
printf("\n--------------driver 2---------------\n");
details(&s2);
printf("\n");
output(&s2);
printf("\n--------------driver 3---------------\n");
details(&s3);
printf("\n");
output(&s3);
return 0;
}
输出是这样的:
--------------driver 1---------------
Enter your name: abhishek
Enter your driving licence no: 123
Enter route taken: abcd
Enter distance travelled:
123
Driver name: abhishek
Driving licence no: 123
Route taken: abcd
Distance travelled: 0.00
--------------driver 2---------------
Enter your name: Enter your driving licence no: 321
Enter route taken: efgh
Enter distance travelled:
45
Driver name: 123
Driving licence no: 321
Route taken: efgh
Distance travelled: 0.00
问题是我在 scanf
中使用了 %.2f
,这导致 运行-时间错误,因此将 scanf("%.2f",&d->km);
更改为 scanf("%f",&d->km);
错误已解决我还必须为 name
和 route
使用数组以避免进一步的 运行-时间错误。
非常感谢大家的建议。