mardi 13 octobre 2020

how can I read line by line and split every line to words ignoring white space and empty lines

So I have a text like this:

Eins zwei     Polizei

Drei vier Grenadier
Fünf Sechs Alte Hex

And I want to read every word using istringstream and in one row print out all the words but for some reason some words get print out multiple times, can someone tell me why this happens and how should I do the above? ` My code:

    #include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {


string pom;
    for (std::string line; std::getline(std::cin, line);) {
       istringstream iss(line);
        while(iss){
         iss >> pom;
        cout << pom << " ";
        }
    }
    return 0;

}

my output: Eins zwei Polizei Polizei Polizei Polizei Drei vier Grenadier Grenadier Fⁿnf Sechs Alte Hex Hex

wanted output:Eins zwei Polizei Drei vier Grenadier Fⁿnf Sechs Alte Hex

Aucun commentaire:

Enregistrer un commentaire