【问题标题】:Bjarne Stroustrup's P:PP Chapter 4 DrillBjarne Stroustrup 的 P:PP Chapter 4 Drill
【发布时间】:2015-09-17 17:39:00
【问题描述】:

我刚刚开始学习 C++,我正在通过 Bjarne Stroustup 的 P:P&P 独自工作。

我正在第 4 章练习

我遇到的问题似乎在于程序的顺序。如果我在向量之前添加另一个右花括号来关闭 while 语句,我会得到max_valmin_val 的正确输出。但是,通过添加该大括号,名为 sum 的双精度保持为零,即使我希望 sum 增加双命名数字。

如果我按照现在编写的方式编译程序(不添加额外的花括号),我会得到 min_valmax_val 的错误输出,但 sum 的正确输出。

此外,正如您在程序底部看到的那样,该行: cout << " values were entered." << '\n';不完整。我想打印输入的总值,但由于沮丧和需要帮助而使其不完整。我对编程非常陌生,任何建设性的批评,无论多么严厉,都将不胜感激。

#include "std_lib_facilities.h"

int main()
{
    double value = 0;
    double max_val = 0;
    double min_val = 0;
    string unit =" ";
    double sum = 0;

    cout << "Enter some numbers, followed by a unit of distance (m, cm, ft, inch):" << '\n';

    while (cin >> value >> unit){

        //determines entered value as max/min/both/neither
        if (min_val == 0 && value > max_val){                       // first number entered is both largest and smallest
            max_val = value;
            min_val = value;
            cout << value << " metres is both the smallest and largest so far" << '\n';
        }
        else if (value < min_val){// smallest number min_val = value;
            cout << min_val << " metres is the smallest so far." << '\n';
        }
        else if (value > max_val){// largest number max_val = value;
            cout << max_val << " metres is the largest so far." << '\n';
        }
        else { // number between smallest and largest
            cout << value << " metres is neither the smallest or largest." << '\n';    }

        //convert entered unit to metres
        if (unit == "m"){
           value = value;
        }
        else if (unit == "cm"){//converts cm to metres
           value = value/100;
        }
        else if (unit == "ft"){//converts ft to metres
           value = value/3.28084;
        }
        else if (unit == "inch"){//converts inch to metres
            value = value/39.3701;
        }
        else{
            cout << "I dont know the unit " << unit << ".";
    }
    vector <double> numbers; //reads input into vector
    double number = 0;
    while (cin >> number) numbers.push_back(number);

    for (double number: numbers) sum += number;

    cout << "The largest value entered was: " << max_val << "." << '\n';
    cout << "The smallest value entered was: " << min_val << "." << '\n';
    cout << "The sum of the numbers entered is: " << sum << "metres" <<'\n';
    cout << " values were entered." << '\n';

    keep_window_open("~");

return 0;
}

【问题讨论】:

  • 看起来您已经将两个不同的任务混为一谈(距离转换 + 最大/最小/总和)。您遇到问题的实际作业的文本是什么?
  • 欢迎来到 Stack Overflow!请edit您的问题与minimal reproducible exampleSSCCE (Short, Self Contained, Correct Example)
  • 使用合理的缩进将是一个好的开始。无论如何,你不能随意添加和删除花括号:要么你当前有正确的数字(不管它们的位置),要么你没有,所以当你说你的程序编译但给出不同的输出取决于您是否随机注入“额外”右括号。
  • 注意缩进。无论添加额外的花括号,我如何“组织这个程序”以及我可以添加什么以便它完成我想要它做的事情?例如,Chad 说我将两个不同的任务混为一谈。我怎么能把它分开?真的很迷茫。。
  • 这如何编译?您使用分号而不是花括号关闭了 main。

标签: c++ vector curly-braces


【解决方案1】:

这里是关于输入循环的一个小见解,找到min_valuemax_valuesum

#include "../../std_lib_facilities.h"

int main(){
    // vector holding the input values
    vector<double> inputValues;
    // vector-input variable
    double temp = 0;                                    
    // variable holding the sum of the input 
    double sum = 0;                                     
    // variables holding the minimum and maximum input value
    double minVal = 100000; // note the initialization values
    double maxVal = -100000;

    vector<string>units;
    string unit;
    // prompt message; value input
    cout << "Enter the first value: ";
    while (cin >> temp >> unit) { 
        cout << "Enter the next value: ";
        inputValues.push_back(temp);
        units.push_back(unit); 
    }

    // conversion... 

    for (int i = 0; i < inputValues.size(); ++i){
        if (inputValues[i] > maxVal){ maxVal = inputValues[i]; }
        if (inputValues[i] < minVal){ minVal = inputValues[i]; }
        sum += inputValues[i];
    }

   // print result
   cout << "Minimum temperature = " << minVal << endl;
   cout << "Maximum temperature = " << maxVal << endl;
   cout << "Average temperature = " << sum/inputValues.size() << endl;
   return 0;
}

对于转换,您可以使用第二个容器,例如向量来存储units,然后为值和单位使用相同的索引创建一个循环,每个单位转换都有一个ifelse if块:

vector<string> units;
for(int i = 0 ; i < inputValues.size(); ++i){
    if(units[i] == "cm") inputValue[i] = inputValue[i] / 100.;
    else if(units[i] == "...") inputValue[i] =...;
    // ...
}

注意:

输入循环需要改进,例如终止条件,考虑带有do-while循环的终止关键字,处理错误的输入类型,cin.clear()等。

【讨论】:

  • 非常感谢您的参考。很有帮助。
  • @Julian Anger 欢迎来到 Stack Overflow。请注意,在这里说“谢谢”的首选方式是投票赞成好的问题和有用的答案(一旦你有足够的声誉这样做),并接受对你提出的任何问题最有帮助的答案(这也给出了你的声誉小幅提升)。请参阅About 页面和How do I ask questions here?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多