使用是或否调用函数

时间:2014-09-12 10:21:07

标签: c loops return-value function-calls

我想将 Ringgit Malaysia 中的金额转换为 USD 中的等值。我想编写一个C程序,可以要求用户输入来继续后续功能。

我做了一些编码

#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

4 个答案:

答案 0 :(得分:1)

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

要清除空白,可以scanf执行此操作。

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

来自cppreference

  

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

因此额外的空格会在输入缓冲区中占用'\n'和其他空格。

答案 1 :(得分:0)

我设法让它运转起来,我在下面发布了我的答案。

#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();
}

答案 2 :(得分:0)

这是一个体面的工作版本我想出来了。它不是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?.
     }
 }

答案 3 :(得分:-1)

我不会分享任何代码,因为我不是专家,但是对我来说使用IF ELSE语句来处理你的交换机所做的事情更有意义,让convert()做它唯一的转换工作并返回一个主函数的数值,让main函数成为printf(“”)结果的函数。你如何实现它取决于你,我发现它的代码更少,组织更有条理。