Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   String manipulation C++ (http://www.go4expert.com/forums/string-manipulation-cpp-t14957/)

thekevin07 4Nov2008 00:40

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:

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;
}


thekevin07 4Nov2008 08:47

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

xpi0t0s 4Nov2008 18:17

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.

thekevin07 5Nov2008 00:44

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.

xpi0t0s 5Nov2008 04:15

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

thekevin07 5Nov2008 23:18

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.

xpi0t0s 6Nov2008 02:35

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.

thekevin07 6Nov2008 03:16

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.

xpi0t0s 6Nov2008 17:22

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

xpi0t0s 6Nov2008 17:30

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 19:38.