Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with strings and substrings

Status
Not open for further replies.

mstekkie

Technical User
May 15, 2002
51
CA
i'm having a lot of problems with this program. hopefully someone can help me out.

the program prompts the user for an equation such as 123 + 123 = what i'm supposed to do is break up the string into four substrings and display each string on its own line. i know how to display the substrings, but my problems is with breaking up the string into the substrings.

this is the code i have so far for trying to separate the string:

if (cin.peek == " ")
cin.ignore();
else
cin >> first;

i know it's wrong but i don't know how to fix it. any help will be greatly appreciated.
 
I see two options. One is to capture everything on the input line and then parse it out.
Code:
cin >> TheLine;
int WordCount = 0, Z = 0;
string TheWord;
int ChrCnt = TheLine.length();
for (int j=0; j<=ChrCnt; j++)
{
    if TheLine[j] == &quot; &quot;
    {
        TheWord++;
        Z = 0;
    }
    else
    {
        if TheWord == 1
        {
            First[Z] = TheLine[j];
            Z++;
        }
        else if TheWord == 2
        {
            Second[Z] = TheLine[j];
            Z++;
.
.
.
[code]

BTW, I'm doing this very quickly so the logic may not exactly work but you should get the idea.

My prefered way to do this is to use [b]getline[/b] and look for the space. You could do this either on the fly or by capturing the whole line like the above example. James P. Cottingham
[COLOR=blue]
When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[/color][tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top