> ... but getline can't be used with integers ...
it is easier to process each element in your image as a char rather than an int. eg.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cassert>
#include <iterator>
using namespace std;
struct replace_chars
{
string operator() ( const string& str ) const
{
string result = str ;
for( size_t i=0 ; i<result.size() ; ++i )
{
switch( result[i] )
{
case '0' : result[i] = ' ' ; break ;
case '1' : result[i] = '-' ; break ;
case '2' : result[i] = '=' ; break ;
case '3' : result[i] = 'O' ; break ;
// etc
}
}
return result ;
}
};
int main()
{
string filename ;
getline( cin, filename ) ;
ifstream file( filename.c_str() ) ;
size_t rows, cols ;
file >> rows >> cols >> ws ; // skip over white spaces
assert( rows>0 && cols>0 ) ;
vector< string > image ;
const string digits = "0123456789" ;
string line ;
while( getline( file, line ) && ( image.size() < rows ) )
{
assert( line.size() == cols ) ;
assert( line.find_first_not_of(digits) == string::npos ) ;
image.push_back( line ) ;
}
assert( image.size() == rows ) ;
transform( image.begin(), image.end(),
ostream_iterator<string>(cout,"\n"),
replace_chars() ) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
You can use >> operator to get the row/column values as ints, then read the data as individual chars
inFile >> rows >> columns;
infile.ignore( 10, '\n'); //ignores up to 10 characters, or until newline
//or
infile.ws( ); //eats up the whitespace
Then begin to read your data ( as characters, as vijayan suggests)
inFile >> image[r][c]; //inside a loop that correctly corresponds to the row and column values you read in.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
>> int image [ROW_SIZE][COL_SIZE];
OK. You want to display the image as characters ... do you need to keep it in memory? If you do you can either change the type of the image to a character array. It'll read in all the numbers as single characters. Then, once it's all read in, you can convert a '0' to a '-' or whatever, hece using the same variable.
Alternatively you could just create a second array of the same size...
It might be worth dynamically allocating enough memory for the image at runtime ... You familiar with new/delete?
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
for (k =0; k < rows; k++)
for (m =0; m < cols; m++)
cout<< image[k][m];
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Sorry! My bad:
for (k =0; k < rows; k++) {
for (m =0; m < cols; m++)
cout<< image[k][m];
cout<< "\n";
}
What I posted earlier just throws everything on one line but this should put a new like after every row. stupid mistake of mine. Sorry.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>I read up on switches, and it says they're used for strings, not chars
Quite the contrary. A switch doesn't work with strings, but because char is an integral type, it works just fine as a switch case.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>> decision == "B" || "b"
That isn't what you think... what you want is:
decision == "b" || decision == "B" or you could do some kind of toupper() usage. That applies to all the comparisons too.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57