bli*_*044 3 c++ arrays struct function
我完全坚持这个任务.我首先在int main()中写了所有内容,没有任何问题.这一切都很可爱!不幸的是,我们的教练希望它分成多个功能(每个功能少于35行).我已将其分解,如下所示,但不幸的是,我的知识(和谷歌没有太多帮助)的功能和传递/参考通过它们并不是那么高.我的程序现在根本不起作用.所有的"书籍"都会出错,所以我不确定我是否正在不正确地传递结构或数组.请帮忙!
原始的txt文件如下:
number of books
title
author
price
title
author
price
Run Code Online (Sandbox Code Playgroud)
码:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void setStruct() {
struct bookTable {
string title;
string author;
double price;
};
}
void setArray(int &arraySize, struct bookTable *Book[]) {
ifstream infile;
int bookCounter = 0;
infile.open("books2.txt");
if (!infile) {
cout << "Unable to open Books.txt" << endl;
}
infile >> arraySize;
infile.ignore(100, '\n');
bookTable *Book = new bookTable[arraySize];
infile.close();
}
void readFile(struct bookTable *Book[]) {
ifstream infile;
int bookCounter = 0;
infile.open("books2.txt");
for (int i = 0; getline(infile, Book[i].title); i++) {
getline(infile, Book[i].author, '\n');
infile >> Book[i].price;
infile.ignore(100, '\n');
bookCounter++;
}
infile.close();
}
void displayMenu(struct bookTable *Book[]) {
int menuChoice = 0, bookCounter = 0;
string findTitle;
do { cout << "\n===== Bookstore App =====" << endl;
cout << "1. Print Books" << endl;
cout << "2. Change Price" << endl;
cout << "3. Quit" << endl;
cout << "\nEnter Choice: ";
cin >> menuChoice;
if (menuChoice == 1) {
for (int i = 0; i < bookCounter; i++) {
cout << "===== BOOK =====" << endl;
cout << "Title: " << Book[i].title << endl;
cout << "Author: " << Book[i].author << endl;
cout << "Price: " << fixed << setprecision(2) << Book[i].price << endl; } }
else if (menuChoice == 2) { cin.ignore(100, '\n');
cout << "What is the title of the book? ";
getline(cin, findTitle, '\n');
for (int i = 0; i < bookCounter; i++) {
if (findTitle == Book[i].title) {
cout << "Enter New Price: " << endl;
cin >> Book[i].price;
}
else if (findTitle != Book[i].title) {
cout << "Unable to find Book" << endl;
}}}
else if (menuChoice < 1 || menuChoice > 3) {
cout << "Invalid Entry. Please enter 1, 2, or 3 from the options menu." << endl;
} } while (menuChoice != 3);
}
void writeFile(int arraySize, struct bookTable *Book[]) {
ofstream outfile;
int bookCounter = 0;
outfile.open("sale2.txt");
outfile << arraySize << endl;
for (int i = 0; i < bookCounter; i++) {
outfile << Book[i].title << endl;
outfile << Book[i].author << endl;
outfile << fixed << setprecision(2) << Book[i].price << endl;
}
outfile.close();
delete[] Book;
}
int main() {
setStruct();
setArray();
readFile();
displayMenu();
writeFile();
cout << "\nSale2.txt has been created." << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我没有编译或运行它,但希望它能让你开始朝着正确的方向前进:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// This declares "struct bookTable"
// You need to actually define a variable of this type later in your program
struct bookTable {
string title;
string author;
double price;
};
bookTable * setArray(int &arraySize, struct bookTable *Book[]) {
ifstream infile;
int bookCounter = 0;
infile.open("books2.txt");
if (!infile) {
cout << "Unable to open Books.txt" << endl;
}
infile >> arraySize;
infile.ignore(100, '\n');
bookTable *Book = new bookTable[arraySize];
infile.close();
// This returns an empty array of bookTable[]
return Book;
}
void readFile(struct bookTable *Book) {
ifstream infile;
int bookCounter = 0;
infile.open("books2.txt");
for (int i = 0; getline(infile, Book[i].title); i++) {
getline(infile, Book[i].author, '\n');
infile >> Book[i].price;
infile.ignore(100, '\n');
bookCounter++;
}
infile.close();
}
void displayMenu(struct bookTable *Book[]) {
int menuChoice = 0, bookCounter = 0;
string findTitle;
do { cout << "\n===== Bookstore App =====" << endl;
cout << "1. Print Books" << endl;
cout << "2. Change Price" << endl;
cout << "3. Quit" << endl;
cout << "\nEnter Choice: ";
cin >> menuChoice;
if (menuChoice == 1) {
for (int i = 0; i < bookCounter; i++) {
cout << "===== BOOK =====" << endl;
cout << "Title: " << Book[i].title << endl;
cout << "Author: " << Book[i].author << endl;
cout << "Price: " << fixed << setprecision(2) << Book[i].price << endl; } }
else if (menuChoice == 2) { cin.ignore(100, '\n');
cout << "What is the title of the book? ";
getline(cin, findTitle, '\n');
for (int i = 0; i < bookCounter; i++) {
if (findTitle == Book[i].title) {
cout << "Enter New Price: " << endl;
cin >> Book[i].price;
}
else if (findTitle != Book[i].title) {
cout << "Unable to find Book" << endl;
}}}
else if (menuChoice < 1 || menuChoice > 3) {
cout << "Invalid Entry. Please enter 1, 2, or 3 from the options menu." << endl;
} } while (menuChoice != 3);
}
// !!! DON'T UNCOMMENT THIS UNTIL YOU FIGURE OUT HOW TO PRESERVE "arraySize" !!!
// Suggestion: use a C++ "vector<>" instead of an array...
// void writeFile(int arraySize, struct bookTable *Book[]) {
//
// ofstream outfile;
// int bookCounter = 0;
//
// outfile.open("sale2.txt");
//
// outfile << arraySize << endl;
//
// for (int i = 0; i < bookCounter; i++) {
//
// outfile << Book[i].title << endl;
// outfile << Book[i].author << endl;
// outfile << fixed << setprecision(2) << Book[i].price << endl;
// }
//
// outfile.close();
//
// delete[] Book;
//
// }
int main() {
// setStruct(); // Not needed
struct bookTable *book_table = setArray(); // Allocate space
readFile(book_table); // Initialize data
displayMenu(book_table); // use book_table
// writeFile(); // TBD
cout << "\nSale2.txt has been created." << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
主要说明:
当你在"main()"中拥有所有内容时,所有代码都可以看到所有变量.
当您将所有内容移动到单独的函数中时,函数将无法再"看到"这些变量.
这被称为"范围"
一种解决方案是将所有内容放回"main()"中.这是不好的.
另一种解决方案是使您的变量全局化.这也是坏事.
一个好的解决方案是在"main()"中声明需要共享的变量,然后将它们作为参数传递.这就是我上面所说的.
更好,更高级的解决方案可能是将您的程序重构为类.
由于您使用C++进行编程,并且由于您具有可变数量的元素,因此将数组更改为C++ 向量可能是个好主意.这有几个优点,包括:
一个.您不再需要阅读整个文件只是为了找到#/ elements - 您可以随意添加新元素.
湾 您始终可以查询"vector.size()"以查找当前的#/元素.
还有其他问题.
我希望有帮助......如果您有任何疑问,请回复.