代码 P 的问题:PP 演练 4

Issue with code P: PP Drill 4

本文关键字:PP 演练 问题 代码      更新时间:2023-10-16

我一直在做一本名为《编程:使用C++的原则和实践》一书中的练习,作者是Bjarne Stroustrup,这个练习(第4章(主要涉及向量。当我运行该程序时,它根本没有给我任何输出。不过没有编译错误。代码如下:

#include "std_lib_facilities.h"
int main()
{
    vector<double> value;
    vector<string> units;
    double max_val=-100000, min_val=100000;
    double temp, sum=0;
    int no_of_inputs=0;
    string unit;
    int i=0;
    cout << "nEnter the first value: " << endl;
    // inputs values and units and assign them in vector value and vector unit respectively.
    while (cin >> temp >> unit){
        ++no_of_inputs;
        cout << "Enter the next value: " << endl;
        value.push_back(temp);
        units.push_back(unit);
    }
    // converts cm, in and ft to m; 
    for (i==0; i<units.size(); ++i){        
        if (units[i]== "cm" || units[i]== " cm"){
                value[i] = value[i]/100.0;
        }else if (units[i]== "in" || units[i]== " in"){
                value[i] = value[i]/2.5/100.0;
        }else if (units[i]== "ft" || units[i]== " ft"){
                value[i] = value[i]*12/2.5/100.0;
        }else if (units[i]=="m" || units[i]== " m"){
            }else cout << "nn" << value[i] << " " << units[i] << " is an incorrect input value."; 
        return 0;
    }
    // Identifies the max_value and the min_value. Also adds all values.
    for (i==0; i<value.size(); ++i){
        if (value[i]>max_val){
            max_val=value[i];
        }
        if (value[i]<min_val){
        min_val=value[i];
        }           
        sum += value[i];
    }
    // outputs all values entered (converted to meters)
    cout << "nValues Entered:"<< endl;
    sort(value);
    for (i==0; i<value.size(); ++i){
        cout << value[i] << " meters" << endl;
    }
    cout << "Total: " << sum << " meters" << endl;
    cout << "Smallest value: " << min_val << endl;
    cout << "Largest value: " << max_val << endl;
    cout << "Total values entered: " << no_of_inputs << endl;
    return 0;
}

谁能告诉我为什么它不起作用?

你有 2 个错误。终止程序的主要原因在这里

else cout << "nn" << value[i] << " " << units[i] << " is an incorrect input value."; return 0;

你把返回放在第一个 for 循环中。我想您可能认为返回属于 else,但由于您不使用范围确定器 {},只有 1 条指令 (cout( 属于 else,并且每次都执行返回。

你的第二个错误是在 for 循环中使用 i==0 而不是 i=0。

您的正确代码是这样的:

vector<double> value;
vector<string> units;
double max_val = -100000, min_val = 100000;
double temp, sum = 0;
int no_of_inputs = 0;
string unit;
int i = 0;
cout << "nEnter the first value: " << endl;
//inputs values and units and assign them in vector value and vector unit respectively.
while (cin >> temp >> unit){
    ++no_of_inputs;
    cout << "Enter the next value: " << endl;
    value.push_back(temp);
    units.push_back(unit);
}
//converts cm, in and ft to m;
for (i = 0; i<units.size(); ++i){
    if (units[i] == "cm" || units[i] == " cm"){
        value[i] = value[i] / 100.0;
    }
    else if (units[i] == "in" || units[i] == " in"){
        value[i] = value[i] / 2.5 / 100.0;
    }
    else if (units[i] == "ft" || units[i] == " ft"){
        value[i] = value[i] * 12 / 2.5 / 100.0;
    }
    else if (units[i] == "m" || units[i] == " m")
    {
    }
    else
    {
        cout << "nn" << value[i] << " " << units[i] << " is an incorrect input value."; 
    return 0;
    }
}
//Identifies the max_value and the min_value. Also adds all values.
for (i = 0; i<value.size(); ++i){
    if (value[i]>max_val){
        max_val = value[i];
    }
    if (value[i]<min_val){
        min_val = value[i];
    }
    sum += value[i];
}
//outputs all values entered (converted to meters)
cout << "nValues Entered:" << endl;
//sort(value);
for (i = 0; i<value.size(); ++i){
    cout << value[i] << " meters" << endl;
}
cout << "Total: " << sum << " meters" << endl;
cout << "Smallest value: " << min_val << endl;
cout << "Largest value: " << max_val << endl;
cout << "Total values entered: " << no_of_inputs << endl;
return 0;