Solved

Run time error with fstream application

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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

17 Experts available now in Live!

Get 1:1 Help Now