这是怎么了单位转换器

时间:2019-07-05 10:09:07

标签: c++ unit-conversion

它继续进入一个继续循环,不确定为什么! 这不是一个作业,只是练习,我想学习,我想做一个单位转换器,但是我不确定这是否是野兽的方法,如果您有更好的主意,请随时。

/// Write a program to promt for units
//calcultes and converts units

#include <iostream>
#include <string>
#include <cmath>

using namespace std;int main () {
    string re_Run, units;
    do{
    int f1;


    cout<<"enter force: >";
    cin>>f1;

    cout<<"enter units: >";
    cin>>units;

    string units, N, kN, lb, kip;
    double conv_lb, conv_N, conv_kN;

    do{
        if (f1<1000 && units == "N"){
            cout<<f1<<" N";
        }
        else if (f1>1000 && units == "kN"){
            cout<<f1<<" kN";
        }
        else if (f1>=1000 && units == "N") {//|| x== kN)
            conv_N=f1/1000;
            cout<<conv_N<<" kN"; //convert from N to kN
        }
        else if (f1<1000 && units== "lb" ){
            cout<<f1<<" lb";
        }
        else if (f1>1000 && units== "lb" ){//|| x==kip
            conv_lb=f1/1000;
            cout<<conv_lb<<" kip";
        }
        else if (f1>1000 && units== "kip" ){
            cout<<f1<<" kip";
        }
        else {
            cout<< "please enter (lb/kip/N/kN)\n    >";
            cin>>units;
        }
    }while (units == "N" || units == "kN" || units == "lb" || units =="kip");//(units != "N" && units != "kN" && units != "lb" && units !="kip");

    cout<<"re-run?";
    cin>> re_Run;
    }
    while (re_Run == "yes");
return 0;
}

3 个答案:

答案 0 :(得分:2)

您重新定义units变量,使其为空:

cin>>units;  // here you read it

string units, N, kN, lb, kip;  // here redefinition

可能不需要第二个units

内部的while循环是因为那是如何设置条件的:while (units == "N" || units == "kN" || units == "lb" || units =="kip");如果unitsN则循环。

答案 1 :(得分:0)

当然可以采用其他方法,但这是与您相关的一些解决方案,您可以看到分析更改后出了什么问题

#include <iostream>
#include <string>
//#include <cmath>

using namespace std;
void printResult(const double f1, std::string& units, bool& legal) {
    cout<<f1<<units<<"\n";
    units = "";
    legal = false;
}

bool check(const std::string& units) {
    return (units == "N")||(units == "kN")||(units == "lb")||(units == "kip");
}

int main () {

string re_Run, units;
bool legal(false);
double conv(0.);
double f1(0.);

do{
    re_Run = "";
    if (f1<1000 && units == "N") {

        printResult(f1,units,legal);
    }
    else if (f1>1000 && units == "kN") {

        printResult(f1,units,legal);
    }
    else if (f1>=1000 && units == "N") {//|| x== kN)
        conv=f1/1000;
        //cout<<conv_N<<; //convert from N to kN
        units = " kN";
        printResult(conv,units,legal);
    }
    else if (f1<1000 && units== "lb" ) {
        //cout<<f1<<" lb";
        printResult(f1,units,legal);
    }
    else if (f1>1000 && units== "lb" ) {//|| x==kip
        conv=f1/1000;
        //cout<<conv_lb<<" kip";
        units = " kip";
        printResult(conv, units,legal);
    }
    else if (f1>1000 && units== "kip" ) {
        //cout<<f1<<" kip";
        printResult(f1,units,legal);
    }
    else if (legal) {
        printResult(f1,units,legal);
    }
    else {
        cout<<"enter force: >";
        cin>>f1;
        cout<< "please enter (lb/kip/N/kN)\n    >";
        cin>>units;
        legal = check(units);
        re_Run = "yes";
    }
    if (false == legal) {
        cout<<"re-run?";
        cin>> re_Run;
    }
}while (re_Run == "yes");

return 0;
}

我们至少可以在这里看到

我们不需要两个嵌套循环

仅一个单位变量

重置单位并在使用后重新运行

即使您没有检查用例,也可以打印合法输入的值

如果默认初始化未初始化,则必须初始化具有块作用域的变量

重复代码已移至单独的功能

我们不需要所有这些变量

答案 2 :(得分:-2)

我认为您可以删除内部的“ do ... while”语句,并保留if else检查。没有无限循环问题,程序仍然是相同的。