0

I need to take 1 2 + 3 4 - * as an input in c++, but couldn't figure out, and even the size is not given about how many chars.
Need to take it as input ans store it in a string without spaces.
Thanks in advance.

  • 1
    what have you tried so far? We can't help you if you don't show us your code. – bb1950328 May 31 '21 at 12:01
  • @bb1950328 I tried like ```string s; char ch; while(cin >> ch) s += ch;``` – Klaus Mickhelson May 31 '21 at 12:03
  • 1
    @KlausMickhelson What were the results when you executed that code? Just stating "couldn't figure out" does not indicate what the issue is. If you stated "here is my code, but the string has extra spaces" or "the string when printed shows ...", etc. then that is a more focused question. – PaulMcKenzie May 31 '21 at 12:13
  • @KlausMickhelson [Could not duplicate](http://coliru.stacked-crooked.com/a/867fe14d50d474bf). Please post a [mcve], or take the code you see at the link, and edit it so that you duplicate the problem you're seeing. – PaulMcKenzie May 31 '21 at 12:19
  • @PaulMcKenzie actually mine was infinate looping – Klaus Mickhelson May 31 '21 at 12:59

1 Answers1

0

Use the getline function (Source) and the remove_if function (Source):

#include <iostream>
#include <algorithm>
#include <cctype>

int main(){
    std::string line;

    while (std::getline(std::cin, line)) {
        line.erase(std::remove_if(line.begin(), line.end(), ::isspace));
        //do whatever you want with `line`, it has no spaces anymore
    }
}
bb1950328
  • 1,403
  • 11
  • 18