2015-01-31 5 views
0
#include <iostream> 
#include <string> 



using namespace std; 

/* 
Function Name: weightConv 
Purpose: To take the weight and convert the following number to the coressponding weight unit 
Return : 0 
*/ 
    double weightConv(double w, string weightUnit) 
{ 

     if (weightUnit == "g" || weightUnit == "G") 
     cout << " Mass = " << w * 0.035274 << "oz"; 
    else if (weightUnit == "oz"||weightUnit == "OZ"||weightUnit == "oZ" ||weightUnit == "Oz") 
     cout << " Mass = " << w * 28.3495 << "g"; 
    else if (weightUnit == "kg"||weightUnit == "KG"||weightUnit == "Kg" ||weightUnit == "kG") 
     cout << " Mass = " << w * 2.20462 << "lb"; 
    else if (weightUnit == "lb" ||weightUnit == "LB" ||weightUnit== "Lb" ||weightUnit == "lB") 
     cout << " Mass = " << w * 0.453592 << "kg"; 
    else if (weightUnit == "Long-tn" ||weightUnit == "LONG-TN"|| weightUnit == "long-tn" || weightUnit == "long-ton") 
     cout << " Mass = " << w * 1.12 << "sh tn"; 
    else if (weightUnit == "sh-tn" || weightUnit == "SH-TN") 
     cout << " Mass = " << w/0.892857 << " Long tons"; 
    // one other converstion that was not listed in the project desription (stones<->tons) 
    else if (weightUnit == "s" || weightUnit == "S") 
     cout << " Mass = " << w * 0.007 << "tons"; 
    else if (weightUnit == "tons" || weightUnit == "Tons" || weightUnit == "TOns" || weightUnit == "TONs"|| weightUnit == "TONS") 
     cout << " Mass = " << w * 142.857 << "stones"; 
    else 
     cout << "Is an unknown unit and cannot be converted"; 

    return 0; 
}// end of weightCov function 


int main() 
{ 
    for (;;) 
    { 
     // variable declaration 
     string user; 
     double mass; 
     string unitType; 

     //Prompt user to enter values 
     cout << "Enter a mass and its unit type indicator(g,kg,lb,oz,long-tn,or sh-tn)" << endl; 
     cin >> mass >> unitType; 


     // Output Results 
     cout << weightConv(mass, unitType) << endl; 
     cout << "Would you like to do another calculation?(yes/no)"; 
     cin >> user; 

     //Loop asking user if they want to do another calculation 
     if (user == "yes") 
     { 
      cout << "Enter a mass and its unit type indicator(g,kg,lb,oz,long tn,or sh tn)" << endl; 
      cin >> mass >> unitType; 
      cout << weightConv(mass, unitType) << endl; 
     } 
     else if (user == "no") 
     { 
      return 0; 
     } 
    }// end of for loop 
}// end of main 

한 가지 오류를 제외하고는 프로그램이 제대로 작동합니다. 결과가 인쇄되면 문자 그대로 끝에 0이 추가됩니다. 함수의 끝에서 0을 반환하지만 return weightConv()가 무한 재귀임을 알았습니다. 아무도 나를 도울 수 없으므로 결국 답을 돌려 주며 끝은 0이 아닙니다. 미리 감사드립니다.출력 말미에 문자 그대로 0을 반환하는 이유

+1

'weightConv'를 출력으로 보내기 때문에. 그냥 void 함수로 호출하십시오. –

+0

고마워, 네가 방금 내가 무효화 할 수 있다는 것을 깨달았다. 나는 이것을 다음과 같이 부르려고했다. << "Mass ="<< weightConv (질량, 단위 유형) << endl; 하지만 void는 weightConv (mass, unitType)와 같은 funciton 호출을 사용합니다. 알리미 롤 주셔서 감사합니다. –

답변

1

변환 된 값을 인쇄하고 있지만 반환하지는 않았습니다. 각각의 if 블록은 계산 결과를 리턴하는 return 문을 포함해야합니다.

관련 문제