代码 P 的问题:PP 演练 4
Issue with code P: PP Drill 4
我一直在做一本名为《编程:使用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;
相关文章:
- 理解递归代码的演练
- 在C++中,如果"int a = 3; int* p = &a;",那么为什么不允许"const int* &pp = p",但允许"const int* const &pp = p"?
- *++*++ppp,*++pp[1],*++(*(1+ppp)有什么具体的区别吗?(C/C++指针问题)
- Bjarne Stroustrup PPP - 第 9 章 演练 4 - cout 枚举错误
- 代码 P 的问题:PP 演练 4
- 编程:原理与实践 使用C++ 第 4 章演练步骤 6:关于数字范围的一般问题
- 编程原理和实践使用C++第 4 章演练,步骤 7
- 使用"cin"进行演练 #7 获得不同的结果 编程:使用C++的原则和实践 (Stroustrup) 第 4 章
- C++ 演练困境:
- 编程原理与实践:第 4 章演练第 1 部分
- 正在学习《编程原理与使用C++的实践》第4章中的演练.Bjarne Stroustrup
- pp标记还剩下哪些字符
- 编程原理和实践使用C++while-loop演练。找不到退出循环的方法
- C/pp套接字,recv()/send()只在gdb下工作
- Bjarne Stroustrup的P:PP Chapter 4 Drill
- 命名空间中没有名为'VarDictionary'的类型'pp'使用 PNaCl
- 斯特劳斯特鲁普的P:PP 第4章 钻头:卡住
- 有效和无效的PP代币的定义是什么?
- 系统睡眠时,pp::Core::GetTimeTicks()与pp::InputEvent::GetTimeStamp(
- 有没有更好的方法来解决这个问题 - 编程原理和实践使用C++:第 4 章 - 演练?