- Jun 13, 2004
- 740
- 0
- 76
Hello everyone,
I've spent a few hours trying to get part of my program working so i can move forward with my project but I've become stuck and I'm not quite sure where I'm going wrong. If you have any advice, I would greatly appreciate it. The following code is using a dummy text file "grid.txt".
While on my laptop just before I got home it was printing the first line with numbers in the 40s, then every line after that was printing what looked to be the max negative double number for each item after that. When I tried to recompile on desktop after getting home it doesn't display anything. I must be missing a minor error somewhere for that problem.
grid.txt shown below
I've spent a few hours trying to get part of my program working so i can move forward with my project but I've become stuck and I'm not quite sure where I'm going wrong. If you have any advice, I would greatly appreciate it. The following code is using a dummy text file "grid.txt".
While on my laptop just before I got home it was printing the first line with numbers in the 40s, then every line after that was printing what looked to be the max negative double number for each item after that. When I tried to recompile on desktop after getting home it doesn't display anything. I must be missing a minor error somewhere for that problem.
grid.txt shown below
Code:
0 0 0 0 0
1 0 0 1 1
0 1 0 1 1
0 1 0 1 0
1 0 1 0 1
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
ifstream blobFile;
blobFile.open ("grid.txt");
int rows = 0;
int coloumns = 0;
string lineBuffer; // buffer to temp store a string
if(blobFile.is_open())
{
while(!blobFile.eof())
{
getline(blobFile,lineBuffer);
rows += 1;
}
coloumns = ((lineBuffer.size()) / 2) + 1; //input contains 0s and 1s separated by spaces. This will give the right number taking away the spaces between the numbers.
/*------------------------------------------------------------------------------------------------
Dynamically allocate the memory for the array based on the dimensions found in the previous search.
-------------------------------------------------------------------------------------------------*/
int **blobGrid;
blobGrid = new int *[rows];
for(int i = 0; i < rows; i++)
blobGrid[i] = new int [coloumns];
/*------------------------------------------------------------------------------------------------
We need to reset the beginning of the seek function for the file to position 0 to go to next check
-------------------------------------------------------------------------------------------------*/
blobFile.seekg(0, ios::beg);
while(!blobFile.eof())
{
int temp = 0;
int temp2 = 0;
string tempLine;
getline(blobFile, tempLine);
/*---------------------------------------------------------
getting each character of string tempLine and storing the
int (0 or 1) if its not a space char.
----------------------------------------------------------*/
for(int i = 0; i<tempLine.length(); ++i)
{
if(i != ' ')
{
blobGrid[temp][temp2] = int(tempLine.at(i));
temp2 += 1;
}
}
temp += 1;
}
/*---------------------------------------------------------
printing the contents of the array
----------------------------------------------------------*/
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < coloumns; j++)
{
cout << blobGrid[i][j] << " ";
}
cout << endl;
}
} //end of blobFile.open()
blobFile.close();
cin.get(); // keep the window open so I can see what was last in console
cin.get();
}
