I'm having some trouble with my problem in which I'm supposed to take a character array from the console and manipulate it to read correctly using cstrings and a class. Everything works as intended except for when I'm supposed to remove multiple spaces if they are entered and have only one space inbetween words.
I take the user inputted sentence from the console and into an array through cin.get and then add '\0' to the end of it to make it a Cstring. The issue I am having is the syntax on how to test to see if there are multiple spaces entered.
Here is what I have so far...
The area I put in bold is the area I am having trouble with. I was attempting to check and see if the current array character was not a space and the previous one was not a space then to update tempString but if both were a space then to move everything in tempString back one character. But its not working : / The two final lines that are bolded I have in there just for debugging use. Thanks in advance for any advice.Code:#include <iostream> #include <cstring> using namespace std; class sentence { private: char userString[100]; public: char getUserString(); void setUserString(char userInput[], int& size); void readSentence(char userInput[], int& size); void correctSentence(int& size); void displayCorrectedSentence(); }; int main () { sentence userSentence; char userInput[100]; int size = 100; cout << "Please enter sentence less than 100 characters. " << endl; cout << "End the sentence with a period." << endl << endl; userSentence.readSentence(userInput, size); userSentence.correctSentence(size); userSentence.displayCorrectedSentence(); } char sentence::getUserString() { return userString[100]; } void sentence::setUserString(char userInput[], int& size) { userString[size] = userInput[size]; } sentence::readSentence(char userInput[], int& size) { int i = 0; cin.get(userInput[i]); userString[i] = userInput[i]; while (userInput[i] != '.') { i++; size = i - 1; cin.get(userInput[i]); userString[i] = userInput[i]; } userString[i + 1] = '\0'; } void sentence::correctSentence(int& size) { int i; char tempString[100]; for(i = 0; i < size; ++i) { if(userString[i] == '\n') //new lines become spaces userString[i] = ' '; } i = 1; //reset counter for new condition int i2 = 0; //represents spaces tempString[0] = userString[0]; for(i; i < size; ++i) { if((userString[i] != ' ') && (userString[i-1] != ' ')) { tempString[i] = userString[i]; } if((userString[i] == ' ') && (userString[i-1] == ' ')) { ++i2; tempString[i] = userString[i-i2]; } } } tempString[i+1] = '\0'; cout << tempString; userString[0] = toupper(userString[0]); //first char is uppercase i = 1; //reset count to run through string again after first character while(userString[i] != '\0') { userString[i] = tolower(userString[i]); ++i; } } void sentence::displayCorrectedSentence() { cout << "\nWith no multiple spaces and only the first letter " << endl; cout << "capitalized the sentence reads as follows..." << endl << endl; cout << userString << endl << endl; }



LinkBack URL
About LinkBacks


