こんにちは、動的に割り当てられた符号なし整数に(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.
すべてのポインタにポイントがありますか?しかしstd :: stringを値渡しします。 – DeiDei
ポインタで渡してみましたが、どちらもうまくいきませんでした。 ポインタ: integersNumberOf_Updateは、stool doとsize_tのようにconsole()で値を直接変更することです。 整数は非常に大きくなる可能性があるので、整数は動的に割り振られなければなりません。 integersConvertedは単にintegersFoundへのポインタを受け取ります。 –