这里怎么了?单位转换器

what is wrong here? Unit converter

本文关键字:转换器 单位 怎么了 这里      更新时间:2023-10-16

它不断进入一个继续循环,不确定为什么!这不是一项任务,只是练习,我想学习,我想制作一个单元转换器,但我不确定这是野兽的方式,如果您有更好的主意,请自由。

/// 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;
}

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

cin>>units;  // here you read it
string units, N, kN, lb, kip;  // here redefinition

可能不需要第二个units

和内部循环,因为这就是设置条件的方式:while (units == "N" || units == "kN" || units == "lb" || units =="kip");如果unitsN,则它是循环的。

当然可以做其他方式,但是这是与您的一些解决方案,您可以看到如果分析更改

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

我们至少可以在这里看到

我们不需要两个嵌套环

只有一个单位变量

使用后重置单位和re_run

即使您的检查不抓住用例

具有块范围的变量需要初始化,如果默认初始化使其不可初始化

重复代码移动到单独的函数

我们不需要所有这些变量

etc

我认为您可以删除内部"做..."语句,然后保留其他检查。没有您的无限循环问题,该程序仍然相同。