需要一些帮助来查找我的程序中的逻辑错误

Need some help finding a logic error in my program

本文关键字:程序 错误 我的 查找 帮助      更新时间:2023-10-16

我正在通过Bjarne Stroustrup的"使用C++编程原理和实践"第二版学习C++。我致力于做所有的练习和练习,因为我一个月后要在学校上游戏人工智能课,我需要知道C++,我想尽可能多地了解。

无论如何,如果有人曾经做过书中的演习,我正在参加第 4 章的演习。我走到了最后。最终产品应该是一个程序,该程序在一段时间循环中获取一个数字,后跟一个测量单位(厘米、英寸、英尺或米(,同时跟踪最大值以及最小值和以米为单位输入的单位总和。我完全以米为单位工作,除了最大和最小值被转换为米以检查哪个更大,但存储为用户最初输入的值。数字也入到一个向量中,直到最后才用于对值进行排序并按顺序打印它们。

无论如何,我似乎没有问题跟踪总米数,因为它在最后正确打印出来,但是当涉及到最小值时,似乎会出现问题,偶尔会出现最大值。另外,某些值在值向量中完全关闭。我会但代码在下面。我认为错误在我的toMeter((中的某个地方,但我已经盯着它看了很长时间,我觉得一双新鲜的眼睛可能会有所帮助。谢谢!

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
constexpr double mToCm = 100.00; //1m == 100cm
constexpr double inToCm = 2.54; //1in == 2.54cm
constexpr double ftToIn = 12.00; //1ft == 12in
//this function just increases total ammount of units in meters
double adder(string unit, double total, double num)
{
if(unit == "cm")
{
total += (num / mToCm);
}
else if(unit == "in")
{
double temp;
temp = num * inToCm;
total += (temp / mToCm);
}
else if(unit == "ft")
{
double temp;
temp = num * ftToIn;
temp *= inToCm;
total += (temp / mToCm);
}
else
{
total += num;
}
return total;
}
double toMeters(string unit, double num)
{
double value = num;
if(unit == "cm")
{
value /= mToCm;
}
else if(unit == "in")
{
value *= inToCm;
value /= mToCm;
}
else if(unit == "ft")
{
value *= ftToIn;
value *= inToCm;
value /= mToCm;
}
return value;
}
int main()
{
double num, biggest, smallest; //current number, biggest and smallest value
double total = 0; //total number in meters
string unit, biggestUnit, smallestUnit; //current unit, unit for biggest and smallest value
int count = 0; //count for the loop it's only real purpose is on the first and second loop runs
vector<double>meters; //a vector of doubles called meters

cout << "Total ammount of units will be converted to meters, largest and biggest values will be kept in original unitsn";
cout << "Start of by entering a number followed by cm, m, in, ft and continue until you want to stop; to stop, press |n";
while(cin >> num >> unit)
{
//check if user want to stop
if(num == '|')
{
break;
}
//check for correct units, if not then break out
if(unit != "cm" && unit != "m" && unit != "in" && unit != "ft")
{
cout << "Unit not recognized; only cm, m, in, ft are validn";
return 1;
}
//this checks if there is no second unit but since cin reads no 
//whitespace it doesn't work. I just haven't gotten around to removing it
else if(unit == " ")
{
cout << "Please input a unitn";
return 1;
}
//all values in vectors are supposed to be in meters. If the unit is not 
//in meters, we will call a push back on the value returned by converting 
//the orignal number to meters
if(unit != "m")
{
meters.push_back(toMeters(unit, num));
}
//else, just call a push_back
else
{
meters.push_back(num);
}
//if the count is 0, i.e. very first run of the program, biggest and 
//smallest is equal to original and call adder() to increment the total
if(count == 0)
{
biggest = smallest = num;
biggestUnit = smallestUnit = unit;
total = adder(unit, total, num);
}
//else, if count is > 0, i.e. this is not the first run, do this part
else
{   
//if the value returned after running toMeters on the current value
//is greater than the biggest, biggest now equals current num and 
//biggest unit is equal to current unit. Then call adder to inceare total
if(toMeters(unit, num) > toMeters(biggestUnit, biggest))
{
biggest = num;
biggestUnit = unit;
total = adder(unit, total, num);
}
//same as top function but for smallers value
else if(toMeters(unit, num) < toMeters(smallestUnit, smallest))
{
smallest = num;
smallestUnit = unit;
total = adder(unit, total, num);
}
//else both numbers are equal so just make a call to adder()
else
{
total = adder(unit, total, num);
}
}
//increase count just because. It was really only needed to be incremented once 
++count;
}
//call sort on the vector then print out the total units followed by bigges the smallest values
//then the values in meters, is ascending sorted order.
sort(meters.begin(), meters.end());
cout << "Total units in meters is " << total << "n";
cout << "Largest unit is " << biggest << biggestUnit << "n";
cout << "Smallest unit is " << smallest << smallestUnit << "n";
cout<< "Here are all values you entered, in meters, in ascending order: n";
for(int i : meters)
{
cout << meters[i] << "m" << " ";
}
cout << "n";
}

我最新的输入是 1 cm 2 in 3 m 4 ft,输出是 以米为单位的总单位为 4.28 最大单位是3m(正确( 最小单位为 2 英寸(应为 1 厘米( 以下是您输入的所有值(以米为单位(,按升序排列: 0.01米 0.01米 0.0508米 3米

在第一个输入上设置biggestsmallest时,不会设置biggestUnitsmallestUnit。因此,比较无法正常工作。

尝试:

//if the count is 0, i.e. very first run of the program, biggest and 
//smallest is equal to original and call adder() to increment the total
if(count == 0)
{
biggest = smallest = num;
biggestUnit = smallestUnit = unit;
total = adder(unit, total, num);
}

此外,将total = adder(unit, total, num);放在每个代码路径中也不是一个好主意。只需将其放在if外一次即可。否则,很难看到它总是为每个输入精确执行一次。