help with istream and making comparisons

April 24, 2011 at 14:23:21
Specs: Windows Vista
Ok, so here' the thing.

I have to input a large amount of data; let's say a checkerboard with pieces on it, into the istream. (Cut and paste, not read from file)

So I input something like this:
- x - x - x - x - x
x - x - x - x- x -
- - - - - - - -
- o - o - o - o - o
o - o - o - o - o

Something like that. (I'm not actually doing checkers but I don't wanna give too much details, then it'd be too easy)

I want to input that data and then be able to read it and use it.

I have a gameboard initialized with a 2 dimensional array: board[7][7] <-- ignore the numbers, I'm not actually doing checkers.

And when I print that, it looks fine.

Now I want to input the above mentioned data, and then start working right away.

So I tried this:


int StringToNumber (char blah[1])
{
if(blah=' ')
return 0;
if(blah='S')
return 1;
if(blah='F')
return 2;
if(blah='.')
return 3;
//stringstream convert(blah); not sure where I was going with this
//T result;
//return convert >> result ? result : 0;
}

void read_input(int board[7][7])
{
cout << "Your move?" << endl;
int x,y=0;
char blah[1];
for(x=0; x<49; x++) //I'm not really doing checkers, but it is a 7x7 with ' ', '.' , 'S', and 'F' chars
{
cin.get(blah,1); //trying to read each char individual, and send it to the above function.
cout << blah; //seeing what it did.
int result = StringToNumber(blah,1);
//cout << result;
}


Basically, it's saying its an invalid conversion from 'char' to 'char*'.

Am I headed in the right direction here, and if so -- how can I make this comparison?

Ive also tried memset and memcmp as well as strcmp. Those aren't working for me either.


See More: help with istream and making comparisons

Report •


#1
April 25, 2011 at 06:19:02
Know what would fix the problem? Replace all references to char blah[1] with char blah. After all, why have an array of one element, when you can just have one element not in an array?
Also, after you make that change, change this line:
cin.get(blah,1); //trying to read each char individual, and send it to the above
to:
cin.get(blah); //trying to read each char individual, and send it to the above

How To Ask Questions The Smart Way

EDIT: VVV Yes, that too! VVV


Report •

#2
April 25, 2011 at 09:10:47
It would also help to use comparisons rather than assignments in the "if" statements! That's a classic C error.

Report •

#3
April 25, 2011 at 11:20:11
Comparisons rather than assignments? Could you elaborate?

Report •

Related Solutions

#4
April 25, 2011 at 11:29:40
Actually you mean ==, versus =. Yeah that's a good idea. It compiled. Dunno if it'll work out in the end; but compiling is a start. Thanks

Report •


Ask Question