Brandon Nguyen
Brandon Nguyen

Reputation: 21

How to Remove the Ending Space of the Output of this Code

Output ends in a sentence and then a final space at the end that I need removed. Help please! I'm stumped! I know it's in the cout at the end, but if I remove that final space, then the entire output sentence comes without spaces. It's my first time doing C++ so I'm a total beginner at this and literally have no idea what to do right now.

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<iterator>
#include<map>

using namespace std;

map<string, string> myMap;

int contains(string str) {
     map<string,string>::iterator it = myMap.find(str);
     if(it != myMap.end())
     {
     return 1;
     }
     return 0;
     }

 int main() {

     myMap["BFF"] = "best friend forever";
     myMap["IDK"] = "I don't know";
     myMap["JK"] = "just kidding";
     myMap["TMI"] = "too much information";
     myMap["TTYL"] = "talk to you later";
     myMap["IDK."] = "I don't know.";
     myMap["TTYL."] = "talk to you later.";
     myMap["BFF,"] = "best friend forever,";
     myMap["JK,"] = "just kidding,";
     myMap["JK."] = "just kidding.";
     myMap["TMI!"] = "too much information!";
     myMap["TMI."] = "too much information.";


    string text;

    cout << "Enter text: ";
    getline(cin, text);
    cout << "You entered: " << text << endl;
    cout << "Expanded: ";

 stringstream ss(text);
 string item;
 while (getline(ss, item, ' ')) {
     if(contains(item) == 1) {
         cout << myMap[item] << " ";
    } 
    else {
         cout << item << " ";
     }

}


cout << endl;


}

Upvotes: 2

Views: 1642

Answers (3)

Bob__
Bob__

Reputation: 12759

An alternative is to use a std::string as a temporary buffer and remove the last char (the extra space) before printing it.

You may also avoid to declare the map as a global variable and pass it as a parameter to the contains function. It's a design choice, I don't know how much of your actual code depends on myMap to be a global.

That's how you can change your code:

#include<iostream>
#include<string>
#include<sstream>
#include<map>

using std::cout;
using std::endl;
using std::string;
using std::map;

bool contains( map<string, string> m, string str) {
    return  m.find(str) != m.end();
}

int main() {
    map<string, string> myMap{
        {"BFF", "best friend forever"},
        {"IDK", "I don't know"},
        {"JK", "just kidding"},
        {"TMI", "too much information"},
        {"TTYL", "talk to you later"},
        {"IDK.", "I don't know."},
        {"TTYL.", "talk to you later."},
        {"BFF,", "best friend forever,"},
        {"JK,", "just kidding,"},
        {"JK.", "just kidding."},
        {"TMI!", "too much information!"},
        {"TMI.", "too much information."},
    };

    string text;

    cout << "Enter text: ";
    getline(std::cin, text);
    cout << "You entered: " << text << endl;
    cout << "Expanded: ";

    std::stringstream ss(text);
    string item;
    string outs;
    while ( ss >> item ) {             // spaces are delimiters for operator>>
        if( contains(myMap, item) ) {
            outs += myMap[item];
        } 
        else {
            outs += item;
        }
        // or, if you know the conditional operator:
        //                outs += contains(myMap, item) ? myMap[item] : item;

        outs += ' ';
    }
    outs.pop_back();                   // removes the last char
    cout << outs << endl;

    return 0;                          
}

Please note that you are searching for item in the map twice, you can avoid that by doing so:

while ( ss >> item ) {
    map<string, string>::iterator it = myMap.find(item);
    outs += (it != myMap.end() ) ? it->second : item;
    ...

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

Just make it a prefix, rather than suffix, space:

cout << "Expanded:";               // Removed trailing space here

stringstream ss(text);
string item;
while (getline(ss, item, ' ')) {
    cout << ' ';                   // now adding space FIRST
    if (contains(item) == 1)
       cout << myMap[item];
    else
       cout << item;
}

Now, there is no space at the end of the line (whether you had items or not), but each item (if you do have some) is still separated by a space.

Upvotes: 1

hacoo
hacoo

Reputation: 121

I'd solve this by printing the space BEFORE the item. E.g.:

cout << " " << item;

Problem is, you'll now have an extra space after "Expanded: ". You could easily solve this by adding an 'if' statement, that prints the first thing without a space, before you kick off the while loop:

      if(getline(ss, item, ' ')) {
        if(contains(item) == 1) {
          cout << myMap[item];
        } 
        else {
          cout << item;
        } 
      }
      while(getline....)

Or, you could just delete the trailing space from where you print "Expanded: " :)

Upvotes: 1

Related Questions