diff --git a/homework1/test1.cpp b/homework1/test1.cpp index f0e6b2c..6ac12db 100644 --- a/homework1/test1.cpp +++ b/homework1/test1.cpp @@ -4,25 +4,30 @@ using namespace std; typedef struct book { - string no; + string id; string name; double price; } book; -int main() { - book books[100]; - int i = 0; - - // input - for (i; true; i++) { - cin >> books[i].no >> books[i].name >> books[i].price; - if (books[i].no == "0" && books[i].name == "0" && books[i].price == 0) { - break; +int input(book *books) { + int length = 0; + for (int length = 0; true; length++) { + cin >> books[length].id >> books[length].name >> books[length].price; + if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { + return length; } } - // output - cout << i << endl; - for (int j = 0; j < i; j++) { - cout << books[j].no << "\t" << books[j].name << "\t" << fixed << setprecision(2) << books[j].price << endl; +} + +int output(book *books, int length) { + for (int i = 0; i < length; i++) { + cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl; } } + +int main() { + book books[100]; + int length = input(books); + output(books, length); + return 0; +}