• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

wtf is wrong with this code

Deeko

Lifer
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
 
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. )
 
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.

😉
 
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.
 
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());
 
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.
 
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
 
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: 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. 😛
 
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 🙂
 
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 😛
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
 
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? 😀
 
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? 😀

Heh, same thing I was thinking.
 
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? 😀

Don't talk down to me boy 😛 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 🙂
 
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? 😀

Don't talk down to me boy 😛 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:
 
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? 😀

Don't talk down to me boy 😛 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... 😀
 
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. 😛
 
Back
Top