开关功能和功能,以便在开关外部

时间:2018-02-04 13:04:31

标签: c

我已经把这个问题作为一项任务,但我还没有走得太远,我也不知道如何正确使用switch功能,我就是这样。不知道如何完成它。有人可以帮忙吗?

enter image description here

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);


}
到目前为止,我所拥有的......在我想到之后,意味着转变。

1 个答案:

答案 0 :(得分:0)

您将switch放在输入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语句,则执行将继续执行下一个语句,这可能是也可能不是。