我已经被分配了这个问题,但我还没有走得很远,我不知道如何正确地使用switch函数来解决这个问题,我也不确定如何完成它。有人能帮上忙吗?

struct car
{
char model[50];
int manufacture_year;
float price;
};
int main()
{
int i;
int function;
struct car array[2];
for(i=0; i<2; i++) {
printf("what is the cars model? ");
scanf(" %s", &array[i].model);
printf("What year was the car manufactured? ");
scanf(" %d", &array[i].manufacture_year);
printf("How much does it cost? ");
scanf(" %f", &array[i].price);
printf("\n");
}
printf("press 1 to show model, 2 to show price and 3 to terminate");
scanf("%d", &function);
}这就是我目前所拥有的..。我想,转换应该在之后发生。
发布于 2018-02-04 21:30:02
将switch放在输入开关变量的值(在本例中为scanf)的函数后面,如下所示:
/* preceding code */
printf("press 1 to show model, 2 to show price and 3 to terminate");
scanf("%d", &function);
switch (function) {
case 1:
show_model(array, 2); /* placeholder */
break;
case 2:
show_price(array, 2); /* placeholder */
break;
case 3:
break;
}开关的语法如上所示。要测试的值位于case关键字之后。而且,在每种情况的语句之后,通常都有一条break语句来退出开关。例如,如果在第一个printf下没有break语句,执行将继续到下一条语句,这可能是也可能不是所希望的。
https://stackoverflow.com/questions/48608347
复制相似问题