开发者

Making an array of pointers to structs or objects in C++

So I'm basically just trying to take in some file input, and then take that data and put it into several structs. The only issue I'm having is with the naming of the pointers to the structs. The struct's themselves are supposed to represent students and I wanted to set each pointer as one of their names rather than an arbitrary variable. I tried to do this in a way that I'm assuming is syntactically wrong for it didn't work. In the code below, I increment the for loop with the temp array because each 4th position is a new student. Any ideas on how I could go about this?

#include<iostream>
#include<iomanip>
#include"student.h"
#include"creditcard.h"
#include<fstream>
using namespace std;

int main ()
{
    string creditcards[20];
    int i;
    int x;
    int amount;
    string temp[20];
    ifstream infile;
    string filename;
    int count;
    int numstudents;
    string newstring="";
    string pointers[20];

    cout<<"enter the file name of which you've stored your"<<endl
        <<"credit card infomation"<<endl;

    getline(cin,filename,'\n');
    infile.open(filename.c_str());

    count=0;
    getline(infile,temp[count],'\n');
    while(! infile.eof())
    {
开发者_JAVA百科        count++;
        getline(infile,temp[count],'\n');          

        numstudents= (count/4);
        if(numstudents < 1 || count%4 != 0)
        {
            cout<<"incorrect data file"<<endl;
        }
    }

    cout<<numstudents<<endl;

    for(i=0,x=0; i<numstudents;i++,x+4)
    {
        student *temp[x];
        temp[x] = new student;
        pointers[i] = temp[x];
    }

    for(i=0;i<numstudents;i+4)
    {
        cout<<temp[i]<<endl;
    }

    return 0;
}


Ok, let's start from the top.

Your code was (before I reformatted it) a mess. Messy code is harder to read and more likely to have bugs.

You have 3 arrays, each containing 20 strings. Why do you need so many?

One of them is named temp; having to use that as a variable name is a good indicator that you're mishandling data somewhere.

You're declaring int count relatively early on, then initializing it to 0 later. While not necessarily a bad thing, that's not the best method (do both at once, when needed).

You can declare local variables more than one in a line, but you don't need to declare them all at the top of the function. That's not necessary in C++.

int main ()
{
    string creditcards[20];
    int i = 0, x = 0, amount = 0;

(legal, but might not be needed)

It's typically better to declare and initialize a variable at the same time, just before you need it:

int count = 0;

getline(infile, temp[count], '\n');

I remember seeing that reading until you hit eof isn't recommended, although I'm not entirely sure on that. You may want to change this:

while ( !infile.eof() )
{

Now, the first actual mistake I see here is that you read a line, increment count, then read another line before acting. Is that intentional, and if so, why is it necessary? Doing the getline and increment inside the loop would be more readable and potentially more reliable.

    count++;
    getline(infile, temp[count], '\n');          

This line is a bug, I think:

 for(i=0,x=0; i<numstudents;i++,x+4)

The last section does i++, x+4. It does not change x.

The next loop after that handles i in the same way this loop uses x, so you can probably combine those two.

Now, on top of all that, massive temp arrays are not the solution to this problem (or any other that I can think of).

To store this kind of data, you'll want to look into a std::map<std::string, student*> or std::vector<student*>. The vector will allow you to push the new student struct to the back when necessary, and the map will allow you to key them based on name and retrieve that later, something like so:

typdef map<string, student*> studentmap;
studentmap students;

studentmap::iterator iter = students.find("Bob");
if ( iter != students.end() )
{
    student * bob = iter->second;
    // Work with data
}

It's a much better way of handling this, and will take a lot of the guess work out of what you're doing now.


If you want to be able to reference the students by name, consider using a map<string, student> or map<string, student*>.

This will allow you to refer to individual students via students["Jack"] or students["Jill"].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜