Solved

Run time error with fstream application

Posted on 2004-09-11
4
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

738 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