为什么它在字面上在输出结尾返回0

编程入门 行业动态 更新时间:2024-10-28 09:23:40
本文介绍了为什么它在字面上在输出结尾返回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,但如果返回weightConv()它是无限递归。任何人可以帮助我,使它只是返回答案,而不是零在结束。提前感谢。

Program works well except for one error. When the results print out it literally adds a zero at the end. I know I am returning 0 at the end of the function but if return weightConv() it's infinite recursion. Can anyone help me so that it just returns the answer and not zero at the end. Thanks in advance.

推荐答案

您只是打印转换后的值,但不会返回。每个if块应该包含返回计算结果的返回语句。

You're just printing the converted value but you're not returning it. Each of those if blocks should contain a return statement returning the result of the computation.

更多推荐

为什么它在字面上在输出结尾返回0

本文发布于:2023-11-23 11:04:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字面   它在   结尾

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!