需要帮助使用递归将信息从文本文件输入到结构中吗?
我是 C++ 和一般编程的新手,所以请耐心等待我尝试解释自己。
我需要将信息从 .txt 文件输入到结构中。文本文件中的信息如下:
Joe
11 12 13
Sally
10 11 12
...
该结构几乎需要包含名称、a(在情况 1, 11 中)、b(在情况 1, 12 中)和c(在情况 1、13 中)。我希望以递归方式执行此操作,以便它遍历每个名称以及 a、b 和 c。我真的不知道从哪里开始,我真的只是在寻找一些指导。
我在想也许可以将名称放入一个 2D 字符数组中,并将 a、b 和 c 放入另一个 3D 字符数组中?但我不太确定如何做到这一点,或者这样做的目的是什么。
感谢您的任何帮助!
好的,这就是我所得到的。虽然还处于非常早期的阶段,但已经很重要了。
#include<iostream>
#include<fstream>
using namespace std;
const int max_names=100, a=100, b=100, c=100;
char names[max_names];
int num1[a];
int num2[b];
int num3[c];
int main()
{
ifstream inFile;
inFile.open("data.txt");
while(!inFile.eof())
{
for(int i=0; i<max_names; i++)
{
inFile>>names[i]>>num1[i]>>num2[i]>>num3[i];
}
}
return 0;
}
struct Person
{
char names[max_names];
int num1[a];
int num2[b];
int num3[c];
}
编辑:虽然我不想使用递归/结构,但我必须在课堂上使用。另外,在进一步查看我应该做什么后,我需要创建一个结构数组。这很难做到吗?我现在正在编写一些代码,但我可能会完全停止工作。
我需要使用结构标识符。就像“struct Person”
编辑2:是的,递归,是结构,没有迭代,没有类。它必须使用递归和结构。
I'm a total newbie to C++ and programming in general, so please bear with me while I try and explain myself.
I need to input information into a structure from a .txt file. The information in the text file is as so:
Joe
11 12 13
Sally
10 11 12
...
Pretty much the structure needs to contain the name, a (in case 1, 11), b(in case 1, 12), and c(in case 1, 13). I'm looking to do this recursively, so that it goes through every name and a, b, and c. I'm really at a loss for where to begin, and I'm really just looking for some guidance.
I was thinking maybe putting the names into one 2D char array and a, b, and c into another 3D char array? But I'm not really sure how to do that, or what purpose that would have.
Thanks for any and all help!
Okay, so here's what I've got. It's at it's very early stages, but it's something.
#include<iostream>
#include<fstream>
using namespace std;
const int max_names=100, a=100, b=100, c=100;
char names[max_names];
int num1[a];
int num2[b];
int num3[c];
int main()
{
ifstream inFile;
inFile.open("data.txt");
while(!inFile.eof())
{
for(int i=0; i<max_names; i++)
{
inFile>>names[i]>>num1[i]>>num2[i]>>num3[i];
}
}
return 0;
}
struct Person
{
char names[max_names];
int num1[a];
int num2[b];
int num3[c];
}
EDIT: While I'd like to not use recursion/structs, I have to for class. Also, upon further looking at what I'm supposed to do, I need to create an array of structs. Is this hard to do? I'm working on what I think is some code right now, but I'm probably going to be totally off.
I need to use the struct identifier. Like "struct Person"
EDIT 2: Yes, recursion, yes structs, no iteration, no class. It has to use recursion and struct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑使用
ifstream
来在基本循环中从文件中读取。我认为递归不是完成这项工作的正确工具。这将允许任何空格分隔
name
、a
、b
和c
。如果您想更加小心空格(例如名称中允许空格),您可以使用peek()
检查新行或切换到类似fscanf
。I would look into using an
ifstream
to read from the file in a basic loop. I think that recursion is not the correct tool for this job.This will allow any whitespace to separate
name
,a
,b
, andc
. If you want to be more careful about whitespace (such as allowing spaces in the names, you can either usepeek()
to check for new lines or switch to something likefscanf
.看起来您想要定义一个
Person
类:使用这个类,您可以从 IOstream 构造 Person,直到遇到 EOF。
It looks like you want to define a
class Person
:With this class, you can construct Persons from an IOstream until you hit EOF.