Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Help] Manipulating a string within a loop bug  (Read 1483 times)

0 Members and 1 Guest are viewing this topic.

crumpetxxix

  • Newbie
  • *
  • Posts: 9
    • View Profile
[Help] Manipulating a string within a loop bug
« on: August 29, 2012, 02:58:47 pm »
Ok so im using SFML2.0 with visual studios 2008 if that helps at all.. In the process of creating my game I've come into a problem when using string.substr and i cant figure out for the hell of me why this is happening.
In the game I'm creating, like most other games you need to load from a database, simple enough.  So I started writing my database reader in my program and created a sample DB for my reader.  It opens, checks for the right DB and formatting, finds, and reads the lines and stores them into a dynamic array of strings for me to manipulate.. But when i actually get to the area where my actual data is I cannot get it to find the right line in my code to be able to decifer where the data is on the line..

If that didnt make enough sense let me show you. If within my database i had the line RegionID==1, i cant get my code to find the part of the string which has the "==" in it to seperate the data table with the data.

Within my code here is the segment where it takes one of the lines within the database which has been read, then it checks the string for the "==" and tries to read the data into the game

dataLine is the string with the data being read, break() is just a cout function to help me decifer whats breaking and the variable dataRep is the "==" which is supposed to be being found within the string

Code: [Select]
for (int cntr = 0; cntr < dataLine.length(); cntr++)
{
Break(8);
std::cout << dataLine.substr(cntr, cntr + 2) << std::endl;
std::cout << cntr << " " << cntr + 2 << std::endl;
if (dataLine.substr(cntr, cntr + 2) == dataRep)
{
if (dataLine.substr(0, cntr) == "Name")
{
//std::cout << dataLine.substr(cnt + 2, dataLine.length()) << std::endl;
player.setName(dataLine.substr(cntr + 2, dataLine.length()));
break;
}
if (dataLine.substr(0, cntr) == "RegID")
{
//std::cout << dataLine.substr(cnt + 2, dataLine.length()) << std::endl;
player.setRegionID(atoi(dataLine.substr(cntr + 2, dataLine.length()).c_str()));
break;
}
if (dataLine.substr(0, cntr) == "RegX")
{
//std::cout << dataLine.substr(cnt + 2, dataLine.length()) << std::endl;
player.setRegionX(atoi(dataLine.substr(cntr + 2, dataLine.length()).c_str()));
break;
}
if (dataLine.substr(0, cntr) == "RegY")
{
//std::cout << dataLine.substr(cnt + 2, dataLine.length()) << std::endl;
player.setRegionY(atoi(dataLine.substr(cntr + 2, dataLine.length()).c_str()));
break;
}
if (dataLine.substr(0, cntr) == "Direction")
{
//std::cout << dataLine.substr(cnt + 2, dataLine.length()) << std::endl;
player.setDirFacing(atoi(dataLine.substr(cntr + 2, dataLine.length()).c_str()));
break;
}
}
}

so as you can see from my code i had the console output what the actual sub string of what was currently being read and also between which two numbers in the code.  The actual numbers i printed within the loop are normal but here is what the substr output reads.

if it was reading the line "PlayerName==Steven" it would output "Pl", "lay", "ayer", "yerNa", "erNa", "rNa", and finally "Na" then ignore the rest of the line.

If anyone has experienced this before please help because this bug does not make sense to me at all.

crumpetxxix

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: [Help] Manipulating a string within a loop bug
« Reply #1 on: August 29, 2012, 05:10:12 pm »
Never mind im an idiot. i thought the substr function parameters were substr(startposition, endposition).. turns out its substr(startpos, numberofcharacters).  i dont know how i ended up getting the rest of my DB reader to work since its also using substr but i think this should fix it

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: [Help] Manipulating a string within a loop bug
« Reply #2 on: August 29, 2012, 05:21:32 pm »
Well you still may be interested to know that the string class also has diffrent functions, including functions to search for specific substrings, so you wouldn't have to iterate over all characters of the line to find the right positions. See the C++ reference for the string class. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

crumpetxxix

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: [Help] Manipulating a string within a loop bug
« Reply #3 on: August 29, 2012, 08:29:58 pm »
Hmm, that was a good read.  I overlooked the find function because i had it confused with the find_first_of function so i never even gave it a second thought about using, that seems like it would have been a lot easier to use the find function instead of the loop checking every character one by one.  Thank you.