2014-09-12 5 views
-2

링깃 말레이시아의 금액을 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 

다음과 같이 표시됩니다? 출력 scanf 입력 버퍼 \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 
+0

왜 'main()'을 재귀 적으로 호출합니까? 그냥 재미 있니? – ikh

+1

convert 함수가'Main()'을 다시 호출하기 때문에 두 번 표시됩니다. – chiapa

+0

실제로 필요한 것은 do-while 루프입니다. – CinCout

답변

1

같아야 그런데

. 실제로는 두 문자 (y\n)를 입력합니다. scanf은 공백을 지우지 않습니다. (예 : Simple dump program)

공백을 없애려면 scanf을 사용하면됩니다. cppreference에서

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

, 형식 문자열에서

단일 공백 ​​문자가 입력에서 사용 가능한 모든 연속적인 공백 문자를 소비

그래서 추가 공백은 입력에서 '\n' 및 기타 공백을 먹는 완충기.

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

메신저 코드 메신저 없기 때문에 전문가를 공유하지 않을, 그러나 당신이 스위치가 일을 가지고있는 변환을하자 처리하는 IF ELSE 문을 사용하여 나에게 더 의미가 있습니다()의 유일한 일을 숫자 값을 변환하여 주 함수로 반환하고 주 함수가 결과를 인쇄 ("")하도록합니다. 구현 방법은 당신에게 달려 있습니다. 코드는 코드가 적고 체계적으로 구성되어 있습니다.

0

다음은 괜찮은 작업 버전입니다. 100 % 완벽하지는 않습니다. 다시 한번 전문가가 아니며 모범 사례 등에 대한 자신의 판단을 사용하십시오. 피드백을 통해 여전히 자신을 배우는 메신저, 학생이 아니라 자유 시간 자습생으로 크게 평가되었습니다. 제가이 일을 시작한 이유는 제가 학습 과정에서 문제를 푸는데 도움이되는지, 미래의 사용을 위해 무언가를 배울 수 있는지를 보는 것입니다.

/* 
* Name: tomyr 
* File: tomyr.c 
* Description: Converts $USD to MYR (RM). 
* Author: Steven Medley <[email protected]> 
* 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?. 
    } 
}