功能在开关盒环路中

-7 c loops function case switch-statement

是否可以在开关盒环路中放置一个功能?因为我试过这个只是为了探索更多这个循环.虽然我尝试了其他方法,但我仍然存在问题.谁能帮我?

#include <stdio.h>
int main(void)

{

    int choice;

    switch(choice);
    {
    case 1:
    {
           int GetData()
           {
           int num;
           printf("Enter the amount of change: ");
           scanf("%d%*c", &num);

           return (num);

           }
           int getChange (int change,int fifty,int twenty,int ten,int five)
           {
                 int num = change;

                 fifty = num/50;
                 num %= 50;
                 twenty = num/20;
                 num %= 20;
                 ten = num/10;
                 num %= 10;
                 five = num/5;
                 num %= 5;

                 return (fifty, twenty, ten, five);
           }
           int main()
           {
                 int change, fifty, twenty, ten, five;

                 change = GetData();

                 if ((change < 5) || (change > 95) || (change%5 > 0))
           {
                 printf("Amount must be between 5 and 95 and be a multiple
                 of 5.");
           }
           else
           {
                 getChange(change, fifty, twenty, ten, five);

                 printf("Change for %d cents: \n", change);

                 if (fifty > 1)
                     printf("%d Fifty cent piece's.\n", fifty);
                 if (fifty == 1)
                     printf("%d Fifty cent piece.\n", fifty);
                 if (twenty > 1)
                     printf("%d Twenty cent piece's.\n", twenty);
                 if (twenty == 1)
                     printf("%d Twenty cent piece.\n", twenty);
                 if (ten > 1)
                     printf("%d Ten cent piece's\n", ten);
                 if (ten == 1)
                     printf("%d Ten cent piece.\n", ten);
                 if (five > 1)
                     printf("%d Five cent piece's\n", five);
                 if (five == 1)
                     printf("%d Five cent piece.\n", five);
           }

          return(0);

         }  
Run Code Online (Sandbox Code Playgroud)

Cro*_*man 7

不可以.不仅无法在switch语句中定义函数,也无法在C中嵌套函数定义.您必须在文件范围内定义它们.

在您提供的代码中,没有理由为什么您需要或需要定义嵌套函数,因此您的底层问题可能实际上是不同的.

此外:

  • 你不能从这样的函数返回多个值:

    return (fifty, twenty, ten, five);
    
    Run Code Online (Sandbox Code Playgroud)
  • 你试图定义main()两次.

  • switchchoice初始化之前就开始了choice,这显然不是你想要做的.

  • caseswitch陈述中只有一个,这意味着你根本不需要switch陈述.

  • 这一切都只是一团糟:( (5认同)