![]() |
String manipulation C++
I'm having trouble writing a program that goes through a string and puts each word on a new line and replaces the characters " { [ ( with what there names are like quote brace bracket paren and prints that on a new line the problem i am having is printing out the other words. I can print the first word but not the second or third. So if i have the string
"go 4 expert" it should print out Code:
quoteCode:
quoteCode:
#include "stdafx.h" |
Re: String manipulation C++
i have a feeling it is a problem with how i am getting the start position (pos) I've spent a week on this and cant figure it out.
Any help is greatly appreciated. Thanks |
Re: String manipulation C++
Try stepping through it in the debugger. The problem should become immediately obvious.
Use f9 to set a breakpoint, f5 to start the program in the debugger, f10 to step line by line. Autos is a good window to have visible; it displays all the info that is relevant to the current statement. Enter "go 4 expert" at the first cin>>t (the debugger will wait for input; you may need to Alt-Tab to the DOS box containing the process) and watch what happens on the second time through the outer while loop, after cin>>t processes the 4. |
Re: String manipulation C++
I tried the debugger it seams like it skips over it but I don't have a clue on how I'm suppose to fix it.
|
Re: String manipulation C++
Why do you think it should go into the while loop?
What are the values of pos and t? (These will be displayed in the Autos window.) |
Re: String manipulation C++
the debugger says t is "4" and pos is 3
my professor put the while loop in. i missed the deadline to submit this so its not really an assignment any more. |
Re: String manipulation C++
Yes, I got those values too. What should pos be at this point? Can you think of a way to achieve that?
It may not be an assignment any more, but if you're interested in programming you should stick with this until you get it; it will help future assignments. |
Re: String manipulation C++
That's the problem i'm having I don't know how to make pos what it should be when it gets to that point. i thought it was to take the size of s and add it to the pos and increment that way i tried the size of t too but that didn't work. I've spent two weeks on this and i still have no idea what to do. Please tell me.
|
Re: String manipulation C++
I don't want to tell you outright - I could easily - but you won't learn anything that way. This isn't difficult so take a break, have a cup of coffee, get "this is impossible I can't do it" out of your head and try again.
Do you at least know what value pos should have? Why? For example, should it be 27, or three million, or -pi, -273.16, any other value? If you know what value pos should have, can you think of a way to make it have that value? If you're completely stuck on this point, have a look at how you made len have the value 1, just after "if(n==pos)" and conditional on that, of course. Having worked out how to make pos have the desired value, where should you place the statement that achieves that? If you're stepping through it in the debugger, watching pos, then as soon as you notice its value is "wrong" could be a good place to put it (i.e. just before the statement where it's wrong). |
Re: String manipulation C++
Another suggestion: t is "4" when the problem occurs. So run the program again, just entering 4, not the full "go 4 expert". What value does pos have at the same point? Does it work OK?
|
| All times are GMT +5.5. The time now is 01:45. |