-3

For example: insert 1 5 88 99 7 in a set if 1,5,88,99,7 is given as an input and then pressed Enter. My code:

#include <bits/stdc++.h>
using namespace std;
set <char> num;
set <char> ::iterator i;
int main()
{
 int a;
 while(a=getchar())
 {

    if(a!='\n')
    {
        if(a!=',')
            num.insert(a);
    }
    else
        break;
}
for(i=num.begin(); i!=num.end(); i++)
    cout<<*i<<endl;
}

The output I'm getting: 1 5 7 8 9

2 Answers2

-2
#include <set>
#include <string>
#include <sstream>
using namespace std;
int main() {
   // cin >> s;
   string s = "5,2,3,4,1",t = "";
   stringstream ss(s);
   set<int> s_;
   while( getline(ss,t,',') )
       s_.insert(stoi(t));
   for(auto i : s_)
       cout << i << " ";
}

Outputs: 1 2 3 4 5

  • 4
    The concept is good, but the main sin of this answer, and maybe the reason for the downvote, is it makes no attempt to explain what you did and why. This is a code-only answer and sometimes they work, but most of the time they just lead to some poor, confused asker becoming a [Cargo Cult Programmer](https://en.wikipedia.org/wiki/Cargo_cult_programming) This answer is easily salvaged by adding even a brief explanation of the problem, why it's a problem, and how your code solves the problem.. – user4581301 Mar 15 '18 at 19:43
  • 2
    `cout << "1 2 3 4 5"` also outputs 1 2 3 4 5. <- this answer is just as useful.. Code without explanation is useless for questions like these. – JHBonarius Mar 15 '18 at 19:45
  • I omitted string s = "5,2,3,4,1" and took various inputs. It works. But I don't understand how getline(ss,t,',') is working. – user2143094 Mar 15 '18 at 19:48
  • @user2143094 [`std::getline` docs](http://en.cppreference.com/w/cpp/string/basic_string/getline). [`std::stringstream` docs](http://en.cppreference.com/w/cpp/io/basic_stringstream). Together they make for one of the most useful tools in the C++ string parsing arsenal. For example, [take a look at option 2 in this linked answer](https://stackoverflow.com/a/7868998/4581301). `getline` reads until it finds a delimiter, ',' in this case and places what it found into the given `string`. Typically you leave out the delimiter and `getline` assumes end of line. – user4581301 Mar 15 '18 at 19:59
  • 1
    And a bit of snark: I recommend against selecting an answer as THE answer if you do not understand the answer. If you don't know what was done and how, how can you be sure the answer actually solves the problem? This one does, but it needs more explanation for you to be able to apply it to your problem correctly. – user4581301 Mar 15 '18 at 20:03
-2

1) To match your modified question (reading integers of any length) char by char, means you need to look at each and see if it is a digit. If it is, you need to keep a running value based on each additional digit you receive.

2) Your set needs to be of int, not char if you're going to save the actual integer values.

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

set <int> num;
set <int> ::iterator i;
int main()
{
  int a;
  int full_num=0;
  while(a=getchar())
  {
     if (isdigit(a)) 
     {
       full_num = full_num*10 + (a - '0');
     } 
       else if (a==',' || a=='\n') 
     {
       num.insert(full_num);
       full_num = 0;
       if (a=='\n') break;
     }
   }
   for(i=num.begin(); i!=num.end(); i++)
       cout<<*i<<endl;
 }
J. Fay
  • 11
  • 2