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: quote go 4 expert quote my program prints out Code: quote go here is my source Code: #include "stdafx.h" #include <iostream> #include <string> #include <cctype> using namespace std; bool isnum(string x) { for(int a=0;a<(int)x.size()-1;a++) { if(isdigit(x[a])) { return true; } else if(x[a]=='.') { return true; } else return false; } return false; } int _tmain(int argc, _TCHAR* argv[]) { string s,t,temp; temp=""; bool quote=false; bool paren=false; bool bracket=false; bool brace=false; bool endQuote=false; bool endParen=false; bool endBracket=false; bool endBrace=false; cout<<"enter string"<<endl; size_t pos =0; size_t n; while(cin>>t) { while(pos <t.size()) { size_t len; n=t.find_first_of("\"(){}[]",pos); if(n==pos) len=1; else if(n==string::npos) len=t.size()-pos; else len=n-pos; s=t.substr(pos,len); pos += s.size(); switch(s.at(0)) { case '"': if(!quote) { quote=true; cout<<"quote"<<endl; continue; } else { endQuote=true; } break; case '(': if(!paren) { paren=true; cout<<"paren"<<endl; continue; } else { endParen=true; } break; case '[': if(!bracket) { bracket=true; cout<<"bracket"<<endl; continue; } else { endBracket=true; } break; case '{': if(!brace) { brace=true; cout<<"brace"<<endl; continue; } else { endBrace=true; } break; default: cout<<s<<endl; break; } } } return 0; }
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
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.
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.
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.)
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.
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.
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.
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).
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?
And another. Run the following code, entering "go 4 expert" when prompted for a string. What is the output? Did you expect that output? Code: int _tmain(int argc, _TCHAR* argv[]) { string t; cout<<"enter string"<<endl; while(cin>>t) { cout << "cin>>t gives t the value: " << t << endl; } return 0; } (OK, that's it for now)
That will print and its what i expected that was part a of the program Code: "go 4 expert" i tried Code: if(n==pos) { len=1; pos=0; } else if(n==string::npos) len=t.size()-pos; else { pos += s.size(); len=n-pos; } but it created an infinite loop so i think pos needs to be 4 now but i have no clue how to do it. We've spent along time on this and I learn better by example any way Please, Please tell what i need to do to fix it (and why otherwise it's pointless). I've used this forum to help with c++ stuff that I dont understand once someone told me what to do and why I understood it better and as a result I Aced my midterm. Thanks for your patience.
OK. But you have to wean yourself off solving problems by getting examples from somewhere. This is not always going to be possible. On second thoughts I'll try just once more. If you don't get it after this I'll deliver you the solution. I do understand your frustrations, I've been there; I remember debugging for two solid weeks once and finding the problem was a missing semicolon or something daft. The outer while loop reads strings from the user and breaks them up by spaces. The inner loop loops over the resulting string, which is <"go> on the first time through, <4> on the second and <expert"> on the 3rd. The string "4" contains one character (a '4') at [0] and a terminating null at [1]. pos loops over this string and is the index of the character you're currently interested in. Why exactly do you think this needs to be 4? t.size() is only 1, so if pos=4 then the expression pos<t.size() is going to evaluate FALSE immediately. To go into the loop pos MUST be less than t.size(). It must also be non-negative, of course. So what is the single value that satisfies that? What number is >=0 and <1? Since pos contains the wrong value immediately before the inner while and is fine before that, I'd say the solution probably has to go... Code: while(cin>>t) { // <-- ...HERE while(pos <t.size())
I'm close i put pos=0; right between the loops and that printed Code: "go 4 expert still missing the second quote though. thanks