Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2005, 4:40 PM   #1
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
trouble reading string into array

so i've got this string called code, of type std::string, which has this inside it

Quote:
1
2
3
4
and yes i've verified this by using cout. now im trying to read each line into a seperate element of the array statements, which is also of type std::string. when i run the below code however:

int last_flushed = 0;
std::string statements[1000];
int array_pos = 0;
for (int i = 0; i <= code.size(); i++)
{
	if (code[i] == '\n')
	{
		statements[array_pos] = string_copy(code, last_flushed, i - last_flushed);
		cout << statements[array_pos];
		array_pos++;
		last_flushed = i;
	}
}

it just prints out this:

Quote:
1

2
any idea why this is happening? ive been staring at this for a day or so, i feel really stupid right now. thanks in advance for your help.
__________________
Children in the dark cause accidents, and accidents in the dark cause children.

http://www.ronincoders.org
Intimidat0r is offline   Reply With Quote
Old Dec 10th, 2005, 4:47 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You are using a lot of things there which you neither show nor define nor explain. Crystal balls are nice, but we don't all have one. Would you care to think about your post, think about your problem, think about your audience, and elucidate?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 10th, 2005, 4:50 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I have no idea what you're trying to do here. Can you post some more code?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 10th, 2005, 4:51 PM   #4
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
I tried reading through that code and i hit dead ends that could be vital to explaining whats wrong.
For example code.size() in the for loop.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Dec 10th, 2005, 5:08 PM   #5
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
I'm sorry, I forgot to post string_copy()'s definition:

string string_copy(string source, int startpos, int length)
{
	string final;
	final = "";
	
	for (int i = startpos; i <= length; i++)
		final += source[i];
	
	return final;
}

the idea with that function is to return a 'length' substring of 'source', starting at 'startpos'.

the idea of all this was to take a string containing several lines, each seperated by a newline (\n) and put each line into a seperate element of the array statements.

the variable last_flushed is so it puts a substring of 'code', between last_flushed (the end of the last string put into the array) and i (the position of the next \n) and not everything from the beginning to whatever newline it encounters.

@coldDeath: size() is a member of std::string. code.size() returns the length of the string 'code'.
__________________
Children in the dark cause accidents, and accidents in the dark cause children.

http://www.ronincoders.org
Intimidat0r is offline   Reply With Quote
Old Dec 10th, 2005, 5:10 PM   #6
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Ty, I couldn't see any variables called code, so i got a little confused.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Dec 10th, 2005, 5:17 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Instead of string_copy, why not just do this?
std::string myNewString = myOldString.substr(offset, length);
They think of these things, y'know.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 10th, 2005, 5:28 PM   #8
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
coldDeath: np

Ooble: thanks, i got it working. yay.
__________________
Children in the dark cause accidents, and accidents in the dark cause children.

http://www.ronincoders.org
Intimidat0r is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC