首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >交换机功能和交换机外部的功能

交换机功能和交换机外部的功能
EN

Stack Overflow用户
提问于 2018-02-04 21:04:31
回答 1查看 49关注 0票数 0

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

代码语言:javascript
复制
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);


}

这就是我目前所拥有的..。我想,转换应该在之后发生。

EN

回答 1

Stack Overflow用户

发布于 2018-02-04 21:30:02

switch放在输入开关变量的值(在本例中为scanf)的函数后面,如下所示:

代码语言:javascript
复制
/* 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语句,执行将继续到下一条语句,这可能是也可能不是所希望的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48608347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档