2017-07-07 9 views
-3

にはどうすればC++STOD()またはにstrtod()を使用せずに、私のreadDetails機能を書き換えることができますか? ++ 11が有効になっており、私はどのように私は(STODなしに書き換えることができます)

「STOD」はこのスコープのエラー以下

int readDetails(SmallRestaurant sr[]) 
{ 
//Declaration 
ifstream inf; 

//Open file 
inf.open("pop_density.txt"); 

//Check condition 
if (!inf) 
{ 
    //Display 
    cout << "Input file is not found!" << endl; 

    //Pause 
    system("pause"); 

    //Exit on failure 
    exit(EXIT_FAILURE); 
} 

//Declarations and initializations 
string fLine; 
int counter = 0; 
int loc = -1; 

//Read 
getline(inf, fLine); 

//Loop 
while (inf) 
{ 
    //File read 
    loc = fLine.find('|'); 
    sr[counter].nameInFile = fLine.substr(0, loc); 
    fLine = fLine.substr(loc + 1); 
    loc = fLine.find('|'); 
    sr[counter].areaInFile = stod(fLine.substr(0, loc)); //line using stod 
    fLine = fLine.substr(loc + 1); 
    loc = fLine.find('|'); 
    sr[counter].popInFile = stoi(fLine.substr(0, loc)); 
    fLine = fLine.substr(loc + 1); 
    sr[counter].densityInFile = stod(fLine); //line using stod 
    counter++; 
    getline(inf, fLine); 
} 

//Return 
return counter; 
} 

で宣言されていなかった得るcは、私が使用することになります コンパイラが持っていない私は読んしようとしているテキストです。

国勢調査トラクト201、オートーガ郡、アラバマ州| 9.84473419420788 | 1808年| 183.651479494869 国勢調査トラクト202、オートーガ郡、アラバマ州| 3.34583234555866 | 2355年| 703.860730836106 国勢調査トラクト203、オートーガ郡、アラバマ州| 5.35750339330735 | 3057 | 570.60159846447

答えて

0

std::istringstreamを使用してください。

std::string number_as_text("123"); 
int value; 
std::istringstream number_stream(number_as_text); 
number_stream >> value; 

編集2:これらの知識をひけらかす人のために:
以下の例は、二重を読み込みます。上の整数の読み方と非常によく似たパターン。

std::string number_as_text("3.14159"); 
double pi; 
std::istringstream number_stream(number_as_text); 
number_stream >> pi; 

sprintfの亜種を使用することもできます。

編集1:別の解析方法
あなたが試みることができる:

std::string tract; 
std::string county; 
std::string state; 
double value1; 
char separator; 

//... 
std::ifstream input("myfile.txt"); 
//... 
std::getline(input, tract, ','); 
std::getline(input, county, ','); 
std::getline(input, state, '|'); 
input >> value1; 
input >> separator; 
//... 
input.ignore(100000, '\n'); // Ignore any remaining characters on the line 

上記番号への変換別の文字列を必要としません。

+1

さあ、これは彼らが求めている変換でさえない。また、それは明らかにいくつかの詐欺です。 –

+1

@BaummitAugen:文字列を10進数(数値)に変換することを尋ねています。これは 'std :: istringstream'の動作です。 –

+0

@BaummitAugen:ファイルの読み方については、たくさんの投稿がありますが、それらはすべて少し異なります。これは '|'セパレータとしてだけでなく、 '、'。 –

関連する問題