delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/13/20:46:17

From: am898 AT chebucto DOT ns DOT ca (Blake McCurdy)
Newsgroups: comp.os.msdos.djgpp
Subject: Input Parsing problem
Date: 13 Jun 1998 23:26:07 GMT
Organization: Chebucto Community Net
Lines: 100
Message-ID: <6lv1qf$nfu$2@News.Dal.Ca>
NNTP-Posting-Host: chebucto.ns.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I have a small problem. I have an array of 80 characters. Is there anyway
to to input some stuff, and then place a terminating '\0' at the end of
the input, not at the end of the 80 char array? If this sounds abstract,
here's the real problem. I want to input a line of text into one array,
and then parse it into 5 different arrays, one word each, and count the
number of words as I go. Here's the source. It works most of the time, but
sometimes rawPhrase has garbage in it. It's all part of a Phrase class.

Phrase::Phrase()	
{
        first  = new char[ 20 ];
        second = new char[ 20 ];
        third  = new char[ 20 ];
        fourth = new char[ 20 ];
        fifth  = new char[ 20 ];

        strcpy( first, "\0" );
        strcpy( second, "\0" );
        strcpy( third, "\0" );
        strcpy( fourth, "\0" );
        strcpy( fifth, "\0" );

        rawPhrase = "help\0";

        cout << rawPhrase;

        nofWords = 0;
}

void Phrase::getLine()
{
        ostrstream sfirst ( first,  20 );
        ostrstream ssecond( second, 20 );
        ostrstream sthird ( third,  20 );
        ostrstream sfourth( fourth, 20 );
        ostrstream sfifth ( fifth,  20 );

        char* temp = new char[ 20 ];

        do
        {

                cout << "\nWhaddya wanna do? ";

                cin.gets( &rawPhrase );
                istrstream sphrase( rawPhrase, 80 );

                strcpy( temp, "" );

                sphrase >> temp;
                if( strcmp( temp, "" ) )
                {
                        nofWords++;
                }
                sfirst  << temp << ends;

                sphrase >> temp;
                if( strcmp( temp, "" ) )
                {
                        nofWords++;
                }
                ssecond << temp << ends;

                sphrase >> temp;
                if( strcmp( temp, "" ) )
                {
                        nofWords++;
                }
                sthird << temp << ends;

                sphrase >> temp;
                if( strcmp( temp, "" ) )
                {
                        nofWords++;
                }
                sfourth << temp << ends;

                sphrase >> temp;
                if( strcmp( temp, "" ) )
                {
                        nofWords++;
                }//if
                sfifth << temp << ends;

                if( nofWords == 0 )
                {
                        cout << "\n\n\nEh?\n\n";
                }//if

        }while( nofWords == 0 );
}//void Phrase::getline()

--
_______________________________________________________________________
|								      |
|								      |
| 	   "So then I said 'No, Juan, it's the rice!!' "	      |
|								      |
|_____________________________________________________________________|

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019