这里怎么了?单位转换器
what is wrong here? Unit converter
它不断进入一个继续循环,不确定为什么!这不是一项任务,只是练习,我想学习,我想制作一个单元转换器,但我不确定这是野兽的方式,如果您有更好的主意,请自由。
/// 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");
如果units
是N
,则它是循环的。
当然可以做其他方式,但是这是与您的一些解决方案,您可以看到如果分析更改
#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
我认为您可以删除内部"做..."语句,然后保留其他检查。没有您的无限循环问题,该程序仍然相同。
相关文章:
- 转换器练习:跳过 if 语句和 if 语句
- 使用转换器提升 Python 问题 - 静态链接
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 用于 boost::p roperty_tree 的通用枚举转换器
- 为什么我的C++基转换器中出现分段错误?
- 将字节数组转换为对象C++ |C++中的 C# 位转换器
- 英语到摩尔斯电码转换器
- 我用c++做了一个转换器,把英里转换成公里,把公里转换成英里,把赛尔修斯转换成法赫伦海特,把法赫伦海特转换成赛尔修斯
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 十进制,包括负到 32 位二进制转换器
- 标准::映射转换器模板
- 英里到千克转换器练习的小语法错误
- 句子转换器在存在新行的情况下不起作用
- 在C Windows程序中获取A/D转换器示例程序
- ROS中未定义的参考误差到OpenCV图像转换器
- 表达式到C++类类型转换器/类型转换
- 这里怎么了?单位转换器
- 华氏度到摄氏转换器C
- 如何理解 c++ 中的 MNIST 二进制转换器?