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:
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.
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.
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?
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.
#include<iostream>
#include<fstream>
usingnamespace 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 . . .
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];
}
}
...