0

trying to take 2 different space separated number input as a string, concatenate them and print final string. somehow the input always messes up...

#define SORT(a)                 sort(a.begin(),a.end())

using namespace std;

int main()
{

  int level, count = 0;

  cin >> level;

  string lx, ly, f;

  getline(cin, lx);

  getline(cin, ly);

  f = lx + " " + ly;

  SORT(f);

  int size = f.size();

  cout << f << endl << size << endl << level << endl;

  return 0;
}
Atom
  • 15
  • 6
  • Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – BessieTheCookie Jun 18 '20 at 18:54
  • Was that macro really necessary? – Asteroids With Wings Jun 18 '20 at 19:08
  • @BessieTheCow not exactly but sort of got the idea what i'm supposed to do – Atom Jun 18 '20 at 19:47
  • @AsteroidsWithWings i use a bunch of more macros this is just the relevant one – Atom Jun 18 '20 at 19:53
  • @Atom Why not use... none? – Asteroids With Wings Jun 18 '20 at 19:53
  • @AsteroidsWithWings learning competitive coding hence getting used to using macros – Atom Jun 18 '20 at 19:56
  • @Atom You'd really be better off learning real coding – Asteroids With Wings Jun 18 '20 at 19:56
  • I thought any form of coding would help in building my understanding of algos no? – Atom Jun 18 '20 at 20:15
  • Competitive programming is fine if you don't plan on doing any real-world software development in the near future. Just know that you'll have to unlearn a bunch of bad habits later on. Also, most people here are professional developers and not competitive programmers so they won't bother with reading cancer code. As a competitive programmer myself I would recommend you to not abuse macros and instead work on your typing speed. – BessieTheCookie Jun 18 '20 at 22:25
  • The problem is that when you do `cin >> level` it will leave the newline character in the input stream. This usually isn't the problem because when you use `cin >>` later it will ignore any leading whitespace, but `getline` works differently. It will see the newline and stop immediately, so you have to ignore the leading whitespace before you use `getline` by doing `std::cin >> std::ws`. – BessieTheCookie Jun 18 '20 at 22:28
  • @BessieTheCow what do i practice if i want to get into real-world software development later on? – Atom Jun 19 '20 at 05:35
  • Learn the language well from a [good book](https://stackoverflow.com/q/388242/9254539), practice stuff with projects, and maybe look into open source. I'm not really in a position to answer that well. – BessieTheCookie Jun 19 '20 at 22:39

1 Answers1

1

Why are you sorting them if all you want to do is combine them and print them?

Your initial code without the SORT() works perfectly.

I am not sure since I never use it, but I believe sort will sort all characters in the string alphabetically, so dfg acb will become abcdfg

orORorOR
  • 133
  • 7
  • I want to sort them and later use that string f, this is a small part of the program. try providing int as input in the strings – Atom Jun 18 '20 at 19:36
  • Then you're going to have to be a lot more specific about the problem than_" somehow the input always messes up..."_. What does "messes up" mean for you? – Asteroids With Wings Jun 18 '20 at 19:54
  • @AsteroidsWithWings the first element of my second input is skipped and the second input is ignored, hence the code gets messed up – Atom Jun 19 '20 at 05:38