Can't get what to output? No functions are called. You just read in a string and don't attempt to print anything or call any functions and the program ends, so the program should run "fine" as you say, but not really do much. Is the question how to convert a string to the way you want it or how to get something to print?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I understand the desired end result. You need to parse the string into the desired string changing the capitalization and the spacing. That's the hard part. The easy part is printing. Do that with a cout statement.
Presumably you'll have a function called FixGrammar or whatever you want to call it...
string FixGrammar(string str)
{
return str;
}
After line 19, you would stick this...
string fixedGrammar = FixGrammar(str);
cout << fixedGrammar << endl;
There's the printing part. The hard part is the parsing and changing the string so that it fixes everything. I'm assuming that's the part you're stuck on, though the thread title suggests otherwise. If so, again, see the two lines above. That's all you need. You print a string with cout, whether it's inside or outside of main. The thread title ("Trying to output cin.getline") doesn't make much sense. There's input (that's the cin and getline part), which reads from the console and sticks it into the string str. Then there's output. That's the cout part above.
Anyway, if the problem is how to parse that string, I have ideas on that. Just confirm that that's the real problem please and not the "printing". Thanks.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Parsing basically means taking the string apart into its smaller components and doing something with them. In your case, you have some helper functions. The RemoveSpaces function is one that I tried out and seems to work just fine. Unfortunately I see no use for it in this particular task. Why? Well the last thing you want to do here is get rid of all the spaces. Then it's a big run-on sentence and you won't know where to separate them. Here's what I think you need to do. First, separate all of the words. So we change
heY, How are You doing?
into
heY,
How
are
You
doing?
Worry about capitalization a bit later in the task. There are at least two ways of doing it. One involves something called stringstreams. They are extremely handy. You may or may not have used them. In this particular case, they are ideal because they will use spaces as a delimiter (a "delimiter" is something that separates something. Spaces are the delimiters between words. They tell us when one word ends and one begins.). I could explain it, but it's faster just to do it and you'll understand better.
int main()
{
string str = "heY, How are YoU doIng?";
stringstream ss(str);
string word;
string sentence;
while(ss >> word)
{
// you won't actually display anything here. The line below just
// shows that the individual words have been isolated, so delete
// the cout statement below.
cout << word << endl;
// now do something with each word with the capitalization and
// append the changed word to the sentence variable. Add a space
// between each word too.
}
cout << sentence;
return 0;
}
The >> operator combined with a stringstream already separates the words based on spaces. Might as well use it.
Now just fix the capitalization of the single words and you're home free. Use the grammar rules. Just make everything lower case (you already have a function for that).
The other option is to go through the big string character by character and look for a space. When you find one, you know you have a word. Me, I like the stringstream approach. http://www.cplusplus.com/reference/iostream/stringstream/
Here's the string library too. There is the substr function that can be handy, as well as a variety of "find" functions. The point is that you replace all multiple spaces with ONE space, make everything lower case, then shove it all together to make the corrected sentence.
http://www.cplusplus.com/reference/string/string/
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
It must be auto correcting when I reply to these threads. The program is supposed to fix spaces between the sentences to make sure there is just one. So it needs to do two things auto correct capitalization and remove unnecessary spaces. Very sorry for not being 100% clear on that.
Could I still use what you have listed above plus the RemoveChar function?
You're 100% clear, or at least you were till you said this... "between the sentences". There's only one sentence, right? Your examples only have one sentence. You want a space between every WORD, correct? There are a whole bunch of ways to do this. I like stringstreams. Just a personal preference. You can use whatever you want that works. My suggestion, and it's only my suggestion and not the only way...Do whatever you need to do to divide the whole sentence into individual words and get rid of all the spaces while doing this. I like stringstreams for this. Again, not the only way.
Do whatever capitalization you need to do on each individual word.
Combine the words back together to form the sentence again. Stick a space between each word.
So for your example...
the CAT does NOT like THAT.
- Create an empty string called "sentence".
- Extract "the".
- Turn it into "The".
- Add "The" to sentence.
- Extract "CAT".
- Change it to "cat".
- Add a space to the end of sentence.
- Add "cat" to sentence.
- Extract "does".
- Change it to "does". There's nothing to change
- Add a space to the end of sentence.
- Add "does" to sentence.
- Extract "NOT".
- Change it to "not".
- Add a space to the end of sentence.
- Add "not" to sentence.
- Etcetera.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I don't understand the need to break the input into words. Just make sure the first letter is capitalized and set all other letters to lower case. Doing it word by word is meaningless and makes the job more complex.
Go through the sentence and fix capitalization.
Go through a second time and remove extra SPACEs.
So this program is supposed to take in a sentence and output it with correct spacing and capitalization.
The program is supposed to fix spaces between the sentences to make sure there is just one.
I assume in the second quote you meantword, not sentence, correct?.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>> Doing it word by word is meaningless and makes the job more complex.
Complex? Seems easy enough. Let the >> operator strip the extra spaces. You have to split up the words to get rid of the extra spaces. Where you fix the capitalization is largely irrelevant.
string str = "heY, How are YoU doIng?";
stringstream ss(str);
string word;
string sentence;
// fix the capitalization here
while(ss >> word)
{
// or fix the capitalization here. Well, I guess this may be the worst choice of the three, but still, it only adds one if statement
sentence += word;
sentence += " ";
}
// Or fix it here
// Now get rid of that last extra space here.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I thought the program was supposed to fix the spaces via programmer code, not C++ I/O idiosyncrasies. One learns more by doing it old-school.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
So I guess you've decided to do it the "old-school" way that Walt P suggests rather than take advantage of anything using the string library or stringstreams? That's fine, there's certainly real learning value in doing it that way that wouldn't happen the other ways, as Walt P points out. However, not knowing you and not knowing what the assignment parameters are, if any, I just threw out what I thought the easiest approach would be.
So if you're not going to do it that way, I guess you're going to take a string character by character and manipulate it. In that case, do the spacing first, get that right, then make everything lower case, then finally make the first character upper case.
That being the case, get rid of makeUpper. There's nothing in the above algorithm that requires it. Keep makeLower. It'll come in handy. Neither will do you any good till you work out the spacing, so do that first.
As far as removeSpace() goes, I suggest defining EXACTLY what you want this function to do and exactly what parameter it takes and what it returns. I can think of several wildly different functions for your problem called removeSpace and they all do different things based on a different approach to the problem. I'm not going to offer a solution without knowing precisely what you intend to accomplish with that function. The big question is whether you're clear what you want that function to do yourself?
It all boils down to the algorithm. If I've said it once, I've said it a million times. Work out the exact algorithm on paper before you even look at a computer. It will help you immensely when you code. The code will simply flow from the algorithm.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
You "erase" a character by shifting everything beyond that character one space left. To erase 'C' from "ABCDEFGH", you would move "DEFGH". You can do this either through a loop with 5 iterations ("DEFGH" having length 5) or you can use the memmove function from the cstring library. Both of these solutions only handle the moving part of it. You'll need to figure out whether / when / what needs to be moved. In your case, when you encounter a space, peek one character ahead. If that's also a space, move all the remaining characters left. And every time you move, you'll need to shorten the string. You can do this by keeping track of the position of the period, then at the very end, use string's "substr" command to delete everything after that period. That's one use of the string library, but only one. To me that's sufficiently "old school". :)
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
while not end-of-string
read character
if character not = SPACE
store character
end while
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Oh, poorly worded description caused a misread.
Set a SPACEflag to false (none seen)
while not end-of-string
read character
if character = SPACE (this char is a SPACE)
if SPACEflag is false (last character was not a SPACE)
SPACEflag = true (flag this space)
store character (store this SPACE)
endif
else
SPACEflag = false (it's not a SPACE)
store character
endif
end while
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944