2017-03-25 8 views
0

서점 인벤토리 (시작 코드)를 추적하는 프로그램을 작성 중입니다. 이 프로그램의 기능 중 하나는 전체 인벤토리를 다소 형식이 지정된 출력으로 인쇄 할 수 있다는 것입니다. 공백이나 공백없이 내용을 인쇄 해 보았습니다. .txt 인 파일의파일에서 읽은 구조체의 데이터를 인쇄하는 방법

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 

}; 

내용은 자신의 특정 데이터 유형으로 각각의 메모리 할당에 읽을 수 후 다음의 각 인쇄됩니다 :

나는 파일이 구조체로 초기화 된 배열로 읽고있다 .

         /* 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; 

다음을 취할 것을 요청됩니다 구조체의 ISBN 구성원 주소 (연산자 &)를 인쇄하여 인쇄하십시오. 따라서 출력에서 ​​볼 수있는 것은 모든 주소입니다.

당신은 단지 값 (안 주소) 인쇄 할 경우 - 이런 식으로, 당신은 "직접"원하는 주소 및 출력을하지 않습니다

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