Solved

Run time error with fstream application

Posted on 2004-09-11
4
314 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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