wtf is wrong with this code

Deeko

Lifer
Jun 16, 2000
30,213
12
81
string first = input.substr(input.find(',') + 2, input.find(':'));

input is a string in the format
LastName, FirstName: Score

the code extracts the first name...I've used almost that exact same piece of code in a previous assignment...but for some reason, it starts at the right position, but then goes till the end of the string. I checked, the find routine is returning the right position in the string...but it isn't working. Example...
Davis, Mark: 100
will extract the first name as
Mark: 100

why? urg
 

DrPizza

Administrator Elite Member Goat Whisperer
Mar 5, 2001
49,601
167
111
www.slatebrookfarm.com
I'm not sure what is wrong with that code... it's been too long for me...

But, I think you're ready to work for Microsoft! Their code never seems to do what I want it to do either!

(I'm on AT because I can't get Word to format some pages the way I want... Or rather, they won't print the way I want. )
 

Syringer

Lifer
Aug 2, 2001
19,333
3
71
If you're not smart enough to post it in the correct forum I'm not surprised you don't know what's wrong with the code.

;)
 

lukatmyshu

Senior member
Aug 22, 2001
483
1
0
Does input.find(':') return the correct position in your test case? That seems to be the only thing I can think of testing ...test that then try hard-coding in first = input.substr(input.find(',')+2,POSITIONOFSECONDMARKER) to verify that you are using substr correctly.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Here is the most aggrivating part. Notice how the code to extract the last name and score is very similar...and they both work perfectly!

string last = input.substr(0, input.find(','));
string first = input.substr(input.find(',') + 2, input.find(':'));
double score = atof(input.substr(input.find(':') + 2, input.size()).c_str());
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Originally posted by: lukatmyshu
Does input.find(':') return the correct position in your test case? That seems to be the only thing I can think of testing ...test that then try hard-coding in first = input.substr(input.find(',')+2,POSITIONOFSECONDMARKER) to verify that you are using substr correctly.

yes. I used the following code to test that:
//int startpos = input.find(',') + 2;
//int endpos = input.find(':');
//cout << startpos << endl << endpos << endl;
and then did startpos and endpos for the variables sent to the substr function...still no good...and startpos and endpos were the correct positions in the string.
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
the second argument of the substr method is not the second marker, it's how many characters you go after the first marker
this will work:
input.substr(input.find(',')+2, input.length() - input.find(':') + 2);
try this and let me know
 

Dudd

Platinum Member
Aug 3, 2001
2,865
0
0
I don't know about the standard string.h (I used apstring.h in AP Computer Science), but our substr function parameters were the position to start at and then the length of the string to extract. So, your call of Davis, Mark: 100 would start at position 7, and then continue for 11 places, which would include everything up to the end. If string.h is the same as apstring.h, then you would want it from
input.find(':')-input.find(',')-2. That would be my guess.

Edit: Beaten to it I see.
 

Aves

Lifer
Feb 7, 2001
12,232
30
101
Originally posted by: Tyler
bah, screw java:
$first =~ s/^[^,]*, ([^:]*):.*$/$1/;

They usually fail you if you do your homework in the wrong language and it isn't Java anyway. :p
 

StageLeft

No Lifer
Sep 29, 2000
70,150
5
0
Originally posted by: Tyler
bah, screw java:
$first =~ s/^[^,]*, ([^:]*):.*$/$1/;
Bah screw whatever the hell mess that crap is! Pearls should be on women only.

Anyway what's wrong with your code is I, the code guru, didn't write it. Sorry :)
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: Dudd
I don't know about the standard string.h (I used apstring.h in AP Computer Science), but our substr function parameters were the position to start at and then the length of the string to extract. So, your call of Davis, Mark: 100 would start at position 7, and then continue for 11 places, which would include everything up to the end. If string.h is the same as apstring.h, then you would want it from
input.find(':')-input.find(',')-2. That would be my guess.

Edit: Beaten to it I see.

Originally posted by: Dudd
I don't know about the standard string.h (I used apstring.h in AP Computer Science), but our substr function parameters were the position to start at and then the length of the string to extract. So, your call of Davis, Mark: 100 would start at position 7, and then continue for 11 places, which would include everything up to the end. If string.h is the same as apstring.h, then you would want it from
input.find(':')-input.find(',')-2. That would be my guess.

yep, that's what i said 2 posts up there :p
the other two codes that Deeko used to test the first name and the score worked too because :
in the first case, the second marker pos is equal to the length of characters that he needs
in the second case, input.size() is far greater than the length of the score, the method will only go to the end of the string, that's why it worked
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Seriously.... if it is that much effort to debug such a simple piece of code, perhaps you should reconsider writing any code in the first place? :D
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Descartes
Seriously.... if it is that much effort to debug such a simple piece of code, perhaps you should reconsider writing any code in the first place? :D

Heh, same thing I was thinking.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Originally posted by: Descartes
Seriously.... if it is that much effort to debug such a simple piece of code, perhaps you should reconsider writing any code in the first place? :D

Don't talk down to me boy :p We all have mental lapses from time to time....I'll have you know I'm one of the top 10 coders in my freshman class, and was the top from my senior class in HS, so you can go lick a d!ck :)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Deeko
Originally posted by: Descartes
Seriously.... if it is that much effort to debug such a simple piece of code, perhaps you should reconsider writing any code in the first place? :D

Don't talk down to me boy :p We all have mental lapses from time to time....I'll have you know I'm one of the top 10 coders in my freshman class, and was the top from my senior class in HS, so you can go lick a d!ck :)

My high school programming teacher took me aside to tell me how much potential he saw in me and <insert blah blah blah here>, I don't really think it means a whole lot, it just means that I was good compared to most of the people that had taken that class, which really doesn't say a whole lot. :music:
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: Deeko
Originally posted by: Descartes
Seriously.... if it is that much effort to debug such a simple piece of code, perhaps you should reconsider writing any code in the first place? :D

Don't talk down to me boy :p We all have mental lapses from time to time....I'll have you know I'm one of the top 10 coders in my freshman class, and was the top from my senior class in HS, so you can go lick a d!ck :)


Still doesn't change the fact that it was a really simple problem... :D
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
It's not that anyone is perfect, or Deeko is stupid, or anything like that, it's the fact that he didn't seem to have put effort into figuring out what was causing the problem. Out of the specific code he posted, there are three functions being utilized. Obviously, if something is not behaving correctly, one of those functions is returning something that you don't expect, and you should find out what each is returning. That's all.

edit: The other day I was pulling hair out about something that wasn't working when I was staring right at the code and there was NOTHING wrong with it. NOTHING! Well.. turns out that I eventually figured out the problem it was a very unnoticeable typo. :p