【发布时间】:2018-11-20 08:33:25
【问题描述】:
所以我将我的数组值设置为 100,当我想显示我的数组时,其他没有值的数组也会显示。有什么办法可以去掉?谢谢!
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<fstream>
#include<limits>
#include<sstream>
using namespace std;
char add();
char list();
struct Book //structure for the book
{
char title[50];
char author[50];
int price;
void display() {
cout << "Name:" << title << endl
<< "Author:" << author<< endl
<< "Price:" << price << endl << endl;
}
};
Book book[100];
void main()
{
int selection;
do {
cout << "Activity Selection: " << endl;
cout << "1. Add book" << endl;
cout << "2. List all books" << endl;
cout << "3. Search book" << endl;
cout << "4. Check total book purchased and total spent" << endl;
cout << "5. Quit" << endl;
cout << "Please enter your selection(1/2/3/4/5): ";
cin >> selection;
cin.ignore();
switch (selection)
{
case 1:
add();
break;
case 2:
list();
break;
case 3:
cout << "number 3" << endl;
break;
case 4:
cout << "number 4" << endl;
break;
default:
exit(1);
break;
}
} while (selection <= 4);
system("pause");
}
char add()
{
int i;
ofstream outFile;
outFile.open("Records.dat", ios::app);
if (!outFile.is_open())
{
cout << "Can’t open file.";
system("pause");
exit(1);
}
cout << "Please enter the book title:";
cin.getline(book[i].title, 51);
cout << "Please enter the author name of the book:";
cin.getline(book[i].author, 51);
cout << "Please enter the price of the book RM: ";
cin >> book[i].price;
cout << "\nThe book\""<< book[i].title <<"\"has been added to the system.\n" <<
endl;
outFile << book[i].title << "," << book[i].author << "," << book[i].price <<
endl;
outFile.close();
}
char list() //second solution but still doesnt work need some fixing here i think
{
int i;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile,line))
{
inFile.get(data);
if (!(data == 0));
cout << data;
}
cout << endl;
inFile.close();
}
当我调试这样的东西时: ss
这是我提出的第二个解决方案,但仍然不起作用
char list()
{
int i , count = 0;
string line, token;
ifstream inFile;
inFile.open("Records.dat", ios::in);
if (!inFile.is_open())
{
cout << "File could not open" << endl;
system("pause");
exit(1);
}
char data;
while (getline(inFile, line))
{
stringstream ss(line);
getline(ss, token, ',');
stringstream(token) >> book[i].title;
getline(ss, token, ',');
stringstream(token) >> book[i].author;
getline(ss, token, ',');
stringstream(token) >> book[i].price;
i++;
}
cout << endl;
inFile.close();
for (int count = 0; count < i; count++)
book[i].display();
}
问题是我真的不知道如何使用 dat 文件显示数组,我成功显示它但出现错误
【问题讨论】:
-
如果没有更多上下文,很难回答您的问题。
Records.dat到底是什么?可能您应该删除if (!(data == 0));之后的分号,这似乎是错误。 -
您的阵列是什么以及在哪里?其他数组是什么?调试意味着使用像 gdb 这样的工具,您可以在其中分析程序流程的每个步骤。
-
if (!(data == 0));看起来很可疑。编译器应该警告你它没有副作用。 -
该代码不包含数组,只包含一个文件,我们无法知道该文件应该包含什么。我们怎么能在这里回答。您真的应该阅读 How to Ask 并考虑一下 minimal reproducible example 应该是什么......只是多一点
while (!inFile.eof())总是错误的,因为您处理了一个未读值。 -
哦,对不起,我是新用户,我真的不知道如何发布代码,我会尝试在这里发布整个代码