1

Lets say there is a competition, and we have x races, and y contestants. Each race, every contestant gets a point between 0 and 10. So there is a file, looking something like this( without the dots, of course):

  • Chet 10 Katie 8 Mark 3 Rachel 5
  • Chet 3 Katie 9 Mark 6 Rachel 8
  • Chet 6 Katie 6 Mark 7 Rachel 0

My problem is that here the strings and integers are alternating, and I dont know how to put them into the multidimensional-array.

Below is how I would do this, if there were only integers in a file. Is it possible to modify this so I can use it with my program?

string x;
int score,row=0,column=0;
int marray[10][10] = {{0}};
string filename;
ifstream fileIN;
cout<<"Type in the name of the file!"<<endl;
cin>> filename;
fileIN.open(filename);
while(fileIN.good())    //reading the data file
{
    while(getline(fileIN, x))
    {
        istringstream stream(x);
        column=0;
        while(stream >> score)
        {
            marray[row][column] = score;
            column++;
        }
        row++;
    }
}

1 Answer 1

2

Array (even multidimensional array) are homogeneous, all the entry must be of the same type. This property let the CPU to compute the address of any entry of the array with a simple displace operation.

In your case you want to store a string and a integer. So you can simple use a struct as entry of your array.

struct Result {
  Result() : point(0)
  {}
  std::string contestantName;
  unsigned int point;
};

Result results[10][10];

the rest of the code is really similar of what you have written.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.