Ok i am writing a program to manipulate structures that are similar to linked list. I have a file that has these contents.
4 5
0 0 0
0 1 0.1
0 2 0.2
0 3 0.3
0 4 0.4
1 0 1
1 1 1.1
1 2 1.2
1 3 1.3
1 4 1.4
2 0 2
2 1 2.1
2 2 2.2
2 3 2.3
2 4 2.4
3 0 3
3 1 3.1
3 2 3.2
3 3 3.3
3 4 3.4
t r 4 d 3
t l 4 u 3
t r 2 d 2
d 1 2
d 2 3
d 3 4
d 2 0
t r 2 d 2
t r 1 u 2
t r 1 d 3
t l 3
t d 1 l 1
t u 2

the first line contains the number of rows and columns, the next few lines contain the row and column and the value that is stored in that position. After that are the traverse lines and delete lines. I have the values read in perfectly and everything created, but i am not quite sure how i would go about reading in the traverse and delete lines. I want to set the first character of each traverse line which is denoted by a t to a traverse variable, the second character to a direction variable, the third character to a step variable, then the fourth would be the direction again and the 5th the step variable again. Then when it gets to the delete lines read in the first character as the action variable and then the second character to a row variable and 3rd character to a column variable. Also i am not quite sure how to read in the traverse line with two directions and then go about reading in the traverse line with only 1 direction and still have the values stored to the correct variables.

Here is my code

Node* currentObject;
	Node a[50][50];
	Node node;
	Node* start;
	if (!cin.eof())
	{
	cin>>row; 
	cin>>column;

	cout<<row;
	cout<<column;
	

	for(int i=0;i<row;i++)
	{
	for ( int j = 0; j < column; j++ )
	{
		
		currentObject=&a[0][0];
			cin>>rowpos;
	cin>>colpos;
	cin>>value2;


   a[ rowpos ][ colpos ].value =value2  ;
   	if(rowpos-1>=0)
	{
		a[rowpos][colpos].top=&a[rowpos-1][colpos];
	}
	if(rowpos+1<=row)
	{
		a[rowpos][colpos].bottom=&a[rowpos+1][colpos];
	}
	if(colpos-1>=0)
	{
		a[rowpos][colpos].left=&a[rowpos][colpos-1];
	}
	if(colpos+1<=column)
	{
		a[rowpos][colpos].right=&a[rowpos][colpos+1];
	}


	
	}

	}

	}

	while(!cin.eof())
	{
		
	
		
		if (!cin.eof())
		{
		/*getline(cin,action,' ');
		getline(cin,dir,' ');
		cin>>step;*/


		/*if(action=="t")
		{
			if(dir=="r")
			{
				for(int i=0;i<step;i++)
				{
					currentObject=currentObject->right;
					cout<<currentObject;
				}
			}
			if(dir=="l")
			{
				for(int i =0;i<step;i++)
				{
					currentObject=currentObject->left;
					cout<<currentObject;
				}
			}
			if(dir=="u")
			{
				for(int i =0;i<step;i++)
				{
					currentObject=currentObject->top;
					cout<<currentObject;
				}
			}
			if(dir=="d")
			{
				for(int i =0;i<step;i++)
				{
					currentObject=currentObject->bottom;
					cout<<currentObject;
				}
			}
		}*/


		}
	}

}

It first reads in the row and column. Then sets the values to the rows and columns. After this the right,left,top,and bottom objects are set. Im just not sure how to read in the traverse and delete lines into variables, so i can then do the if statements to do my traversing. Any help would be really appreciated.

> i am not quite sure how i would go about reading in the traverse and delete lines.
instead of trying to do everything in one place, break it up into smaller tasks (each of which is quite manageable).

> I want to set the first character of each traverse line which is denoted by a t ...
the first character on each line would tell you if it is a traverse action or a delete action.

enum action { TRAVERSE, DELETE, NOTHING } ;
action what( const std::string& line )
{
  switch( line[0] )
  {
     case 't' : case 'T' : return TRAVERSE ;
     case 'd' : case 'D' : return DELETE ;
     default : return NOTHING ;
  }
}

> i am not quite sure how to read in the traverse line with two directions
> and then go about reading in the traverse line with only 1 direction
each move in the traverse is given by a (direction,num_steps) tuple.
a traversal is a sequence of one or more of such tuples.
you would need a sequence container of these tuples to keep the moves in a traversal.

enum direction { UP, RIGHT, DOWN, LEFT, STAYPUT } ;
typedef std::vector< std::pair<direction,int> > traverse_data ;

for a traverse, read in these tuples, adding each one to traverse_data till end of line.

traverse_data get_traversal( const std::string& line )
{
  traverse_data traversal ;
  std::istringstream stm(line) ;
  char eat_it ; stm >> eat_it ; // skip the first char (action)
  char dir ;
  int step ;
  while( stm >> dir >> step )
  {
     direction where ;
     switch(dir)
     {
       case 'u' : case 'U' : where = UP ; break ;
       case 'r' : case 'R' : where = RIGHT ; break ;
       case 'd' : case 'D' : where = DOWN ; break ;
       case 'l' : case 'L' : where = LEFT ; break ;
       default : where = STAYPUT ;
     }
     traversal.push_back( std::make_pair( where, step ) ) ;
  }
  return traversal ;
}

> the delete lines read in the first character as the action variable and
> then the second character to a row variable and 3rd character to a column variable.
data for a delete is just a (row,column) tuple. and reading this in is easy.

typedef std::pair<int,int> delete_data ;
delete_data get_position( const std::string& line )
{
  std::istringstream stm(line) ;
  char eat_it ;
  int row, col ;
  if( stm >> eat_it >> row >> col )
     return std::make_pair( row, col ) ;
  else return std::make_pair( -1, -1 ) ;
}

now put it all together:

void act_on_it( std::istream& stm )
{
  std::string line ;
  while( std::getline( stm >> std::ws, line ) )
  {
    action act = what( line ) ;
    if( act == TRAVERSE )
    {
      traverse_data traversal = get_traversal(line) ;
      // perform the traversal
      for( std::size_t i=0 ; i<traversal.size() ; ++i )
      {
        // direction is traversal[i].first
        // num_steps is traversal[i].second
        // move num_steps in direction
      }
    }
    else if( act == DELETE )
    {
      delete_data position = get_position(line) ;
      // delete element at row position.first, 
      // column position.second
    }
    else
    {
      // handle error
    }
  }
}

Ok awesome thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.