2017-03-25 14 views
0

書店の在庫(開始コード)を追跡するプログラムを作成しています。このプログラムの機能の1つは、多かれ少なかれフォーマットされた出力で全在庫を印刷できるということです。スペースや空白を入れずに内容を印刷するようにしました。ファイルから読み取った構造体のデータを印刷するには

私は構造体として初期化配列に読み込むファイルがあります:

struct Book //declare struct 
{ 

int ISBN; //ISBN within struct 

std::string Title; //Title within struct 

std::string Author; //Author within struct 

std::string Publisher; // Publisher within struct 

int Quantity; //Quantity within struct 

double Price; //price within struct 

}; 

.txtファイルの内容が特定のデータ型として、各メモリの割り当てに読み込ま取得し、これらのそれぞれがプリントアウトされますが。

         /* READ CONTENTS OF INVENTORY*/ 

string readInventory(ifstream &readFile) //define readInventory, pass it a refrence to an infile 
{ 
    const int ARRAY_size = 100; 

string_book read_out_inventory[ARRAY_size]; 

ifstream inFile; 

string stub = "\n\n***** COMPLETED: files read ******\n\n"; 
string buffer = " "; 

inFile.open("inventory.txt"); 

if (!inFile.eof()) 
{ 
    for (int i = 0; i < ARRAY_size; ++i) 
    { 
     inFile >> read_out_inventory[i].ISBN; 
     inFile >> read_out_inventory[i].Title; 
     inFile >> read_out_inventory[i].Author; 
     inFile >> read_out_inventory[i].Publisher; 
     inFile >> read_out_inventory[i].Quantity; 
     inFile >> read_out_inventory[i].Price; 

    } 
} 

cout << setw(43) <<"The books are: \n\n" ; 

for (int ARRAY_read = 0; ARRAY_read < ARRAY_size; ARRAY_read++) 
{ 

    cout << &read_out_inventory[ARRAY_read].ISBN << endl; 
    cout << &read_out_inventory[ARRAY_read].Title << endl; 
    cout << &read_out_inventory[ARRAY_read].Author << endl; 
    cout << &read_out_inventory[ARRAY_read].Publisher << endl; 
    cout << &read_out_inventory[ARRAY_read].Quantity << endl; 
    cout << &read_out_inventory[ARRAY_read].Price << endl; 

} 

return stub; 

}

私は(他の明白なものに囲まれた)に実行している問題は、出力があるということです。

0x7fff5fbfe250 
0x7fff5fbfe268 
0x7fff5fbfe280 
0x7fff5fbfe288 
0x7fff5fbfe290 
0x7fff5fbfe298 
0x7fff5fbfe2b0 
0x7fff5fbfe2c8 
0x7fff5fbfe2e0 
0x7fff5fbfe2e8 
0x7fff5fbfe2f0 
0x7fff5fbfe2f8 
0x7fff5fbfe310 
0x7fff5fbfe328 
0x7fff5fbfe340 
0x7fff5fbfe348 
0x7fff5fbfe350 
0x7fff5fbfe358 
0x7fff5fbfe370 
0x7fff5fbfe388 
0x7fff5fbfe3a0 
0x7fff5fbfe3a8 
0x7fff5fbfe3b0 
0x7fff5fbfe3b8 
0x7fff5fbfe3d0 
0x7fff5fbfe3e8 
0x7fff5fbfe400 
0x7fff5fbfe408 
0x7fff5fbfe410 
... 

私は私の問題はそうあるものであっても完全にはわからないんだけど助けていただければ幸いです。私はこの質問が別の場所で答えられているのを見ましたが、私は完全に理解していませんでした。

.txtファイルは次のようになります。あなたはそのようなコードの行を書くとき

20451 
My First Book 
Mark Lusk 
Pearson Publishing 
40 
45.34 
9780316 
Brown Family 
Mason Victor 
Little Brown 
36 
105.99 
1349877 
Story of My Life 
Norah M Jones 
CreateSpace Independent Publishing Platform 
20 
18 
1234567 
The Big Book 
Mypals Pennyweather 
GreenThumb 
4 
13.23 
+0

は私もそう構造体の配列を持つことが、将来のために有用であろう将来的にこれらの本をソートする必要がある:)私の作品、私はあなたのために書いた変更されたコードであり、校正 – TBurns

+1

あなたの構造体メンバーのアドレスを表示しています。 'cout <<&read_out_inventory [ARRAY_read]'の代わりに 'cout << read_out_inventory [ARRAY_read]'を書いてください。 '&'を単に削除してください。 – JustRufus

+1

すべての '&'を 'cout << ...'にドロップしてください –

答えて

0

あなたのコード内の多くのミスがあります印刷は、あなたが&演算子を使用して、すべてのインデックスのアドレスを印刷している間および

  1. ファイル。
  2. アレイ全体を印刷するときにArray_sizeを使用していますが、ファイルには2-3冊しかありません。
  3. テキストファイルに空白があり、あなたは本の本数を読み込んでいます。
  4. ここ
string readInventory() //define readInventory, pass it a refrence to an infile 
{ 
const int ARRAY_size = 100; 
int countBooksInFile=0; 
Book inventory[ARRAY_size]; 
ifstream inFile; 
string stub = "\n\n***** COMPLETED: files read ******\n\n"; 
inFile.open("inventory.txt"); 

if (!inFile.eof()) 
{ 
    inFile >> ws; 
    inFile >> inventory[countBooksInFile].ISBN; 
    inFile >> ws; 
    getline(inFile, inventory[countBooksInFile].Title); 
    inFile >> ws; 
    getline(inFile ,inventory[countBooksInFile].Author); 
    inFile >> ws; 
    getline(inFile, inventory[countBooksInFile].Publisher); 
    inFile >> ws; 
    inFile >> inventory[countBooksInFile].Quantity; 
    inFile >> ws; 
    inFile >> inventory[countBooksInFile].Price; 
    countBooksInFile++; 
} 
cout << "Total Books: " << countBooksInFile<<endl; 
cout << "The books are: \n"; 

for (int ARRAY_read = 0; ARRAY_read < countBooksInFile; ARRAY_read++) 
{ 

    cout << inventory[ARRAY_read].ISBN << endl; 
    cout << inventory[ARRAY_read].Title << endl; 
    cout << inventory[ARRAY_read].Author << endl; 
    cout << inventory[ARRAY_read].Publisher << endl; 
    cout << inventory[ARRAY_read].Quantity << endl; 
    cout << inventory[ARRAY_read].Price << endl; 

} 

return stub; 
} 
+0

ありがとうございました!ありがとうございました!私はすべての間違いを正確に突き止めることができず、それをあまりにも長く見ていました。 – TBurns

+0

@TBurnsあなたは私の友人を親切に歓迎する以上に投票し、受け入れられたように答えをマークします:) – Weaboo

0

:あなたは何をしているか

cout << &read_out_inventory[ARRAY_read].ISBN << endl; 

は以下の通りです:あなたが取ることを求めていますstructのISBNメンバのアドレス(オペレータ&)を印刷して印刷します。だから、あなたの出力に見えるものはすべてそれらのアドレスです。

あなただけの値(ないアドレス)を印刷したい場合 - あなたは「直接」何をしたいアドレスと出力を取ることはありません、このように:

cout << read_out_inventory[ARRAY_read].ISBN << endl; 
関連する問題