-1

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

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

3 回答 3

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那么它循环。

于 2019-07-05T10:18:31.297 回答
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;
}

我们至少可以在这里看到,

我们不需要两个嵌套循环

只有一个单位变量

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

打印合法输入的值,即使您的 if 检查未捕获用例

如果默认初始化未初始化,则需要初始化具有块范围的变量

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

我们不需要所有这些变量

ETC

于 2019-07-05T11:19:45.163 回答
-2

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

于 2019-07-05T10:57:44.983 回答