So simple, yet hard for me...Not reading the date from file...

Need help with my program. It is supposed to read the data from the file and display it...so simple and I am struggling to see my problem...

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

//This program is....
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

    const int numrow = 20;
    const int numcol = 2;
    const int size = 20;

void getts(int[][numcol], int);
void printts(int[][numcol], int);

int main()
{
    int testscores[numrow][numcol];
   
    getts(testscores, numcol);
    printts(testscores, numcol);

    return 0;
}

void getts(int testscores[][numcol], int size)
{
    ifstream inFile;

    inFile.open("practicetestscore.dat");

    for (int row=0; row < size; row++)
    {
        for (int col = 0; col < size; col++)
        inFile >> testscores[numrow][numcol]>>testscores[numrow][numcol];
    }

    inFile.close();
}

void printts(int testscores[][numcol], int size)
{
    cout <<"The testscores are:\n";
        for (int row = 0; row < size; row++)
        {
            for (int col = 0; col < size; col++)
            cout << setw(2) <<testscores[numrow][numcol]
            << setw(2) <<testscores[numrow][numcol] <<endl;
        }
       
        return;
}
This statement in getts is totally wrong. You are using the constant used to define the array as an index into the array. You need to be using row and col. Is this just a cut and paste error? Anyway, that is undefined behavior. You are not properly filling the array in the first place. It is wrong elsewhere in the program as well. Use the local variables that are being incremented by the for loops. That is the biggest problem I see. Fix that and then see how it works.

inFile >> testscores[numrow][numcol]>>testscores[numrow][numcol];
Last edited on
Thanks, that was an error, I changed a few since, but it is only showing one of the numbers now but 20 times...

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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

	const int numrow = 20;
	const int numcol = 2;
	const int size = 10;

void getts(int[][numcol], int);
void printts(int[][numcol], int);

int main()
{
	int testscores[numrow][numcol];
	
	getts(testscores, size);
	printts(testscores, size);

	return 0;
}

void getts(int testscores[][numcol], int size)
{
	ifstream inFile;

	inFile.open("practicetestscore.dat");

	for (int row=0; row < size; row++)
	{
		for (int col = 0; col < numcol; col++)
		{
			inFile >> testscores[row][0];
			inFile >> testscores[row][1];
		}
	}

	inFile.close();
}

void printts(int testscores[][numcol], int size)
{
	cout <<"The testscores are:\n";
		for (int row = 0; row < size; row++)
		{
			for (int col = 0; col < numcol; col++)
			{
				cout << setw(2) <<testscores[row][0] 
				<< setw(2) <<testscores[row][1] <<"";
			}
			cout << endl;
		}
Why are you manually specifying 0 and 1 for the column index into the array? What is the point of the embedded for loop then? Everytime operator>> executes the filestream is advancing so you are filling the array incorrectly.
I am not sure how to get the program to read two different columns. I am searching online but have not come across that concept. I am not very familiar with C++, just started learning it. Before I had:

 
testscores[row][0] 


but that wasn't working, so I am trying different things to get it to work. Do you know where I can see an example of code that retrieves info from a file with 20 rows and 2 columns?
Last edited on
I changed it to:

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
void getts(int testscores[][numcol], int size)
{
	ifstream inFile;

	inFile.open("practicetestscore.dat");

	for (int row=0; row < size; row++)
	{
		for (int col = 0; col < numcol; col++)
		{
			inFile >> testscores[row][col];
		}
	}

	inFile.close();
}

void printts(int testscores[][numcol], int size)
{
	cout <<"The testscores are:\n";
		for (int row = 0; row < size; row++)
		{
			for (int col = 0; col < numcol; col++)
			{
				cout << setw(2) <<testscores[row][col]<<"";
			}
			cout << endl;
		}
}


and it showing 20 numbers but repeating so at least I made some progress but still not full there yet.
Last edited on
I made a change to the initial consant size and was able to get 20 numbers to show but only one column, just need to get the second column now.
What does the file look like? Looks like you are getting closer. It should be something like,


25 30
20 40
60 70


and so forth. Each operator>> is called it reads up to the whitespace and stops. You should have a file with two columns of integers. Is that correct?
The file looks like this:

1001 90
1002 88
1003 99
1004 63
1005 56
1006 76
1007 86
1008 71
1009 74
1010 75
1011 83
1012 64
1013 87
1014 92
1015 68
1016 79
1017 63
1018 89
1019 78
1020 75


at this point I am getting the both columns to show but not with the space, you are correct.
Last edited on
Ok, I got it now. I changed the set width. Thank you very much I am going to work on putting it in ascending order now with the following:

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
void selectsort(int testscores[][numcol], int size)
{
	int startscan, lowindex;
		int lowts;
		
		for (startscan = 0; startscan < (size-1); startscan++)
		{
			lowindex = startscan;
			lowts = testscores[startscan][0];

			for (int index = startscan + 1; index < size; index++)
			{
				if (testscores[index][0] < lowts)
				{
					lowts=testscores[index][0];
					lowindex=index;
				}
			}
			testscores[lowindex][0]=testscores[startscan][0];
			testscores[startscan][0]=lowts;

			lowts = testscores[startscan][1];
			testscores[startscan][1] = testscores[lowindex][1];
			testscores[lowindex][1]=lowts;
		}
}


This was in the program but had to fix my errors first before I add sorting function to it.
Last edited on
Topic archived. No new replies allowed.