LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2006, 07:22 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
C++: Problem with char's in strings.


The code that I am trying to write, is supposed to search a string for a left bracket ('{'), then begin to copy information afterwards to be parsed later by the program. And any information between the time the program is not copying characters, to the time it finds the character needed to allow copy of characters will result in a termination of the program. I first find a word in a text file (which is "Dimensions") using find(). I add 10 to that word so it starts just right after the 's' in dimensions. I use an integer (celement) to store the character location right after 's', and to check each element after that character in the string map.level to see if the character '{' comes up. While it is searching for the left bracket, it is supposed to make sure no other characters, besides '\n' and ' ', are present between the left bracket '{' and the 's' in Dimentions.

Now the problem. The problem is that it does not seem to differentiate spaces (' ') in the string which it has(the full string shown before the block of code), as not an error, and instead, it sees it as an error and exits. I have made another program which does the exact thing, and works, but this one seems to have a problem which I have been overlooking.

Here is part of the string I'm pulling information from and using in my program. The beginning is the only necessary element of the string:

Code:
Dimensions { -nebula x0 y0 w800 h600 m1 -gravel_whole x0...

Here is the code that is having problems:

Code:
if ( map.level.find("Dimensions") != string::npos )
{
	celement = map.level.find("Dimensions") + 10;
}
else
{
	cout << "\nCould not find \"Dimensions\" in map.level or levels.txt, in MapParse.  Exit failure!\n"; 
	_EXIT_( 1 );
}

for ( ; map.level[celement] != '{'; ++celement )
{			
	if ( map.level[celement] == '}' )
	{
	        cout << "\n\t'}' was found while attempting to locate '{'.  Levels was setup incorrectly.  MapParse, exit failure!\n";
		_EXIT_( 1 );
	}
	
	if ( map.level[celement] != ' ' || '\n' )
	{
		cout << "\n\tUnknown characters found after \"Dimensions\" and before '{' in MapParse.\n"
		<< "\n\tThe character found was, " << map.level[celement] << ".\n\tLocation: map.level[" << celement << "].\n"
		<< "\n\tThe whole string is, \n\n"
		<< map.level << "\nExit failure!\n";

		_EXIT_( 1 );
        }
}
I will give more info if needed.

Last edited by RHLinuxGUY; 09-10-2006 at 10:07 PM.
 
Old 09-10-2006, 09:58 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uh - I think you mean "Dimensions"
 
Old 09-10-2006, 10:06 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
ah, you right. changed.
 
Old 09-11-2006, 12:36 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Thanx for not taking umbrage at my spelling correction ;-)

As far as parsing your input, I would take a somewhat different approach than it looks like you might be pursuing. I'd do something like this:
Code:
  // Read next line
  while (fgets (buff, sizeof buff, fp)!= NULL)
  ptr = buff
    // Parse next "token"  
    while ((token = next_token (buff, ptr)) != EOL)
      switch (token)
        case a:
          ...
        case b:
          ...
In other words:
1. Get the data (however makes sense)
2. Parse the data (into some kind of "token")
3. Act on the parsed data (typically, with some kind of case block)

I found this on link on Google. I haven't studied it carefully, but it looks like the general idea I'm trying to explain:
http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4069/

'Hope that helps .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Problem the Strings in C++ gjagadish Programming 4 10-15-2005 03:45 AM
php problem with strings graziano1968 Programming 5 10-07-2005 02:10 PM
parsing strings problem McB Programming 6 04-19-2004 09:50 AM
Problem with strings in Red Hat 8.0 smriti Linux - General 1 03-03-2003 07:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration