【问题标题】:call function using yes or no使用是或否调用函数
【发布时间】:2014-09-12 10:21:07
【问题描述】:

我想将 马来西亚林吉特 的金额转换为以 美元 表示的等值。我想编写一个 C 程序,可以要求用户输入 yes 或 no 以继续后续功能。

我已经做了一些编码

#include <stdio.h>
#include <stdlib.h>

int main()
{

    char y, n, Y, N, ans;
    printf("\nWould you like to continue ?:\t");
    scanf("%c",&ans);

    switch (ans)
    {
        case 'y':
        case 'Y':
           convert();
           break;
        case 'N':
        case 'n':
           return(0);
           break;
        default:
            {
                return (0);
            }
            break;
            return 0;
    }

    while (ans==y || ans==Y);
        return main();

}

int convert () {
    float ia,ca;

    printf("\nEnter amount in Ringgit Malaysia : RM ");
    scanf("%f",&ia);

    ca=ia/3.30;

    printf("\nRM %.2f is equals to USD %.2f\n\n\n",ia,ca);

    return main();
}

但我的输出如下所示

Would you like to continue ? : y
Enter amount in Ringgit Malaysia : RM 100
RM 100.00 is equals to USD 30.30


Would you like to continue ? : 
Would you like to continue ? : y
Enter amount in Ringgit Malaysia : RM 100
RM 100.00 is equals to USD 30.30

Would you like to continue ? : 
Would you like to continue ? : n

我的问题是为什么这个问题重复两次?

顺便说一下输出应该是这样的

Would you like to continue ? : y
Enter amount in Ringgit Malaysia : RM 100
RM 100.00 is equals to USD 30.30

Would you like to continue ? : y
Enter amount in Ringgit Malaysia : RM 100
RM 100.00 is equals to USD 30.30

Would you like to continue ? : n

【问题讨论】:

  • 为什么递归调用main()?好玩吗?
  • 您的转换函数再次调用Main(),这就是它显示两次的原因
  • 你真正需要的是do-while loop
  • @chiapa 没有。即使递归看起来很奇怪,在这种情况下也不会产生错误。 (如果我们大量使用这个程序,stackoverflow除外>o
  • 哇,一个短程序中有这么多错误...摆脱那些return main() 声明开始。如果你想在用户输入 Y 后重复这个过程,那么只需使用一个简单的循环。

标签: c loops return-value function-calls


【解决方案1】:

这是因为scanf 将\n 留在输入缓冲区中。实际上,您输入了两个字符:y 和 \n。 scanf 不会删除空格。 (例如:Simple dump program)

要清除空白,您可以让scanf 这样做。

scanf(" %c",&ia);
/*     ^                 */
/* additional whitespace */

来自cppreference,

格式字符串中的任何单个空白字符都会消耗输入中所有可用的连续空白字符

所以额外的空格会吃掉'\n' 和输入缓冲区中的其他空格。

【讨论】:

    【解决方案2】:

    我已经设法让它工作,我已经在下面发布了我的答案。

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    char y, n, Y, N, ans;
    printf("\nWould you like to continue ?:\t");
    scanf(" %c",&ans);
    
    
    switch (ans)
    {
    case 'y':
    case 'Y':
           convert();
    break;
    case 'N':
    case 'n':
           return(0);
    break;
    default:
        {
            return (0);
        }
    break;
    return 0;
    }
    
    while (ans==y || ans==Y);
    return main();
    }
    
    int convert (){
        float ia,ca;
    
        printf("\nEnter amount in Ringgit Malaysia : RM ");
        scanf("%f",&ia);
    
        ca=ia/3.30;
    
        printf("\nRM %.2f is equals to USD %.2f\n\n\n",ia,ca);
    
     return main();
    }
    

    【讨论】:

      【解决方案3】:

      这是我想出的一个不错的工作版本。它不是 100% 完美的。 再说一次,我不是专家,并且对最佳实践等使用您自己的判断。非常感谢反馈,因为我仍在学习自己,我不是学生,只是一个空闲时间的自学者。我开始这样做的原因是想看看我是否可以帮助解决学习过程中的问题,也许可以学到一些东西以备将来使用。

      /*
      * Name: tomyr
      * File: tomyr.c
      * Description: Converts $USD to MYR (RM). 
      * Author: Steven Medley <smedley76@gmail.com>
      * Copyright (c) 2016 Steven Medley.
      * This program is free software: you can redistribute it and/or modify
      * it under the terms of the GNU General Public License as published by
      * the Free Software Foundation, either version 3 of the License, or
      * (at your option) any later version.
      * This program is distributed in the hope that it will be useful,
      * but WITHOUT ANY WARRANTY; without even the implied warranty of
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      * GNU General Public License for more details.
      * You should have received a copy of the GNU General Public License
      * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      */
      #include <stdio.h>
      // stdlib.h is NOT needed.
      int main()
        {
          float usdol; // used to store input as $USD.CC eg: 1.50 
          float myr = 3.30; // this rate (usd/myr) may not be up to date
          char answer; // stores the answer to continue
          do //Loops until you QUIT saying y or Y to continue
            {
              printf("Enter in USD to convert to MYR (Malasia Ringgit) $");
              scanf("%f", &usdol); //possible FIXME: fgets()?? but for this program as is, scanf works
              printf("$%.2f is equal to RM %.2f\n", usdol, usdol/myr);
      
      
              /*
              FIXME: i dont check length of answer and enter key acts as newline
                     still expecting an answer to the question.
                     can be a "2 birds, 1 stone" type of fix.
              */
              LABEL: printf("would you like to continue? y/Y/n/N: ");
              scanf(" %c", &answer);;
      
           }  
         while (answer == 'y' || answer == 'Y'); // see above comment next to "do".
         if (answer == 'n' || answer == 'N')
           {
             return 0;
           }
         else
           { // oops answer wasnt a Yy/Nn, attempt at error handling.
             printf ("That was an invalid answer.\n");
             goto LABEL; //possible FIXME: this works, but there could be a better way?.
           }
       }
      

      【讨论】:

        【解决方案4】:

        我不打算分享任何代码,因为我不是专家,但对我来说,使用 IF ELSE 语句来处理你的开关所做的事情更有意义,让 convert() 完成它唯一的转换工作并返回一个将数值添加到您的 main 函数中,并让 main 函数成为 printf("") 结果的函数。你如何实现它取决于你,我发现它的代码更少,更有条理。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-01
          • 2012-08-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多