0

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.

Pete
  • 10,651
  • 9
  • 52
  • 74
vamoose
  • 5
  • 2
  • If you are at a loss of where to begin then I might suggest that you first figure out how to read the data from the text file. Once you have that you can create a class or struct that will hold the name and possibly an array of whatever those values are supposed to be if they are even related. Do you have some code to post? – Pete Apr 28 '11 at 06:21
  • 1
    Using recursion is probably not the best way to do this, unless it's an homework assignment in which your teacher requires you use recursion. – Seth Carnegie Apr 28 '11 at 06:26
  • For beginners, I'd suggest to put the name in a `std::string`, and the numbers in a `std::vector`. As a beginner, you don't want to care about memory allocations. A std::string will take care of memory management for you. When you say `std::string name = "Sally";`, it will allocate those 5 bytes of memory. – MSalters Apr 28 '11 at 12:36
  • "Recursion"? Not "Iteration"? "`struct`"? Not "`class`"? "`char`" arrays to store strings in C++ are rarely the best choice. – johnsyweb Apr 28 '11 at 14:17
  • Sorry, I was on a different computer when I wrote out that code down below. But yeah, we need to use recursion and structs on this program. I think we're able to string the names, if that makes it earlier. – vamoose Apr 28 '11 at 15:06

2 Answers2

1

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.

Community
  • 1
  • 1
Matt
  • 2,139
  • 1
  • 16
  • 20
0

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350