3 Replies - 1051 Views - Last Post: 08 December 2012 - 09:59 PM Rate Topic: -----

#1 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Iteration problem

Posted 08 December 2012 - 08:47 PM

Hi
I have this problem I am trying to find the v in the iteration now for some reason the iteration skips index 0.
I know theirs a logical solution to this but I'm a bit stuck any help would be appreciated

string line = "v 0 2.928242206573486 0";

for( unsigned int i = 0; i < line.length(); i++ )
	{
		if( num = line.find( "v", i ) )
		{
			if( i == num )
			{
				cout << "V Numbers: " << i << endl;
			}
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Iteration problem

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Iteration problem

Posted 08 December 2012 - 09:03 PM

"v" is a string, and there is no string matching that.
'v' is a char, and that maybe is what you should be looking for. I believe that's true, but I'm a C guy, NOT C++ - could be WAY off here. But see below.

Also, this part, looks either incomplete or incorrect.
 if( num = line.find( "v", i ) )   //looks dodgy to me



If num is set to 0, won't that translate to the whole if(expression) being false?
Was This Post Helpful? 0
  • +
  • -

#3 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Re: Iteration problem

Posted 08 December 2012 - 09:45 PM

Hi
I changed my code to this this definetely works and finds v at 0

string line = "v 0 2.928242206573486 0";

for( unsigned int i = 0; i < line.length(); i++ )
	{

		cout << line.at( i ) << endl;
		
                if( 'v' == line.at( i )  )
		{
			cout << "V Numbers: " << i << endl;
		}
	}


Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Iteration problem

Posted 08 December 2012 - 09:59 PM

Excellent - that looks like robust and clear code!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1