Solved

Run time error with fstream application

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
The line on IDE 4 97
Recommendation vb6 to vb.net or others 14 201
gdb doesn't stop on breakpoint 2 112
How do i run a c++ file? 15 51
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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

713 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