stuck at beginning of task

I've been having problems displaying 36 numbers in a .txt file in 5 rows as a part of a 2d array.

my "scores.txt" file contains

 
67 64 93 81 92 98 13 75 89 81 56 88 99 71 80 97 58 78 74 84 21 64 72 69 78 87 84 72 96 83 68 62 88 90 23 75


I am supposed to put these variables into a vector and do some calculations with them. should not have any problems with the calculations, I've just been stuck trying 100 different ways to output it in rows & columns.

this is my most recent code. the only error i get is:

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

int main()
{
	const int MAX = 100;
	string scoresheet[5][7];
	ifstream infile("scores.txt");
	
	while (!infile.eof()){
		int x;
		int y;
		infile >> scoresheet[x][y];
		for (x = 0, x < 5; x++;)
		{
			for (y = 0, y < 7; y++;)
			{
				cout << scoresheet[x][y] << endl;
			}
		}
          }
	return 0;
}
Last edited on
while (!infile.eof()){ you should do while(infile >> temp) also..
1
2
3
		int x;
		int y;
		infile >> scoresheet[x][y];
x and y are undefined the first time around(well not first time around but each time the while loop iterates). You are also missing a closing brace to your while statement. Should be on line 23.
Last edited on
if someone could explain to me what while (infile >> temp) would do to help initialize the variables or why they aren't able to be initiated in that circumstance, it would be greatly appreciated.
This code has no compiler errors, but also nothing is displayed. Not sure what I am missing.

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

int main()
{
	int count = 0;
	string scoresheet[5][7];
	ifstream infile("scores.txt");
	int temp;
	int x, y;
	while (!infile.eof()){
		

	
		while (infile >> temp){
			
		
			
			for (x = 0, x < 5; x++;)
			{
				for (y = 0, y < 7; y++;)
				{
					infile >> scoresheet[x][y];
					
				}
			}
			for (x = 0, x < 5; x++;)
			{
				for (y = 0, y < 7; y++;)
				{
					cout << scoresheet[x][y] << endl;
					
				}
			}
		}
	}
	return 0;
}
line 13 is useless in this case. Same with line 17. Since you are reading in using for loops on line 25 and apparently it is 100% guaranteed to be a 5x7 array.

Why do you have lines 29-36? Can't you just move line 33 to line 26?


So final would be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int count = 0;
	string scoresheet[5][7];
	ifstream infile("scores.txt");
	int x, y;
	for (x = 0, x < 5; x++;)
	{
		for (y = 0, y < 7; y++;)
		{
			infile >> scoresheet[x][y];
			cout << scoresheet[x][y] << ' ';		
		}
		cout << endl;
	}
	return 0;
}
That method also shows nothing. I changed the amount of numbers in my text file to 35 to see if that's what was causing the problem, but it did not resolve the issue.
Are you sure it's opening the file then?
Add this to your code after you make the infile.
1
2
3
4
5
if (!infile.is_open())
{
   cout << "ERROR: Cannot open file...\n";
    return -1;
}

Also, since you're only dealing with ints, you might want to make scoresheet an array of ints instead of strings.

If it's not opening the file, it might be because the IDE you're using (if any) is trying to get resources from the directory of the project file and not where you have the executable, but that is just a guess.
Last edited on
I'm using Visual Studio 2013. The file reads fine, and this seems like a clear cut command. Going to use another compiler and see what happens.
Last edited on
Just realized that your for loop syntax is wrong. VS 2013 should work fine :)

You have for (x = 0, x < 5; x++;)
It should be

1
2
3
	for (x = 0; x < 5; x++)
	{
		for (y = 0; y < 7; y++)
Last edited on
This is the closest I've gotten to succeeding

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

#include<iostream>
#include<fstream>

using namespace std;

int input(istream& in = cin)
{
	int x;
	in >> x;
	return x;
}

int main()
{
	int board[5][7];
	int scores;
	ifstream fin;
	fin.open("scores.txt");
	bool isSuitable = false;
	while (fin >> scores)
	{
		for (int i = 0; i < 5; i++)    //This loops on the rows.
		{
			for (int j = 0; j < 7; j++) //This loops on the columns
			{
				board[i][j] = scores; //you can also connect to the file
				//and place the name of your ifstream in the input after opening the file will
				//let you read from the file.

			}
		}
	}


	for (int i = 0; i<5; i++)    //This loops on the rows.
	{
		for (int j = 0; j<7; j++) //This loops on the columns
		{
			if (board[i][j] >= 1 && board[i][j] <= 7)
				isSuitable = true;
			else
			{
				isSuitable = false;
				break;
			}
		}
		if (isSuitable == false)
			break;
	}



	for (int i = 0; i<5; i++)    //This loops on the rows.
	{
		for (int j = 0; j<7; j++) //This loops on the columns
		{
			cout << board[i][j] << "  ";
		}
		cout << endl;
	}


	if (isSuitable)
		cout << "The Numbers are Suitable" << endl;
	else
		cout << "The Numbers are NOT suitable" << endl;
	return 0;
} 


@Ganaldo, I've used that and still nothing shows up on the screen.

The results of the code linked

1
2
3
4
5
6
7
8
9

 75  75  75  75  75  75  75
75  75  75  75  75  75  75
75  75  75  75  75  75  75
75  75  75  75  75  75  75
75  75  75  75  75  75  75
The Numbers are NOT suitable
Press any key to continue . . .
Think about what's happening,

you have
1
2
3
4
5
6
	while (fin >> scores)
	{
		//i for loop
                        //j for loop
				board[i][j] = scores;
	}

score's value is changing each iteration in the outer while loop, but you assign scores to every element in the board array. At the end of your while loop, every element in board will have the last number in your text file, which happens to be 75. Hope that makes sense.

As giblit said, a while loop in this case is pointless if you already know beforehand how many elements are in the text file.
Try to apply giblit's code:
1
2
3
4
5
6
7
8
9
10
11
12
13
        ...
	ifstream fin;
	fin.open("scores.txt");
	bool isSuitable = false;
        ...
	for (int i = 0, i < 5; i++)
	{
		for (int j = 0, j < 7; j++)
		{
			fin >> board[i][j];	
		}
	}
        ...
Last edited on
Sorry guys, I feel like a huge n00b. This will be my last attempt.
i've tried to make this as simple as possible.


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>
#include <vector>
using namespace std;
int main()
{
	fstream fin;
	fin.open("scores.txt");
	if (! fin)
	{
		cout << "file not found" << endl;
	}
	int box[6][6];
	
	int count = 0;
	for(int x = 0; x < 6; x++)
	{
		for (int y = 0; y < 6; y++)
		{
			fin >> box[x][y];
			
			
		}
		
	}
	for(int x = 0; x < 6; x++)
	{
		for (int y = 0; y < 6; y++)
		{
			
			cout << box[x][y] << endl;
			
		}
		cout << endl;
	}
	return 0;
}


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
67
64
93
81
92
98

13
75
89
81
56
88

99
71
80
97
58
78

74
84
21
64
72
69

78
87
84
72
96
83

68
62
88
90
23
75

Press any key to continue . . .


SOO CLOSE. but it goes straight downwards.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
	for(int x = 0; x < 6; x++)
	{
		for (int y = 0; y < 6; y++)
		{
			
			cout << box[x][y] << endl;
			
		}
		cout << endl;
	}
	return 0;


>>>>>

1
2
3
4
5
6
7
8
9
10
11
	for(int x = 0; x < 6; x++)
	{
		for (int y = 0; y < 6; y++)
		{
			
			cout << box[x][y] << " ";
			
		}
		cout << endl;
	}
	return 0;
aaaaand I can call it a night. thank you!
Last edited on
endl is used for printing new line.
It's obviously. Just changed it to space.
Topic archived. No new replies allowed.