2016-10-12 14 views
-1

私は入力ファイルを読み込み、入力されたキーに基づいて解読するカスタム解読プログラムを用意しています。 テキストファイル:私はしかし私が困って読書を持っている、キーや言葉の量の数を読み込むためのプログラムのために管理している顧客解読プログラムC++問題の読解

[Number of Words] 
[Number of Keys] 

[Word] [Jump] [Word] [Jump] ... [Word] [Jump] 

[Key] [Key] ... [Key] 

23 
11 

Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2 computers, 0 not 0 the 0 open 2 car! 2 C, 0 lack 0 of 0 dog 1 green 2 C++ 0 bottle 2 wrong, 2 them. 0 

5 1 10 21 9 6 21 11 13 16 20 

このファイルは、次のように表されます単語とその隣の数字、最後の数字をキーとして登録します。これはこれまで私が持っているものです:

#include <iostream> 
#include <fstream> 

using namespace std; 

struct pieces { 

char word; 
int jump; 

} ; 

// Main Function 
int main() 
{ 
// declare variables 
int keyCount = 1; 
int wordCount = 8; 
int wordAmount[8]; 
int keyAmount[8]; 
pieces cipher[5]; 
char decoded[20][20]; 
char filename[10]; 
int keys[keyCount]; 
char tArray[20][20]; 
ifstream inData; 

    //prompt user for input file 

cout << " Enter file name: "; 
cin >> filename; 

inData.open(filename); 

if(inData.is_open()); 
{ 

    // read list of names into array 

    for (int i = 0; i < keyCount; ++i){   

    inData >> wordAmount[i] >> keyAmount[i]; 

     for(int j = 0; j < wordCount; j++){ 

     inData >> cipher[j].word >> cipher[j].jump; 

     } 

    } 

cout << " Key Count: " << keyCount << "\n"; 

    // print out 

     for (int i = 0; i < keyCount; ++i){ 

    cout << " KeyAmount: "; 
    cout << keyAmount[i] << "\n"; 
    cout << " WordAmount: "; 
    cout << wordAmount[i] << "\n"; 

     for(int j = 0; j < wordCount; j++){ 

     cout << cipher[j].word << " " << cipher[j].jump; 

      } 

      } 

    } 

inData.close(); 

    return 0; 
} 

私は配列としてchar単語を入れてみましたが、その後、私はセグメンテーションファイルを得ました。何かアドバイスをいただければ幸いです!あなたの入力は次のようなものになります

struct Word_Jump 
{ 
    std::string word; // Using std::string because it has space for more than one character. 
    int jump; 
    friend std::istream& operator>>(std::istream& input, Word_Jump& wj); 
}; 

std::istream& 
operator>>(std::istream& input, Word_Jump& wj) 
{ 
    input >> wj.word; 
    input >> jump; 
    return input; 
} 

::のための同様

std::vector<Word_Jump> database; 
for (unsigned int i = 0; i < number_of_words; ++i) 
{ 
    Word_Jump wj; 
    my_data_file >> wj; 
    database.push_back(wj); 
} 

を、あなたの入力をあなたがstd::stringを使用してフォーマットされた入力オペレータ>>をオーバーロードしている場合

+1

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低でも、あなたはあなたが行った観察と一緒に、[編集]あなたの質問あなたの問題を再現[、最小完全、かつ検証](http://stackoverflow.com/help/mcve)の例を含むようにする必要があります\しますデバッガ。 –

+0

複数の文字(単語、センテンス、*テキスト行*など)に対して、* single *文字に 'char'を、そして複数の文字に' std :: string'を使うことをお勧めします。 –

答えて

0

IMHO、あなたのプロジェクトが単純になります読み取りキー:

std::vector<int> key_database; 
for (unsigned int j = 0; j < number_of_keys; ++j) 
{ 
    int k = 0; 
    my_data_file >> k; 
    key_database.push_back(k); 
} 

これは、単語&のジャンプペアとそのキーを読むための2つの可能な方法です。

関連する問題