0

I’m writing this code where I fill a 2 dimensional array with information from a file. Here’s the file:

5 

Franks,Tom 2 3 8 3 6 3 5 

Gates,Bill 8 8 3 0 8 2 0 

Jordan,Michael 9 10 4 7 0 0 0 

Bush,George  5 6 5 6 5 6 5 

Heinke,Lonnie  7 3 8 7 2 5 7

Now the numbers are going in the array: data[50][8].

I also total all the numbers in each line which I have done. I want to add this total to the data array so it looks something like 2 3 8 3 6 5 3 30. How do I do this? Here’s all my code if you wanted to see it:

int main()
{

    ifstream fin;
    char ch;
    int data[50][8];
    string names[50];

    fin.open("empdata.txt");

    int sum = 0;
    int numOfNames;
    fin >> numOfNames;

    for (int i = 0; i < numOfNames; i++) {

        fin >> names[i];

        for (int j = 0; j < 7; j++) {
            fin >> data[i][j];
        }
            }

        for (int i = 0; i < 5; i++)
        {

            for (int j = 0; j < 7; j++)
            {
                sum += data[i][j];

            }
            cout << sum << endl;
            sum = 0;
        }
}

Here's the new code that c650 helped me with. It's not outputting anything now: int main() {

ifstream fin;
char ch;
int data[50][8];
string names[50];

fin.open("empdata.txt");

int sum = 0;
int numOfNames;
fin >> numOfNames;

for (int i = 0; i < numOfNames; i++) {

    fin >> names[i];

    data[i][7] = 0;

    for (int j = 0; j < 7; j++) {
        fin >> data[i][j];
        data[i][7] += data[i][j];
    }
}

for (int i = 0; i < numOfNames; i++)
{
    cout << data[i][7] << endl;

}



system("pause");
return 0;

}

13
  • Please format the file... Commented Nov 1, 2016 at 23:36
  • I just did. Hope that helps Commented Nov 1, 2016 at 23:37
  • I'd recommend you use the STL. A std::vector may really help you out! Commented Nov 1, 2016 at 23:37
  • I have to use arrays Commented Nov 1, 2016 at 23:38
  • Will there always be seven numbers after each name? Commented Nov 1, 2016 at 23:39

2 Answers 2

1

The following seems like what you are trying to do. I will point the flaws in the code you've presented.

This is not the best way to do it, but according to OP there are some constraints, such as not being able to use the STL.

/*  1 */  #include <fstream>
/*  2 */  #include <iostream>
/*  3 */  #include <string>
/*  4 */  
/*  5 */  using namespace std;
/*  6 */  int main()
/*  7 */  {
/*  8 */  
/*  9 */      ifstream fin;
/* 10 */      int data[50][8];
/* 11 */      string names[50];
/* 12 */  
/* 13 */      fin.open("test.txt");
/* 14 */  
/* 15 */      int numOfNames;
/* 16 */      fin >> numOfNames;
/* 17 */  
/* 18 */      for (int i = 0; i < numOfNames; i++) {
/* 19 */  
/* 20 */          fin >> names[i];
/* 21 */  
/* 22 */          data[i][7] = 0; /* use last spot in array for sum, set to 0. */
/* 23 */          for (int j = 0; j < 7; j++) {
/* 24 */              fin >> data[i][j];
/* 25 */              data[i][7] += data[i][j];
/* 26 */          }
/* 27 */      }
/* 28 */  
/* 29 */      for (int i = 0; i < numOfNames; i++)
/* 30 */      {
/* 31 */          cout << data[i][7] << endl; /* add each element to the sum here*/
/* 32 */      }
/* 33 */      return 0;
/* 34 */  }

OP was having a problem where no output was generated. OP, you must make sure that the your input file has the correct name...

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

9 Comments

Ok that seems like it would work but it's not outputting anything now. Here's the code
data[i][7] += data[i][j]; } } for (int i = 0; i < numOfNames; i++) { cout << data[i][7] << endl; } system("pause"); return 0; }
ohhh that didn't past right, hold up I'll just edit my top question
@Ralf: I am afraid that I cannot reproduce your "no output" results. Are you sure your input file is named correctly?
Oh yeah...that was my problem. It works now! Thanks so much for your help :)
|
0

As I've answered in other similar posts: Use a structure to model the record.

struct Record
{
  std::string last_name;
  std::string first_name;
  unsigned int grades[MAXIMUM_GRADES];
};

This allows you to have an array of mixed types:

Record student_info[MAXIMUM_STUDENTS];

To make reading from a file easier, you can overload operator>> inside the structure:

struct Record
{
  //...
  friend std::istream& operator>>(std::istream& input, Record& r);
};

std::istream& operator>>(std::istream& input, Record r)
{
  std::getline(input, r.last_name, ',');
  std::getline(input, r.first_name, ' ');
  for (unsigned int i = 0; i < MAXIMUM_GRADES; ++i)
  {
    input >> r.grades[i];
  }
  return input;
}

This simplifies your input loop:

for (unsigned int i = 0; i < MAXIMUM_STUDENTS; ++i)
{
  my_data_file >> student_info[i];
}

Too bad you can't use std::vector. The std::vector is ideal for reading from files especially when you don't know the size of the file.

2 Comments

this guy still don't about matrix how he could understand the struct ?
@ShahrairNazimReal: Depends on the instruction course and the order of the topics discussed. There is no need to discuss arrays before structures. Structures can be discussed before arrays.

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.