Skip to main content
gamedev.net
🔒 Locked

cin.getline()

Started by randomDecay Nov 16, 2001 at 8:29 PM 7 replies 450+ views
Original Post
randomDecay
randomDecay
Hey all. I am reading line-by-line from a file using C++ in a console program. Here is what I have on one line that I want to stick in variables: 1, 6 49 1 0 1,6 are the x,y coords 49 is the ASCII value of a character symbol 1 is its visible state 0 is the color here is what I have so far to do this:
  
	inputFile.getline( buffer, BUF_SIZE );
		
			m_nCells = atoi( buffer );
			cout << "Number cells = " << m_nCells << endl;
		
			// Get cell coords, ASCII character, visibility state

			// and color for each cell

			for ( int i = 0; i < m_nCells; i++ )
			{				
				inputFile.getline( buffer, BUF_SIZE );

				coordX = atoi( buffer );

				int index = 0;
				while ( buffer[ index ] != ''\0'' && buffer[ index ] != '','' )
				{
					index++;
				}
				if ( buffer[ index ] = '','' )
				{
					coordY = atoi( &buffer[ ++index ] );
				}
				else
				{
					coordY = 0;
				}

				cout << "x = " << coordX << " " << " y = " << coordY << endl;

				Cell *ps = &cell[ coordX ][ coordY ];
				ps->symbol = atoi( &buffer[ index + 2 ] );
				cout << "Symbol = " << ps->symbol << endl;
				
				ps->visible = true;
				cout << "Visible = " << ps->visible << endl;

				ps->color = atoi( &buffer[ index + 8 ] );
				cout << "Color = " << ps->color << endl;
  
basically it works, but I haven''t figured out a better way or cleaner way to do this...anyone want to help? And I couldn''t figure out how to get a bool value out of the visible state, seeing as how I set up my struct so that I have a variable called bool visible. So for the time being I just made it true.
randomDecay
randomDecay
That sounds good, thanks. But is there a way to do it using C++?
Neophyte
Neophyte
Why not simply
  
int tmp;
inputFile >> coordX >> coordY;
Cell *ps = &cell[coordX][coordY];
inputFile >> ps->symbol;
inputFile >> tmp;
ps->visible = (tmp == 1);


-Neophyte
randomDecay
randomDecay
Does that take into account the stupid comma?
Oluseyi
Oluseyi
"Pure" C++:

#include <cstdlib> // definition for atoi
#include <fstream>
#include <iostream>
#include <string>
//
int main(int argc, char *argv[])
{
// assume ifstream opened and validated
// parse a single line:
string coords;
int x, y, iAscSym, iVisible, iColor;
//
infile >> coords >> iAscSym >> iVisible >> iColor;
int pos = coords.find_first_of(",", 0);
if(pos == string::npos)
{
cout << "Mis-formed coordinate pair" << endl;
return -1;
}
x = atoi((coords.substr(0,pos)).c_str());
y = atoi((coords.substr(pos, coords.size() - pos)).c_str());
...
// use data
return 0;
}
Neophyte
Neophyte
Ah, I didn''t actually notice the comma.
To cope with that you can do:
  
int tmpi;
char tempc;
inputFile >> coordX >> tempc >> coordY;
Cell *ps = &cell[coordX][coordY];
inputFile >> ps->symbol;
inputFile >> tmpi;
ps->visible = (tmpi == 1);


-Neophyte


- Death awaits you all with nasty, big, pointy teeth. -
randomDecay
randomDecay
Thanks to all for the help :D

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.