0

The program should get a few words (number is unknown) from the user as long as he continues entering text and then should print them down as a list.

Consider the user enters some words as:

Aang Kyoshi Shirin Farhad Parand Kamran  

The output should be:

[Aang, Kyoshi, Shirin, Farhad, Parand, Kamran]

I've write down this code:

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

int main()
{
    string current;
    int counter = 1;
    while (cin >> current)
    {
        if (counter == 1)
            cout << '[' << current << ", ";
        else
            cout << current << ", ";
        counter = counter + 1;
    }
    cout << ']' << endl;
    return 0;
}  

And the result is as:

enter image description here In line 14:

cout << current << ", ";  

What should I do to not print the last ,?

For line 17:

cout << ']' << endl;  

How the code will exit the while loop? It doesn't exit the loop with an Enter, Ctrl+Z or Ctrl+D and so the line 17 is not executed?!

Sepideh Abadpour
  • 2,550
  • 10
  • 52
  • 88
  • if the words are enter on the same line first read the line then for instance use a stringstream to extract the words – bruno May 25 '19 at 16:13
  • If you're using `Ctrl+Z` on Windows to end the standard input, you need to press `Enter` after that since `Ctrl+Z` is treated like a character which only gets processed after to enter a line. – eesiraed May 25 '19 at 16:58

4 Answers4

3

Do the while loop this way to print comma.

while (cin >> current)
{
    if (counter == 1)
        cout << '[' << current;
    else
        cout << ", " << current;
    counter = counter + 1;
}
Mathias Schmid
  • 431
  • 4
  • 7
  • You're not likely to ever see it, but the counter will roll back to 1 after a few trillion words. If you don't need the counter for something else, test an `isfirst` flag and set it to false when you've finished handling the first word. – user4581301 May 25 '19 at 16:00
  • @user4581301 trillion? More like a few billion :) – Aykhan Hagverdili May 25 '19 at 16:01
  • the counter can be replaced by a `bool useful = false;` and set to _true_ in the loop, and probably after the loop add `if (useful) cout << ']' << endl;` ? – bruno May 25 '19 at 16:01
  • @Ayxan Could be as few as 64k, still unlikely to be encountered in this use case, but the upper bound is unspecified, so I just went with a generic, comically large number. – user4581301 May 25 '19 at 16:04
3

Here's one implementation with no ifs, no flags, no branching:

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

int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::istringstream stream{line}; 
    std::string s;
    stream >> s;
    std::cout << "[ " << s;
    while (stream >> s)
    {
        std::cout << ", " << s;
    }
    std::cout << " ]";
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

you can use stringstream:

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

   istringstream iss(t);
   string word;
   iss >> word;
   cout << "[" << word ;
   while(iss >> word) {
      cout<< " ," <<word;
    }
    cout<<"]\n";
}

taking input of a string word by word

Oblivion
  • 7,176
  • 2
  • 14
  • 33
0

If you can use a specific word to end the process, like end for instance, this can work:

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

int main()
{
    string current, output;
    bool first = true;

    output = "[";
    while (cin >> current)
    {
        if(current != "end"){
            if(first){
                output += current;
                first = false;
            } else {
                output += ", " + current;
            }
        } else {
            break;
        }
    }
    output += "]";
    cout << output << endl;
    return 0;
}