Solved

Run time error with fstream application

Posted on 2004-09-11
4
313 Views
Last Modified: 2013-12-14
Hi.
The requirement for this application is to read in from a txt file the dimensions of a 2D bool vector, followed by the data of the grid. The txt file consists of two integers followed by the vector grid data, where a '*' character represents a true value and a '.' represents a false value. We are using Bloodshed Dev-C++ compiler and the program compiles but when the name of the file is input into the program by the user during run time, an error occurs.

Here is an example input file (input.txt):

6 8
......*.
.**..*.*
.***..*.
..*.....
....*...
...**...

Here is the source code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class grid
{  
   public :
       void readGrid(string filename);
       void printGrid();
             
   private :
       vector < vector <bool> >  v;          
       int rows;                      
       int cols;                    
};    

int main()
{
  grid funGrid;
  string file;                                      
 
  cout << "Enter filename" << endl;
  cin >> file;  // type in the name of the text file e.g "input.txt" (without quoations)
  funGrid.readGrid(file);
 
  funGrid.printGrid();

  system("PAUSE");
  return 0;
}


void grid::readGrid(string filename)
{
  char gridCell;
 
  ifstream inFile;
 
  inFile.open(filename.c_str());
 
  if (!inFile)
    cout << "File does not exist" << endl;
 
  else
  {
    inFile >> rows >> cols;
   
    v.resize(rows, vector <bool> (cols, false));
   
    for (int i=0; i <= rows; i++)
    {  
      for (int j=0; j <= cols; j++)
      {
        inFile >> gridCell;
       
        if (gridCell == '*')
        {
          v[i][j] = true;
        }
        else if (gridCell == '.')
        {
          v[i][j] = false;
        }    
      }
    }    
  }

  inFile.close();  
}

void grid::printGrid()
{
  char cell;
 
  for (int i = 0; i < rows; i++)
  {
    for (int j = 0; j < cols; j++)
    {
      cout << cell;
     
      if (v[i][j] == true)
      {
         cell = '*';
      }
      else if (v[i][j] == false)
      {
         cell = '.';
      }    
    }
   
    cout << endl;
  }
}

0
Comment
Question by:Bonescraper
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12037471
(1) Your run time error is a result of...

       v.resize(rows, vector <bool> (cols, false));

...not resizing (redimensioning?) your array as you hoped. What you've written will resize a 1D array to rows, with new elements given a default value returned by the expression vector <bool> (cols, false), which I am suprised evaluables to a bool (but it does on my system too VC 7.1).

You want a vector of vectors to get a 2D array.

I recommend using typedefs to make it look less complicated.

  typedef vector<bool> BoolVector; // Vector of bool
  typedef vector<BoolVector> BoolVectorVector; // Vector of vector of bool
  BoolVectorVector v;

First resize the Vector of vector of bool for the number of rows.

  v.resize(rows);

Then resize each row's vector of bool to have the right number of columns.

  for (int row = 0;row < rows;++row)
      v[row].resize(columns);

(2) Also take a look at:

      cout << cell;

cell is uninitialised. You want to do this after the assignment.
0
 

Author Comment

by:Bonescraper
ID: 12037627
Thanks for the reply. I altered the code as follows but the error is still occurring. Any thoughts?

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

typedef vector<bool> BoolVector; // Vector of bool
typedef vector<BoolVector> BoolVectorVector; // Vector of vector of bool

class grid
{
   public :
       void readGrid(string filename);
       void printGrid();
   private :
       BoolVectorVector v;      // the 2-dimensional grid of data
       int rows;                     // the number of rows in the grid data
       int cols;                      // the number of columns in the grid data
};

int main()
{
  grid funGrid;
  string file;                                      
 
  cout << "Enter filename" << endl;
  cin >> file;
  funGrid.readGrid(file);
 
  funGrid.printGrid();

  system("PAUSE");
  return 0;
}


void grid::readGrid(string filename)
{
  char cell;
 
  ifstream inFile;
 
  inFile.open(filename.c_str());
 
  if (!inFile)
    cout << "File does not exist" << endl;
  else
  {
    inFile >> rows >> cols;
   
    //v.resize(rows, vector <bool> (cols, false));
    v.resize(rows);
    for (int row = 0;row < rows;++row)
      v[row].resize(cols);
   
    for (int i=0; i <= rows; i++)
    {  
      for (int j=0; j <= cols; j++)
      {
        inFile >> cell;
       
        if (cell == '*')
        {
          v[i][j] = true;
        }
        else if (cell == '.')
        {
          v[i][j] = false;
        }    
      }
    }    
  }

  inFile.close();  
}

void grid::printGrid()
{
  char cell;
 
  for (int i = 0; i < rows; i++)
  {
    for (int j = 0; j < cols; j++)
    {
      if (v[i][j] == true)
      {
         cell = '*';
      }
      else if (v[i][j] == false)
      {
         cell = '.';
      }
      cout << cell;    
    }
   
    cout << endl;
  }
}

0
 
LVL 17

Accepted Solution

by:
rstaveley earned 65 total points
ID: 12038765
>   for (int i=0; i <= rows; i++)
>    {  
>      for (int j=0; j <= cols; j++)

Change the <= to < in both cases.
0
 

Author Comment

by:Bonescraper
ID: 12040933
Thankyou this resolved the error.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 92
Eclipse MARs unable to start with exit code=13 2 69
Recommendation vb6 to vb.net or others 14 105
object oriented programming comparison 5 52
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now