我是初學者,在通過Stroustrup的原則和實踐時遇到了這樣一個簡單的問題。整數應該是輸入字符串 - C++
只使用基本的要素
#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;
}
我需要的程序,以顯示用戶之和的最高值和最低值當字符「|」被輸入。問題是:我需要輸入整數值的地方。
注:我不太瞭解轉換,但嘗試了一些,但他們沒有工作。
只要保持輸入爲一個字符串,測試「|」並在之後轉換爲整數。關於後者的大量例子在這裏。 –