C++动态分配数组的方式让我不知所措
C++ Dynamically Allocated Array-Way in over my head
所以我要在一个文本文件中读取一个作业,该文件包含四条关于一个人的信息,这些信息用逗号分隔。我正在尝试读取名为"lab8.txt"的文件,分解信息,然后允许用户查看信息以及修改信息。目前,我专注于从文本文件中获取信息,并能够向用户显示这些信息。我在整个代码中有几个错误,希望有人能帮我纠正这些错误,并告诉我我做错了什么。感谢您的任何帮助。文本文件如下所示。很抱歉,我知道这是一个很大的问题,但作业是由我的教授以外的人创建的,我所在的班级比做这项作业的教授班落后得多。
M123456789,Smith,Joe,1978年12月30日,M2345689,Jones,Marc,1980年1月28日,M000022,巴赫,约翰,1685/03/31,M000024,莫扎特,沃尔夫冈,1791/01/27,M000023,Handel,George,1759/04/14,M222222,Geisel,Theodor,1904/03/02,M000044,Sousa,Philip,1854/11/06,M000046,The Frog,Kermit,1955/05/09,M000049,小猪,小姐,1974/10/13,M000049,M000043,Henson,James,1936/09/24,
我的代码包括三个部分。我所有函数定义的主函数、头文件和cpp文件。我将按顺序列出它们。
//主要功能
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include <classname.h>
using namespace std;
int main()
{
File var1;
var1.MenuEditor();
}
#ifndef CLASSNAME_H_INCLUDED
#define CLASSNAME_H_INCLUDED
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct StudentData
{
string StudentId;
string LastName;
string FirstName;
string BirthDate;
};
//头文件
class File
{
public:
File();
File(string);
bool ReadRecordsFromFile(string);
void MenuEditor();
void SearchFile (string);
private:
int number_records;
void BreakUp (string);
void SortName (StudentData, string);
StudentData allocate(string);
void AddRecord(string);
void DeleteRecord(string);
void ModifyRecord(string);
bool ReadRecordsFromFile(string fileName);
void WriteRecordsToFile(string fileName);
};
#endif // CLASSNAME_H_INCLUDED
//带有函数定义的CPP文件
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include "classname.h"
using namespace std;
// This method returns a true if you can open the file or a false if you can
// not
// You will need to add calls to your methods to allocate the array and
// "breakup" the input string.
File::File(string fileName)
{
}
bool File::ReadRecordsFromFile(string fileName)
{
string input_line;
ifstream infile(fileName.c_str()); // This might be useful for you.
if(!infile)
{
return false;
}
Records=new StudentData[100];
// number_of_records or some other variable to store current record count
number_of_records = 0;
while(getline(infile, input_line))
{
// CHECK to see if you need to allocate or expand dynamic array here
// - Use method defined in class to do the allocation
// Call Method to break up data stored in input_line
// and than store new data into dynamic array
// - See Files++ code sample on blackboard
string temp,MNum,
int Current_comma= input_line.find(",");
Records[number_of_records].StudentID = input_line.substr(0, Current_comma);
temp= input_line.substr(Current_comma+1);
cout << [number_of_records].StudentID <<endl;
number_of_records = number_of_records + 1;
}
return true;
}
// This method returns a true if you can open the file for writing and
// false if not
bool Lab8::WriteRecordsToFile(string fileName)
{
string output_line;
ofstream outfile(fileName.c_str());
if(!outfile)
{
return false;
}
// number_of_records or some other variable to store current record count class
for (int rec_count = 0; rec_count < number_of_records; rec_count++)
{
// CREATE a seperate output line out of your data or create it inline
fileobject << output_line << endl;
number_of_records = number_of_records + 1;
}
outfile.close();
return true;
}
File::StudentData allocate(string);
{
}
void File::AddRecord(string fileName)
{
}
void File::DeleteRecord(string fileName)
{
}
void File::ModifyRecord(string fileName)
{
}
void File::SortName(string fileName)
{
}
void File::SearchFile(string fileName)
{
}
void File::MenuEditor()
{
ReadRecordsFromFile("lab8.txt")
cout << "Press 1 to add file." <<endl;
cout << "Press 2 to delete file." <<endl;
cout << "Press 3 to modify file." <<endl;
cout << "Press 4 to sort file." <<endl;
cout << "Press 5 to search file." <<endl ;
getline(cin, int menuchoice);
if (menuchoice == 1)
{
AddRecord();
}
else if(menuchoice == 2)
{
DeleteRecord();
}
else if(menuchoice == 3)
{
ModifyRecord();
}
else if(menuchoice == 4)
{
SearchFile();
}
else if(menuchoice == 5)
{
SortName();
}
else
{
cout << "You done messed up. Re run the program" << endl;
}
}
#include <fstream>
class File
{
public:
File();
*****File(string);******** change to File(&ifstream) <--- do this when passing a text file to a function.
bool ReadRecordsFromFile(string);
void MenuEditor();
void SearchFile (string);
private:
int number_records;
void BreakUp (string);
void SortName (StudentData, string);
StudentData allocate(string);
void AddRecord(string);
void DeleteRecord(string);
void ModifyRecord(string);
bool ReadRecordsFromFile(string fileName);
void WriteRecordsToFile(string fileName);
};
以下是一些提示:
StudentData结构需要在任何使用位置都可见。这应该在你的头文件中-它可能是,但它似乎不是你展示它的方式。
您的File类中有两个ReadRecordsFromFile。你不能让他们两个有相同的论点,但一个是私人的,一个是公共的。取下其中一个。
您需要StudentData*记录;在File类中。这应该在构造函数中初始化为nullptr或零。
您的类定义了number_records,但您使用了一种名为number_of_records的东西。你需要始终如一。此外,这应该在构造函数中初始化为零。
StudentId中的资本化不一致。您确实需要在变量名称上保持精确和一致。
ReadRecordsFromFile中的cout输出并没有说明您正在编写的变量。大概你指的是Records
WriteRecordsToFile的返回值在声明和定义之间不同
文件对象在WriteRecordsToFile中未声明您的意思是什么文件对象?也许是外场??
allocate方法中的File::位于错误的位置,末尾有分号意味着它是一个声明,而不是定义。
SortName的定义与其声明不匹配。
在对getline的调用中,您尝试创建一个变量。在参数内部不能这样做。你需要先定义它。
getline返回的字符串不是整数,因此您需要读入字符串并与字符串进行比较,或者读入整数。
- 如何在c++中为模板函数实例创建快捷方式
- 在c代码之间共享数据的最佳方式
- 在C++中将函数压缩为两种方式
- 以螺旋方式打印矩阵的程序.(工作不好)
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 创建引用向量的优雅方式
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 不同/较旧的处理器运行c++代码的方式是否不同
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 重载方法的方式会在使用临时调用时生成编译器错误
- 在reactor中存储eventHandlers的最佳方式是什么
- 如何以优化的方式同时迭代两个间距不相等的数组
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- 为什么C++有不同的变量初始化方式?
- 在AVX通道中混洗的最佳方式
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- 如何以静态代码分析友好的方式使用 #define 防护?
- C++动态分配数组的方式让我不知所措