【问题标题】:How can i display the array without other array with no value? C++如何在没有其他没有价值的数组的情况下显示数组? C++
【发布时间】: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()) 总是错误的,因为您处理了一个未读值。
  • 哦,对不起,我是新用户,我真的不知道如何发布代码,我会尝试在这里发布整个代码

标签: c++ arrays file-io char


【解决方案1】:

您正在将 CSV 作为文本文件读取并打印出来。 所以,解决方法是做一个记录类,从流中读取,然后检查它是否为空。

#include <iostream>
#include <sstream>
using namespace std;

class Record {
private:
    std::string title;
    std::string shortTitle;
    int number;
public:
    Record(std::string title, std::string shortTitle, int number): title(title),
                                                         shortTitle(shortTitle),
                                                                 number(number){

    }
    inline std::string getTitle() const { return title; }
    inline std::string getShortTitle() const { return shortTitle; }
    inline int getNumber() const { return number; }
};

// Make this function capable of handling any stream by making it template
// (anything that could be passed to a `getline` as a first parameter and have
//  an operator >> overloaded that accepts an integer)
template<typename Stream>
Record readFromStream(Stream &str) {
    std::string title;
    std::string shortTitle;
    int number;
    std::string line;
    // Read a line from the stream
    getline(str, line);
    // Make a new stream from that line
    std::stringstream lineStream(line);
    // Read until the comma
    getline(lineStream, title, ',');
    getline(lineStream, shortTitle, ',');
    // The last one is an integer, simply read it
    lineStream >> number;
    return Record(title, shortTitle, number);
}

int main() {
    Record rec = readFromStream(cin);
    // Check for emptiness, if is NOT empty write it
    if (rec.getTitle().size() && rec.getShortTitle().size()) {
        cout << rec.getTitle() << "\n";
        cout << rec.getShortTitle() << "\n";
        cout << rec.getNumber() << "\n";
    }
    return 0;
}

【讨论】:

  • 即使我的数组是 char 类型,我也可以这样做吗?
  • 如果要检查你的char数组是否为空,可以使用C风格的&lt;string.h&gt;,例如可以使用strlen,如果字符串为空则返回0.
猜你喜欢
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
相关资源
最近更新 更多