-3

I'm trying to solve this question below:

Write code to read a list of song durations and song names from input. For each line of input, set the duration and name of newSong. Then add newSong to playlist. Input first receives a song duration, then the name of that song (which you can assume is only one word long).

Input example:

424 Time
383 Money
-1

This is the code that I used:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Song {
public:
    void SetDurationAndName(int songDuration, string songName) {
        duration = songDuration;
        name = songName;
    }

    void PrintSong() const {
        cout << duration << " - " << name << endl;
    }

    int GetDuration() const { return duration; }

    string GetName() const { return name; }

private:
    int duration;
    string name;
};

int main() {
    vector<Song> playlist;
    Song newSong;
    int songDuration;
    string songName;
    unsigned int i;

    cin >> songDuration;
    while (songDuration >= 0) {
     /* Solution is below */

        getline(cin, songName);
        newSong.SetDurationAndName(songDuration, songName);
        playlist.push_back(newSong);
           /* Solution is above */

         cin >> songDuration;
    }

    for (i = 0; i < playlist.size(); ++i) {
        newSong = playlist.at(i);
        newSong.PrintSong();
    }
    return 0;
}

This is the message I get when I try to run my code:

Message from running the code

Can someone please help me remove the extra space from the method? I don't know how to remove this space, I tried everything I know.

  • Suggestion: check this [question](https://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-c) – GAVD Sep 26 '22 at 02:28
  • Links rot and otherwise may not be accessible when needed. Don't use them for important information like a code example. – user4581301 Sep 26 '22 at 02:28
  • All questions here should have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Can you [edit] this question, removing and replacing all links and images with all relevant information as plain text? All code must meet all requirements of a [mre]. You'll find many other questions here, with a [mre], in plain text. Please use them as an example for how your question should look. – Sam Varshavchik Sep 26 '22 at 02:30
  • There are no tools in C++ streams to remove a character once written--in many cases it won't even be possible because the stream flushed and the data has been sent--so generally when you have an extra space in your output the best thing to do is not write it in the first place. – user4581301 Sep 26 '22 at 02:30
  • GDBOnline is a debugger. Use it to step through you code and see exactly how the space got there. Then stop doing that. – user4581301 Sep 26 '22 at 02:32
  • Any reason you are using getline inside the while loop? try substituting it with std::cin – Shriram Sep 26 '22 at 02:50
  • std::getline can contain spaces, while std::cin can not. [check out this answer](https://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin) – Shriram Sep 26 '22 at 03:04

1 Answers1

1

On this statement:

cin >> songDuration;

Reading stops when a non-digit character is encountered, such as the whitespace following the number. The whitespace is left in the input buffer for subsequent reading.

Then this statement:

getline(cin, songName);

Reads from the input buffer until a line break is encountered. As such, the whitespace that is present between the number and the name ends up in the front of the songName variable. That is the whitespace you are seeing in your output.

The solution is to ignore the whitespace before reading the songName.

You can use the std::ws I/O manipulator for that purpose, eg:

#include <iomanip>
...
getline(cin >> ws, songName);

However, the instructions clearly state the following about the song name:

the name of that song (which you can assume is only one word long)

So, you can alternatively use operator>> to read in the songName. It will ignore the leading whitespace for you:

cin >> songName;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770