换行问题 "C"
Line breaking issue "C"
我注意到我的短代码中有一个奇怪的错误。这个脚本的目的是创建一个结构数组,其中包含一些关于汽车的信息
("Struct Car Car1: char brand[50], char model[50], int ccm, int hp, int kw, char[50]")
在 运行 脚本之后一切正常,直到第一个实例被填充,但是当变量 i
增加 1 时,换行符从我要求的行之间消失用户提供“品牌”和“型号”。很抱歉我是菜鸟,这是我第一次使用结构。感谢您提供任何帮助。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS
#define MAX 10
int main() {
struct Car {
char brand[50];
char model[50];
int ccm;
int hp;
int kw;
char wheel[50];
};
struct Car Car1;
strcpy(Car1.brand, "Skoda");
strcpy(Car1.model, "Octavia");
Car1.ccm = 1960;
Car1.hp = 170;
Car1.kw = 114;
strcpy(Car1.wheel, "215/75R18");
printf("The car's brand is: %s\n", Car1.brand);
printf("The model is: %s\n", Car1.model);
printf("The volume of the engine is %d ccm\n", Car1.ccm);
printf("It has %d horsepower\n", Car1.hp);
printf("The car has %d kws\n", Car1.kw);
printf("You can put: %s wheels on it\n", Car1.wheel);
struct Car car_arr[MAX];
int i;
for (i = 0; i < MAX; i++) {
printf("Enter details of a car: %d\n", i+1);
printf("\n");
printf("Enter the brand: ");
fgets(car_arr[i].brand, sizeof car_arr[i].brand, stdin); //Brand
car_arr[i].brand[strcspn(car_arr[i].brand, "\n")] = '[=12=]';
printf("Enter the model: ");
fgets(car_arr[i].model, sizeof car_arr[i].model, stdin); //Model
car_arr[i].model[strcspn(car_arr[i].model, "\n")] = '[=12=]';
printf("Enter the parameters of the wheel: ");
fgets(car_arr[i].wheel, sizeof car_arr[i].wheel, stdin); //Wheel
car_arr[i].wheel[strcspn(car_arr[i].wheel, "\n")] = '[=12=]';
printf("Enter the volume of the engine in ccms: ");
scanf_s("%d", &car_arr[i].ccm);
printf("Enter the horsepower: ");
scanf_s("%d", &car_arr[i].hp);
printf("Enter the amount of kilowatts: ");
scanf_s("%d", &car_arr[i].kw);
}
return 0;
}
以上评论回答了您的问题,但作为旁注:
您可以通过将“typedef”和“struct”一起使用来避免重复。
typedef 结构 { int x1;长x2;双等; } my_type;
我注意到我的短代码中有一个奇怪的错误。这个脚本的目的是创建一个结构数组,其中包含一些关于汽车的信息
("Struct Car Car1: char brand[50], char model[50], int ccm, int hp, int kw, char[50]")
在 运行 脚本之后一切正常,直到第一个实例被填充,但是当变量 i
增加 1 时,换行符从我要求的行之间消失用户提供“品牌”和“型号”。很抱歉我是菜鸟,这是我第一次使用结构。感谢您提供任何帮助。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS
#define MAX 10
int main() {
struct Car {
char brand[50];
char model[50];
int ccm;
int hp;
int kw;
char wheel[50];
};
struct Car Car1;
strcpy(Car1.brand, "Skoda");
strcpy(Car1.model, "Octavia");
Car1.ccm = 1960;
Car1.hp = 170;
Car1.kw = 114;
strcpy(Car1.wheel, "215/75R18");
printf("The car's brand is: %s\n", Car1.brand);
printf("The model is: %s\n", Car1.model);
printf("The volume of the engine is %d ccm\n", Car1.ccm);
printf("It has %d horsepower\n", Car1.hp);
printf("The car has %d kws\n", Car1.kw);
printf("You can put: %s wheels on it\n", Car1.wheel);
struct Car car_arr[MAX];
int i;
for (i = 0; i < MAX; i++) {
printf("Enter details of a car: %d\n", i+1);
printf("\n");
printf("Enter the brand: ");
fgets(car_arr[i].brand, sizeof car_arr[i].brand, stdin); //Brand
car_arr[i].brand[strcspn(car_arr[i].brand, "\n")] = '[=12=]';
printf("Enter the model: ");
fgets(car_arr[i].model, sizeof car_arr[i].model, stdin); //Model
car_arr[i].model[strcspn(car_arr[i].model, "\n")] = '[=12=]';
printf("Enter the parameters of the wheel: ");
fgets(car_arr[i].wheel, sizeof car_arr[i].wheel, stdin); //Wheel
car_arr[i].wheel[strcspn(car_arr[i].wheel, "\n")] = '[=12=]';
printf("Enter the volume of the engine in ccms: ");
scanf_s("%d", &car_arr[i].ccm);
printf("Enter the horsepower: ");
scanf_s("%d", &car_arr[i].hp);
printf("Enter the amount of kilowatts: ");
scanf_s("%d", &car_arr[i].kw);
}
return 0;
}
以上评论回答了您的问题,但作为旁注: 您可以通过将“typedef”和“struct”一起使用来避免重复。
typedef 结构 { int x1;长x2;双等; } my_type;