Reading a txt file to create 2D and 1D arrays

I am asked to read a txt file that looks like

Tom 50 60 70.5
Jerry 80.3 65 91
Mark 75.2 77 92.7
Lucy 100 87.6 93

and then create a 1D array to store the names and a 2D array to store the scores, the program should print the arrays in exactly the format of the table in the txt file.
I tried different codes but the best I did was printing all names and scores character by character vertically. Below is the last version of my code.
Can anyone shed some light on my problem? I am really stressed by this whole C++ thing coz what I have been studying has nothing to do with computer science and this is only the beginning part of the program my assignment asks for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()

{
   char namesArray[50];
   double marksArray[50][4];

   ifstream assignmentmarks;
   assignmentmarks.open("table.txt");

   int i, j = 0;

      while(!assignmentmarks.eof())
      {
		  for(i=0;i<50;i++);
		  {
            assignmentmarks >> namesArray[i];
            cout << namesArray[i] << endl;


			for(j=1;j<4;j++);
			assignmentmarks >> marksArray[i][j];
			cout << marksArray[i][j] << endl;
		  }
      }

    assignmentmarks.close();
    return 0;

}
Last edited on
>>> for(i=0;i<50;i++);

>>> for(j=1;j<4;j++);
assignmentmarks >> marksArray[i][j];
cout << marksArray[i][j] << endl;

a) don't end for "statements" by semicolon
b) you are missing the curly braces for the statement block in the 'j' loop.
It still doesn't work...All I get is infinite numbers popping up...
Is there anything wrong with this approach itself to create a 2D along with a 1D array?
Nope, the approach is fine.

Post the source code again acompanied by the contents of the input file.
input file:

Tom 50 60 70.5
Jerry 80.3 65 91
Mark 75.2 77 92.7
Lucy 100 87.6 93

Actually this is only one sample. To be specific, the question says "Each line of the file contains 4 items separated by a single space. The first one is a string whose length is
less than 10, without any whitespace inside, and only contains one uppercase letter as the first letter. It is the name of a student. Then 3 real numbers represents the student's marks for the first, second and the third assignment, respectively. You may assume that the number of students in the file is no more than 50 and their names are unique.", does this affect the codes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()

{
   char namesArray[50];
   double marksArray[50][4];

   ifstream assignmentmarks;
   assignmentmarks.open("table.txt");

   int i, j;

      while(!assignmentmarks.eof())
      {
		  for(i=0;i<50;i++)
		  {
            assignmentmarks >> namesArray[i];
            cout << namesArray[i]<< endl;
		  }

		  for(i=0;i<50;i++)
          {
			for(j=1;j<4;j++)
            {
			assignmentmarks >> marksArray[i][j];
			cout << marksArray[i][j] << endl;
			}
		  }
      }

    assignmentmarks.close();
    return 0;

}

Last edited on
namesArray should be of type string.

Try this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
	{
	string namesArray[50];
	double marksArray[50][4];

	ifstream assignmentmarks;
	assignmentmarks.open("table.txt");

	int i, j;

		for(i=0;i<50;i++)
			{
			assignmentmarks >> namesArray[i];
			if (assignmentmarks.eof()) 
				break;
			cout << namesArray[i]<< endl;
			for(j=1;j<4;j++)
				{
				assignmentmarks >> marksArray[i][j];
				cout << marksArray[i][j] << endl;
				}
			}
	

	assignmentmarks.close();
	return 0;

	}
Thanks so much for your help!
But when I run it the names and the scores are not in rows and columns like the txt file.
I tried to delete "endl" but it failed. Also, I wonder what " if (assignmentmarks.eof()) break; " means??
oh I kind of fixed it!!
Thanks a lot!!! I am such an idiot in programming....
Turns out that the question is much more complicated than merely reading the file...
input file :
Tom 50 60 70.5
Jerry 80.3 65 91
Mark 75.2 77 92.7
Lucy 100 87.6 93

I have to ask the user to input a number (0 to 3), indicating the number of column in the input file. Then sort the table in the input file in ascending order/alphabetical order of that column, then finally prints the sorted table only. For example, if the user enters 0, then it should print:

Jerry 80.3 65 91
Mark 75.2 77 92.7
Lucy 100 87.6 93
Tom 50 60 70.5

I am really in a mess.....my codes are not getting anywhere....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string namesArray[50];
double marksArray[50][4];
void sorting (string, double);


int main()

{
    string namesArray[50];
	double marksArray[50][4];
    int n, m;

    while (n >= 0)

    {
     cout << "Options:" << endl;
     cout << "1. Sort" << endl;
     cout << "2. Assignment Average" << endl;
     cout << "3. Student Average" << endl;
     cout << "4. Update" << endl;
     cout << "0. Exit" << endl;
     cout << "Select: ";

     cin >> n;

     if (n == 1)

        cout << "Column number: ";
        cin >> m;

        if (m == 0)
        {
            sorting (namesArray[50], marksArray[50][4]);
        }



    return 0;
}


void sorting (string namesArray[], double marksArray[][])

     {
         string temp, tempp;

         ifstream assignmentmarks;
         assignmentmarks.open("table112.txt");

         for (int i = 0;  i < 50; i++)

        {
            assignmentmarks >> namesArray[i];
            if (assignmentmarks.eof())
            break;

            for(j=1;j<4;j++)
				{
				assignmentmarks >> marksArray[i][j];
				}

          if (namesArray[i] > namesArray[i + 1])
          {
            string temp = namesArray[i];
            namesArray[i] = namesArray[i + 1];
            namesArray[i + 1] = temp;
            for (int j = 0; j < 49 - i; j++)
              {
                double tempp = marksArray[i][j];
                marksArray[i][j] = marksArray[i][j + 1];
                marksArray[i][j + 1] = tempp;
              }
          }

          cout << namesArray [i] << "\n" << marksArray[i][j] << endl;

        }

     }


hi
i do not know what do you want?
but i fix your code to work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void sorting (string namesArray[], double marksArray[][4]);




int main()

{
    string namesArray[50];
	double marksArray[50][4];
    int n, m;
	cin >> n;
    while (n >= 0)

    {
     cout << "Options:" << endl;
     cout << "1. Sort" << endl;
     cout << "2. Assignment Average" << endl;
     cout << "3. Student Average" << endl;
     cout << "4. Update" << endl;
     cout << "0. Exit" << endl;
     cout << "Select: ";

     cin >> n;

     if (n == 1)

        cout << "Column number: ";
        cin >> m;

        if (m == 0)
        {
            sorting (namesArray, marksArray);
        }
    }
	cin.get();
	return 0;
}


void sorting (string namesArray[], double marksArray[][4])
     {
         string temp, tempp;
		 int j;
         ifstream assignmentmarks;
         assignmentmarks.open("table112.txt");

         for (int i = 0;  i < 50; i++)

        {
            assignmentmarks >> namesArray[i];
            if (assignmentmarks.eof())
            break;

            for(j=1;j<4;j++)
				{
				assignmentmarks >> marksArray[i][j];
				}

          if (namesArray[i] > namesArray[i + 1])
          {
            string temp = namesArray[i];
            namesArray[i] = namesArray[i + 1];
            namesArray[i + 1] = temp;
            for (int j = 0; j < 49 - i; j++)
              {
                double tempp = marksArray[i][j];
                marksArray[i][j] = marksArray[i][j + 1];
                marksArray[i][j + 1] = tempp;
              }
          }

          cout << namesArray [i] << "\n" << marksArray[i][j] << endl;

        }

     }
Last edited on
Well, your last task is not quite an easy one. Essentially, you have to sort items by the given column, and to make matters harder the columns are of different types.

First, I don't know whether this task is appropriate for your level of knowledge, but it seems to me that it isn't.

Second, you should use std::vector instead of C-style arrays. Otherwise you might be making many bugs. If your instructor/book didn't teach you std::vector befor C-style arrays, then he/it is a bad and outdated instructor/book.

Here is the outline of how the solution should go:
1. you should create a new structure type named 'Person' that can contain data for one line of the input file.
2. Read the input file into std::vector<Person>
3. Create a comparison function
bool Compare(Person a, Person b, int column)
that can compare two persons by the given column
4. Write a sorting function
std::vector<Person> Sort(std::vector<Person> persons, int column)

Last edited on
I really have no idea what you're talking about.....let alone writing it by myself...
I'm pretty sure my instructor has just talked about simple arrays and strings...
but in a sample question I found the following codes. It is written to sort a menu in ascending order of food names, where a[] represents food names and b[] represents the corresponding prices. Only that the food names and prices have all been initialized in arrays before sorting.So I am wondering if I can make use of this if I can find a way to read all the names and scores from the file and store them in arrays just like initializing them?

I am desperate by now....since this part is only one of four tasks this program asks for...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void sort (string a[], double b[])
{
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 9 - i; j++)
    {
        if (a[j] > a[j + 1])
        {
            string temp = a[j];
            a[j] = a[j + 1];
            a[j + 1] = temp;
            double tprice = b[j];
            b[j] = b[j + 1];
            b[j + 1] = tprice;
        }
    }
}
I repeat, your instructor is bad. Quit the course, if you can. Look for better courses or find a good book.

I think that you will have serious problems completing this task, but lets give it a shot. Even I am curious as to how far can you make it.

For starters, answer the questions:
1. Did you learn so far how to use type bool (I assume you didn't)
2. Did you learn so far how to use structures (I assume you didn't)

Is the number of colums constant? I assume it is. Lets say it equals 3. So, define a global constant:

const int colN = 3;

For starters, try to write a function:

int Compare(double dataA[], double dataB[], int col);

...which compares the elements at index 'col' of arrays dataA and dataB, and returns 1 if the selected element of dataA is smaller than the selected element of dataB. Return 0 otherwise.
Last edited on
Sadly, not only am I not allowed to quit the course, it's going to screw my overall GPA if I mess this up...I just want to get through with this so I can spend time on the main disciplines I am studying....But I don't see any hope struggling with this by myself...
Can any one plz just write me some codes??? I really want to do this myself but I'm just not getting anywhere....This is taking me too much time and making me screw up on what I should be studying....C++ is just a mandatory side course for me and I'm not going any deeper into C++ once I get this assignment done....I am so desperate by now... I can even pay for whoever helps me get through with this...
Last edited on
Topic archived. No new replies allowed.