서점 인벤토리 (시작 코드)를 추적하는 프로그램을 작성 중입니다. 이 프로그램의 기능 중 하나는 전체 인벤토리를 다소 형식이 지정된 출력으로 인쇄 할 수 있다는 것입니다. 공백이나 공백없이 내용을 인쇄 해 보았습니다. .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
내가 너무 너무 구조체의 배열을 갖는 것은 미래를 위해 도움이 될 것입니다 미래에이 책을 정렬해야합니다 :) 나를 위해 작동 내가 당신을 위해 쓴 수정 된 코드입니다 교정. – TBurns
구조체 멤버의 주소를 인쇄하고 있습니다. 'cout <<과 read_out_inventory [ARRAY_read] 대신'cout << read_out_inventory [ARRAY_read]'를 쓰십시오. 그냥'&'를 제거하십시오. – JustRufus
'cout << ...'에있는 모든'&'를 버리십시오. –