2017-12-11 11 views
2

こんにちは、動的に割り当てられた符号なし整数に(100)102などの数値の行を変換しようとしています。期待されるのは、可変長入力で、数値を配列としてアクセスできることです。私は2個の数字以上、すべての位置は、最初の整数を取得する入力ならば、私の入力だけ数が、全体1024のアレイが印刷されている場合 整数配列のstoul :: stringを使用したstd :: stringでの出力が正しくありません

#include <iostream> 
#include <new> 
#include <string> //Memset 
int console(){ 
    std::string console_buffer; 
    unsigned long int* integersConverted = NULL; 
    unsigned int integersNumberOf = 0; 
    for(; ;){ 
     std::getline(std::cin, console_buffer); 
     integersConverted = console_defaultSyntaxProcessing(console_buffer, &integersNumberOf); 

     std::cout << "Found the following integers from conversion: "; 
     for(unsigned int debug_tmp0 = 0; debug_tmp0 < integersNumberOf; debug_tmp0++){ 
      std::cout << integersConverted[debug_tmp0] << " "; 
      std::cout << std::endl; 
     } 

     delete integersConverted; 
     integersConverted = NULL; 
    } 
    return 0; 
} 

unsigned long int* console_defaultSyntaxProcessing(std::string console_buffer, unsigned int* integersNumberOfUpdate){ 
    *integersNumberOfUpdate = 0; 
    unsigned int integersNumberOf = 0; 
    unsigned long int* integersFound = NULL; 
    integersFound = new unsigned long int(sizeof(unsigned long int) * 1024); 
    std::size_t stringPosition = 0; 
    for(; stringPosition < console_buffer.length() && integersNumberOf < 1024;){ 
     integersFound[integersNumberOf] = std::stoul(console_buffer, &stringPosition, 10); //10 = Decimal 
     integersNumberOf++; 
    } 
    *integersNumberOfUpdate = integersNumberOf; 
    return integersFound; 
} 

は、私が正しい値を取得しています。私は手動で関数std :: stringを定数に設定しようとしましたが、console_buffer.length()をゼロにして '\ 0'などを探します。残念ながら働いていません。

更新日 ---トピックの最初の投稿後5分。 問題は、Yashasが答えたように、console_defaultSyntaxProcessing forループにあります。 stoul & stringPositionは、std :: stringの位置ではなく、読み込んだ文字の数を返します。 Iは入力100(101、それが動作しない場合 stoulを使用して別の問題はそれほど固定コードに従うが、使用してはならない、である。 lamandyは、提案利用STDとして::にstringstreamを代わりに。

std::size_t stringPosition = 0; 
std::size_t stringPositionSum = 0; 
for(; stringPosition < console_buffer.length() && integersNumberOf < 1024;){ 
    try{ 
     integersFound[integersNumberOf] = std::stoul(&console_buffer[stringPositionSum], &stringPosition, 10); 
     integersNumberOf++; 
     stringPositionSum = stringPositionSum + stringPosition; 
    } 
    catch(std::exception& exception){ 
     break; 
    } //This catch will be used constantly by this buggy code. 
+0

すべてのポインタにポイントがありますか?しかしstd :: stringを値渡しします。 – DeiDei

+0

ポインタで渡してみましたが、どちらもうまくいきませんでした。 ポインタ: integersNumberOf_Updateは、stool doとsize_tのようにconsole()で値を直接変更することです。 整数は非常に大きくなる可能性があるので、整数は動的に割り振られなければなりません。 integersConvertedは単にintegersFoundへのポインタを受け取ります。 –

答えて

1
for(; stringPosition < console_buffer.length() && integersNumberOf < 1024;){ 
     integersFound[integersNumberOf] = std::stoul(console_buffer, &stringPosition, 10); //10 = Decimal 
     integersNumberOf++; 
    } 

は、あなたがやりたいことはありません。

をあなたは何度も何度もstd::stoulに同じ文字列を渡している。あなたのstd::stoul関数は最初の数を毎回読んで保持しています。あなただけの1つの数を持っていた場合は、stringPosition < console_buffer.length()はにあなたのループを引き起こしましたあなたが複数の番号を持っている場合はstringPositionは、console_buffer.length()を決して超えません。

std::stoulの2番目のパラメータは、文字列のどこから読み込みを開始するかを決定しません。処理された文字数が表示されます。

あなたが扱っているタスクについては、stringstreamが必要です。

#include <iostream> 
#include <sstream> 
#include <array> 

int main() 
{ 
    std::istringstream console_buffer("123 345 3 5 2 3 4 5 6 7 7 232 34 332 234 55"); 

    std::array<unsigned long, 1024> integerArray; 
    size_t count = 0; 
    while(console_buffer && count < integerArray.size()) 
     console_buffer >> integerArray[count++]; 

    for(int i = 0; i < count; i++) 
     std::cout<<integerArray[i] << ' '; 
    return 0; 
} 
+0

ああ私の神!私はすでに2時間ほどコードを修正しようとしています。あなたの答えが大きくなると、try-catchでコードを修正しました。それは本当に私がそれを使用して私の最初の時間としてそのパラメータを理解していない必要が何もしないstoulとしてまだ非常に不安定です。 メインポストは固定コードで更新されますが、どこでも使用されることはありません。 –

+0

文字列ストリームを使用する必要があります。彼らは 'cin'と 'cout'のように動作します。相違点は文字列が文字列で動作するのに対し、 'cin'と' cout'は 'stdin'と' stdout'で動作することです。 'unsigned long'に' typedef'を使うことも考えてください。 – Yashas

+0

補遺:[最近追加された 'std :: size'](http://en.cppreference.com/w/cpp/iterator/size)は、必要性を排除します。あなたはこれを無意味なものにしましたか? – user4581301

1

あなたの仕事を容易にするためにstd::vectorstd::stringstreamを使用することを検討してください。

std::vector<unsigned long int> StringToIntegerVector(const std::string& input) 
{ 
    std::istringstream iss(input); 
    unsigned long int temp; 
    std::vector<unsigned long int> results; 
    while (iss >> temp) 
     results.push_back(temp); 
    return results; 
} 
+0

サンプルコードのおかげで多くの!私はちょうど私のchunckyコードよりも小さくなります。 –

関連する問題