Scroll to my latest post
---------------------------------
Alright so I made this program for class that will separate a string into words. The program performs correctly except for after the output another line is displayed with lots of odd characters and such. The only reason I can think of for why this is happening is that it is reading outside of the character array but I do not see how it could do this.
Code:
#include <iostream>
using namespace std;
#define LINELENGTH 100
void spaceWords(char phrase[]);
void tWords(char phrase[]);
void rWords(char phrase[]);
void nWords(char phrase[]);
int main(void)
{
while (!cin.eof())
{
char phrase[LINELENGTH] = {' '};
cout << "Please enter a line of text: ";
cin.getline (phrase, LINELENGTH);
spaceWords(phrase);
}
return 0;
}
void spaceWords(char phrase[])
{
int count = 0;
int total = 0;
int base = 0;
int count2 = 1;
//loop until count is equal to line length
while (count < LINELENGTH)
{
//while not a blank space, add to the counter
while (phrase[count] != ' ')
{
count ++;
}
//print out word number
cout << "Word " << count2 << ": ";
//
for (base; (base < count); base ++)
{
cout << phrase[base];
}
cout << '\n';
base = count + 1;
count++;
count2++;
}
}
Also I have to account for \r, \n, \t...but when i make the while loop look for it...I get the error "illegal escape sequence" I tried this to look for it:
Code:
while ((phrase[count] = '\') && (phrase[count+1] = 'n'))
Also we are not allowed to use strtok. Only using like comparing (==) in an if or while statement or a switch statement for one char.
Thanks for the help,
...Dan