Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于txt文件的动态变量_C++_Pointers_Range - Fatal编程技术网

C++ 基于txt文件的动态变量

C++ 基于txt文件的动态变量,c++,pointers,range,C++,Pointers,Range,我必须创建一个学校图书馆作为OOP作业。我发现很难理解,我的问题是: int RANGE = total_books; total_books应表示文本文件中的当前图书。 格式使它可以阅读3部分信息(标题、作者、体裁)。我如何在函数之间指出这一点 我想加载程序并读取文件,以查看当前有多少本书(假设有7本书,所以变量应该是7*3=21)。然后,当用户查看该文件时,它将显示7本书 目前它是静态的:我把它设置为21。如果我再加一本书,它只会读前7本书。如果我将其设置为24,并且有7本书(不是需要的8

我必须创建一个学校图书馆作为OOP作业。我发现很难理解,我的问题是:

int RANGE = total_books;
total_books
应表示文本文件中的当前图书。 格式使它可以阅读3部分信息(标题、作者、体裁)。我如何在函数之间指出这一点

我想加载程序并读取文件,以查看当前有多少本书(假设有7本书,所以变量应该是
7*3=21
)。然后,当用户查看该文件时,它将显示7本书

目前它是静态的:我把它设置为21。如果我再加一本书,它只会读前7本书。如果我将其设置为24,并且有7本书(不是需要的8本),它将崩溃。我试着在这些论坛和网上的其他地方寻找,得到了“C++编程简易步骤”这本书,在那里我得到了这些格式化代码,但它不是很有用

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "fstream"
#include "iostream"
using namespace std;

unsigned int number_of_books = 0; //this is based on the number of books *3
int total_books = number_of_books * 3; //*3 to read 1 more book

class Book
{
  private: // properties
    char Title[16];
    char Author[16];
    char Genre[16];
  public: // methods
    int iDetailsGet(void); 
    int iDetailsShow(void);
    int iRecordWrite(void);             
};

int Book::iDetailsGet(void)
{
  // prompt for the data
  fflush(stdout);
  puts("\n \t !USE_UNDERSCORE_FOR_SPACE!");
  puts("\n \t Please enter the Book name: ");
  fflush(stdin);
  scanf("%s", Title, 16);

  fflush(stdout);
  puts("\n \t Please enter the Author: ");
  fflush(stdin);
  scanf("%s", Author, 16);

  fflush(stdout);
  puts("\n \t Please enter the Genre: ");
  fflush(stdin);
  scanf("%s", Genre, 16);

  // Get total number of lines(books)
  FILE *infile = fopen("books.txt", "r");
  int ch;
  while (EOF != (ch = getc(infile)))
    if ('\n' == ch)
      ++number_of_books; // read from variable above but static.
    printf("%u\n", number_of_books);

  //return to menu
  int main();
} // end method definition

int Book::iDetailsShow()
{
  system("CLS");
  int RANGE = total_books; // should be dynamically read on start up
  string tab[RANGE];
  int i = 0, j = 0;
  ifstream reader("books.txt");
  if(!reader)
  {
    cout << "Error Opening input file" << endl;
    return -1;
  }

  while(!reader.eof())
  {
    if((i + 1) % 3 == 0) // the 3 read title,author,genre then adds new line
      getline(reader, tab[i++], '\n');
    else
      getline(reader, tab[i++], '\t');
  }

  reader.close();
  i = 0;

  while (i < RANGE)
  {
    cout << endl << "Record Number: " << ++j << endl;
    cout << "Title: " << tab[i++] << endl;
    cout << "Author: " << tab[i++] << endl;
    cout << "Genre: " << tab[i++] << endl;
  }
  int main();
} // end method definition

// code for the method: iRecordWrite(void)
int Book::iRecordWrite(void)
{
  ofstream NewBook("books.txt", ios::app);
  if (!NewBook)
  {
    printf("Error Recording Book");
    return -1;
  }
  NewBook << "    " << Title << "     " << Author << "    " << Genre << endl;
  NewBook.close();
  int main();
} // end of method deinition
#包括“stdio.h”
#包括“malloc.h”
#包括“stdlib.h”
#包括“string.h”
#包括“conio.h”
#包括“fstream”
#包括“iostream”
使用名称空间std;
书籍的无符号整数=0//这是基于书的数量*3
int total_books=_books的数量*3//*3.再读一本书
课堂用书
{
私人物业
字符标题[16];
char作者[16];
字符类型[16];
公共方法
int iDetailsGet(无效);
int IDETAILSSSHOW(无效);
int iRecordWrite(无效);
};
int Book::iDetailsGet(void)
{
//提示输入数据
fflush(stdout);
puts(“\n\t!使用下划线表示空格!”);
puts(“\n\t请输入图书名称:”);
fflush(stdin);
scanf(“%s”,标题,16);
fflush(stdout);
puts(“\n\t请输入作者:”;
fflush(stdin);
scanf(“%s”,作者,16岁);
fflush(stdout);
puts(“\n\t请输入流派:”);
fflush(stdin);
scanf(“%s”,体裁,16);
//获取行(书籍)的总数
文件*infle=fopen(“books.txt”、“r”);
int-ch;
而(EOF!=(ch=getc(infle)))
如果('\n'==ch)
++书籍的数量;//从上面的变量读取,但为静态。
printf(“%u\n”,书的数量);
//返回菜单
int main();
}//结束方法定义
int Book::iDetailsShow()
{
系统(“CLS”);
int RANGE=total_books;//应在启动时动态读取
字符串选项卡[范围];
int i=0,j=0;
ifstream阅读器(“books.txt”);
如果(!读卡器)
{

cout当声明时,您可能应该在实际阅读
number\u of \u books
后设置它,而不是根据
number\u of \u books
初始化
total\u books
。变量,无论是全局变量还是范围变量,都不会动态更新自身。因此,您可以有如下情况:

int number_of_books = 0;

void read_number_of_books() {
    // parse input file for value
    total_books = number_of_books * 3;
}

这就是你要找的吗?< /p>你不应该从程序中的任何函数调用<代码>主代码>代码。粘贴的代码是C和C++的非常糟糕的混合(以及代码< int int()/<代码的正向声明)?您试图理解的问题是动态分配数组。也许您可以使用

std::vector
来简化您的生活。另请参见:此代码中存在许多问题。我建议您先尝试一些更简单的方法。编写一个程序,读取文件并逐行打印到屏幕上。一旦工作正常,请编写一个程序,按字段(标题,作者,类型)读取文件;有不止一种方法来做这件事,所以请省下一些悲伤,使用C++。然后把所有的东西放在一边,学习动态分配的数组。然后加入这两段代码,然后以小的步骤进行。