2013-09-25 15 views
1

我是初學者,在通過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; 
} 

我需要的程序,以顯示用戶之和的最高值和最低值當字符「|」被輸入。問題是:我需要輸入整數值的地方。

注:我不太瞭解轉換,但嘗試了一些,但他們沒有工作。

+1

只要保持輸入爲一個字符串,測試「|」並在之後轉換爲整數。關於後者的大量例子在這裏。 –

回答

3

如果我理解正確的,你想從std::cin閱讀int,但:

int i; 
if (std::cin >> i) { 
    ... 

不適合您的需求,因爲有可能是'|'標誌作爲讀取終止的信號。

這裏是你能做什麼:通過字(std::string)讀取輸入字並解析這些詞單獨使用臨時std::istringstream

std::string word; 
if (std::cin >> word) { 
    if (word == "|") 
     ... 
    // else: 
    std::istringstream is(word); 
    int i; 
    if (is >> i) { 
     // integer successfully retrieved from stream 
    } 
} 

只是#include <sstream>

+0

謝謝,這個作品很棒。如果你能簡單地解釋一下這個我會非常棒! – RavenEye

0

閱讀與字符串值。如果它不匹配|使用以下函數將其轉換爲雙精度:

double toDouble(string s) 
{ 
    int sign = 1, i=0; 
    if (s[0]=='-') 
     sign = -1, i=1; 

    double result = 0, result2 = 0; 
    for (; i < s.size(); i++) 
    if (s[i] == '.') 
     break; 
    else 
     result = result * 10 + (s[i] - '0'); 

    for (i = s.size()-1 ; i>=0 ; i--) 
    if (s[i] == '.') 
     break; 
    else 
     result2 = result2/10 + (s[i] - '0'); 

    if (i>=0) 
     result += result2/10; 
    return result * sign; 
} 
+0

對不起。我更新了。 – hasan83

0

用英寸計算米數並沒有多大意義。因此,您應該考慮將這些單位轉換爲比例因子。您可以使用地圖來獲取比例因子。 即使這有點超調,也可以使用正則表達式來解析用戶輸入。如果正則表達式不匹配,你可以測試像「|」這樣的東西。 在新的C++標準(http://en.wikipedia.org/wiki/C%2B%2B11)中爲此定義了一個正則表達式庫。可惜,g ++正則表達式庫很麻煩。但你可以使用提升(http://www.boost.org/doc/libs/1_54_0/libs/regex/doc/html/boost_regex/)。 這裏是一個例子:

#include <iostream> 
#include <vector> 
#include <map> 
#include <boost/regex.hpp> //< Pittyingly std::regex is buggy. 

using namespace std; ///< Avoid this in larger projects! 
using namespace boost; 

int main() { 

const string strReFloat("([-+]?[[:digit:]]*\\.?[[:digit:]]+(?:[eE][-+]?[[:digit:]]+)?)"); 
const string strReUnit("([[:alpha:]]+)"); 
const string strReMaybeBlanks("[[:blank:]]*"); 
const string strReFloatWithUnit(strReMaybeBlanks+strReFloat+strReMaybeBlanks+strReUnit+strReMaybeBlanks); 
const regex reFloatWithUnit(strReFloatWithUnit); 

const map<const string,double> unitVal= { 
    {"m", 1.0}, 
    {"in", 0.0254}, 
    {"ft", 0.3048}, 
    {"cm", 0.01} 
}; 

double highest = 0; 
double lowest = 100; 
int i=0; 
double sum = 0; 
vector <double> inputlist; 
double input; 
double unitToMeter; 
string unit; 
string str; 

while((cout<<"\nType in a number followed by it's unit \n", getline(cin,str), str != "")){ 

    smatch parts; 

    if(regex_match(str,parts,reFloatWithUnit)) { 
     unit = parts[2].str(); 

     auto found = unitVal.find(unit); 
     if(found != unitVal.end()) { 
      cout<<unit<<"\n"; 
      input = found->second * atof(parts[1].str().c_str()); 
     } else { 
      cout << "Unit \"" << unit << "\" not recognized. Using meters.\n"; 
     } 

     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 if(str == "|") { 
     cout << "sum:" << sum << "\n"; 
    } else { 
     cout << "Input not recognized.\n"; 
    } 
} 

return 0; 
} 
相關問題