So this program is supposed to take in a sentence and output it with correct spacing and capitalization.

-For example if I entered:

heY, How are You doing?

-the output should be:

Hey, how are you doing?

The program runs fine, I just cannot get it to output.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
 
char * RemoveSpaces(char * source);
 
string makeUpper (const string& s);

 
string makeLower (const string& s);
 
 
int main()
{
    string str;
    cout << "Enter a sentence to be corrected\n"
         << "followed by pressing Enter.\n";
    getline(cin, str);

	return 0;
}


 
char * RemoveSpaces(char * source)
{
    char * dest, * ret = dest = source;
    while( *(isspace(*source) ? dest : dest++) = *source++ );
 
	return ret;
}
 
 

string makeUpper(const string& s)
{
    string temp(s);
    char c = toupper('a');
    cout << c;
 
    for (int i = 0; i < s.length(); i++)
        temp[i] = toupper(s[i]);
 
    return temp;
}
 
string makeLower(const string& s)
{
    string temp(s);
    char c = tolower('A');
    cout << c;
 
    for (int i = 1; i < s.length(); i++)
        temp[i] = tolower(s[i]);
 
    return temp;
}

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?

Guess I may have not been all that clear. The problem is I am a little confused on how to get the program to print something. Like you mentioned I am pretty sure I have to call the functions. So my end goal is to have the program run like this:

Enter a sentence to be corrected
followed by pressing Enter.

the CAT does NOT like THAT.

Here is the correct version of your sentence:

The cat does not like that.

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.

Correct, I need help on "parsing" the string. I am not familiar with that term though.

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/

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?

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...

  1. 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.
  2. Do whatever capitalization you need to do on each individual word.
  3. 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.

  1. Create an empty string called "sentence".
  2. Extract "the".
  3. Turn it into "The".
  4. Add "The" to sentence.
  5. Extract "CAT".
  6. Change it to "cat".
  7. Add a space to the end of sentence.
  8. Add "cat" to sentence.
  9. Extract "does".
  10. Change it to "does". There's nothing to change
  11. Add a space to the end of sentence.
  12. Add "does" to sentence.
  13. Extract "NOT".
  14. Change it to "not".
  15. Add a space to the end of sentence.
  16. Add "not" to sentence.
  17. Etcetera.

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 meant word, not sentence, correct?.

>> 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.

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.

Here is the ORIGINAL question from my book. Not sure if this will clarify things. I am going to continue working on this problem tonight.


Write a program that will read in a sentence of up to 100 characters and output the sentence with spacing corrected and with letters corrected for capitalization. In other words, int the output sentence all strings of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Do not worry about proper names; if the first letter is changed to lowercase, that is acceptable. Treat a line break as if it were a blank space in the sense that a line break and any number of blanks are compressed to a single blank. Assume that the sentence ends with a period and contains no other periods.

Okay, I changed the removeSpace to a string like the other two. But now I have two problems I STILL have no idea how to actually get these strings to work in the main() and I cannot figure out how to write the removeSpace string. Here is what I have:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string removeSpace (const string& s);

string makeUpper (const string& s);
 
string makeLower (const string& s);
 
  
int main()
{
    string str;
    cout << "Enter a sentence to be corrected\n"
         << "followed by pressing Enter.\n";
    getline(cin, str);


    removeSpace(str);
    makeUpper(str);         //I have no idea how to call those strings to make them work.
    makeLower(str);



    cout << "Here is your sentence after it has been corrected:"
    cout << str;
  
    return 0;

}

string removeSpace(const string& s)
{
     string temp(s);
     char c = isspace('a');
     cout << c;

     if (isspace(' ');           //this is where I get all messed up
         
     return temp;    
}

string makeUpper(const string& s)

{

    string temp(s);
    char c = toupper('a');
    cout << c;

    for (int i = 0; i < s.length(); i++)
        temp[i] = toupper(s[i]);

    return temp;

}

  

string makeLower(const string& s)
{

    string temp(s);
    char c = tolower('A');
    cout << c;

    for (int i = 1; i < s.length(); i++)
        temp[i] = tolower(s[i]);

    return temp;

}

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.

I cannot figure out how if there is a space for the function to remove it.

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". :)

Vernon, I want the function removeSpace to read in a character and if it is a space then it will go to the next character if it is a letter it will continue looping, if it is another space it will delete that space and then restart the loop. I just cannot for the life of me get that down to a algorithm. I dont even know if that explains it correctly.

while not end-of-string
    read character
    if character not = SPACE
        store character
end while

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

After staring at my book for an hour I was able to come up with something new. Here is the new string I wrote, I really think I am over analyzing this. Anyways I cannot get this string below to work properly either. Thanks for all the help so far, I just must be having the ultimate brain fart.

string removeSpace(const string& s)

{
     string temp(s);
     
  
 
   for( int i  = 0; i < in->length(); ++i)
{
        if(isspace(in->at(i)))
{
        *in = in->erase(i,1);
}

}
    return temp
}

Where's "in" defined? What happened to "temp"? I still don't see how it can work even if you fix "in" and "temp". Seems like it only looks at one character, not two for the test. You only erase if this character is a space and the last character was a space.

After staring at my book for an hour I was able to come up with something new. Here is the new string I wrote, I really think I am over analyzing this. Anyways I cannot get this string below to work properly either. Thanks for all the help so far, I just must be having the ultimate brain fart.

No, you're not having a brain fart. You're posting questions, getting answers, and completely ignoring the answers.

I'm done here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.