2016-04-26 4 views
0

私は入力として名前をとり、最終的にバイナリ検索&を並べ替えるプログラムに取り組んできました。しかし、配列を動的なサイズにしようとすると(ループの繰り返しごとに1つずつ増加する)、さまざまな問題が発生しました。動的配列のサイズとgetline()でのクラッシュ。

私は文字列配列を20個の要素で構成することができますし、プログラムは動作しますが、私の割り当てに余分なクレジットはそれを動的なサイズにすることです。現在、プログラムは "getline(cin、Names [x]);"に達するとエラーコードなしでクラッシュします。 私は周りを探索してきましたが、この場合は配列の代わりにベクトルを行うほうが簡単かもしれないことは分かっていますが、この割り当てでベクターを使用することは許されていません。それが作成された後

おかげ

オリジナルコードは

using namespace std; 
#include <iostream> 
#include <string> 

void main() 
{ 
    int x = 0; 
    string * Names = new string[x]; 
    bool NameInputEnd(0); 

    cout << " Enter your names to be sorted\n"; 
    cout << "To exit just press [Enter] at any time\n"; 

    do 
    { 
     cout << x << endl; 
     cout << "\n< Name " << (x + 1) << " > = "; 

     !!**CRASHES HERE**!! 

     getline(cin, Names[x]); 

     if (Names[x].empty() || x == 19) 
     { 
      cout << "\nFinal Name Amount = " << (x + 1) << endl << endl; 
      NameInputEnd = 1; 
      continue; 
     } 

     x++; 

    } while (NameInputEnd == 0); 

    delete [] Names; 
} 

変更

int tempsize(1), x(0); 
string * Names = new string[tempsize]; 
... 

do 
{ 
... 
    x++; 
    tempsize++; 
}while (NameInputEnd == 0); 
+0

を使用するとき、X ' 'の値が何であるかを考えてみて'文字列*名=新しい文字列[X]; ' 。何個の要素を作りましたか( 'x'の価値)?また、 'std :: vector'を使うことに変えてください。 – NathanOliver

+0

ああ、私は0の要素のサイズを持つように配列を設定していたことを理解しています。これは検索したものからは無効です。私は今、代入する一時変数を作成しました。最初は1に設定され、各ループ反復でxと同時に1ずつ増加します。 –

+1

アレイは作成後は拡張できません。 'x'をインクリメントすると、配列のサイズが魔法的に増加することはありません。最初に十分なスペース( 'string * Names = new string [20];')を割り当てるか、サイズが大きくなる 'std :: vector'を使います。 –

答えて

1

配列のサイズを変更することはできません。あなたはそれを破壊し、既存のデータのコピーを持つ新しい配列を作成する必要があります。たとえば:

#include <iostream> 
#include <string> 
#include <algorithm> 

using namespace std; 

void main() 
{ 
    int x = 0; 
    int capacity = 20; 
    string * Names = new string[capacity]; 
    string Name; 

    cout << " Enter your names to be sorted\n"; 
    cout << "To exit just press [Enter] at any time\n"; 

    do 
    { 
     cout << x << endl; 
     cout << "\n< Name " << (x + 1) << " > = "; 

     if ((!getline(cin, Name)) || Name.empty()) 
      break; 

     if (x == capacity) 
     { 
      int newCapacity = capacity + 20; 
      string *newNames = new string[newCapacity]; 
      copy(Names, Names + x, newNames); 
      delete [] Names; 
      Names = newNames; 
      capacity = newCapacity; 
     } 

     Names[x] = Name; 
     ++x;  
    } 
    while (true); 

    cout << "\nFinal Name Amount = " << x << endl << endl; 

    delete [] Names; 
} 

は、あなたは本当にかかわらず、std::vectorを使用する必要があります。

#include <iostream> 
#include <string> 
#include <vector> 

using namespace std; 

void main() 
{ 
    vector<string> Names; 
    string Name; 

    Names.reserve(20); // optional 

    cout << " Enter your names to be sorted\n"; 
    cout << "To exit just press [Enter] at any time\n"; 

    do 
    { 
     cout << Names.size() << endl; 
     cout << "\n< Name " << (Names.size() + 1) << " > = "; 

     if ((!getline(cin, Name)) || Name.empty()) 
      break; 

     Names.push_back(Name); 
    } 
    while (true); 

    cout << "\nFinal Name Amount = " << Names.size() << endl << endl; 
} 
+0

これだけでなく、さまざまな構文を理解するのにも役立ちました。私のコーディングでは、実際には多くのエラーがあります。ありがとうMr Lebeau –

関連する問題