【发布时间】:2013-09-25 11:38:28
【问题描述】:
我是一名初学者,在学习 Stroustrup 的原则和实践时遇到了这么一个简单的问题。
仅使用基本元素
#include "std_lib_facilities.h"
int main()
{
double highest = 0;
double lowest = 100;
int i=0;
double sum = 0;
vector <double> inputlist;
double input;
string unit;
cout<<"Type in a number followed by it's unit \n";
while(cin>>input>>unit){
inputlist.push_back(input);
sum += inputlist[i];
if (input >= lowest && input <= highest){
cout<<input<<" \n";
++i;
}
else if (input < lowest){
lowest = input;
cout<<"\nLowest Number so far \n"<<lowest;
++i;
}
else if (input > highest){
highest = input;
cout<<"\nHighest number so far \n"<< highest;
++i;
}
else
cout<<"Lowest is: \n"<<lowest<<"\n\n Highest is: \n"<<highest<<" \n\n and the total is: \n"<<sum;
if (unit == "ft", "m", "in","cm")
cout<<unit<<"\n";
else
cout<<"cannot recognize unit";
}
keep_window_open();
return 0;
}
当字符“|”时,我需要程序向用户显示总和以及最高和最低值被输入。问题是:我需要在应该输入整数值的地方输入这个。
注意:我对转换了解不多,但尝试了一些,但没有成功。
【问题讨论】:
-
只保留输入为字符串,测试“|”然后转换为整数。 SO上有很多关于后者的例子。