Need help inputting info from text file to structure using recursion?
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 开发者_StackOverflow中文版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.
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.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream ifs("test.txt");
while (ifs.good()) {
struct foo;
ifs >> foo.name >> foo.a >> foo.b >> foo.c;
}
ifs.close();
return 0;
}
This will allow any whitespace to separate name
, a
, b
, and c
. If you want to be more careful about whitespace (such as allowing spaces in the names, you can either use peek()
to check for new lines or switch to something like fscanf
.
It looks like you want to define a class Person
:
class Person {
std::string name_;
int numbers[3]; // Is this always 3 ?
public:
Person(std::istream& input)
{
std::getline(input, name_); // First line is name.
input >> numbers[0] >> numbers[1] >> numbers[2];
std::ignore(INT_MAX, '\n'); // Eat newline.
// Can you 100% rely on the input being correct?
// If not, you'll need to throw an exception: if (input.fail()) throw ...
}
std::string const& name() const { return name_; }
int a() const { return numbers[0]; }
int b() const { return numbers[1]; }
int c() const { return numbers[2]; }
};
With this class, you can construct Persons from an IOstream until you hit EOF.
精彩评论