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

int main(){
  while (true){
  int n;
  string a;
  cin >> n;
  if (cin.eof())
    {break;}
  if (!cin) {
    cin.clear();
    cin >> a;
    cout << a;
  }
  cout << n;
  }
}

When i input 1 2 +. The output would be 12. but I want it to be 12+. Whats wrong with this code? it works if i input 1 2 a 5 b 7 but for some reason it doesnt work for +.

sfrehse
  • 1,062
  • 9
  • 22
Damon Li
  • 13
  • 3
  • 1
    Please provide some input data you want to process and failing scenario. – sfrehse Nov 11 '14 at 07:28
  • 2
    didnt i? I said I want to input 1 2 + but the output would always be 12 not 12+ which is what i want. – Damon Li Nov 11 '14 at 07:35
  • Sorry, I some sense yes. Do wanna parse a modified polish notation? – sfrehse Nov 11 '14 at 07:39
  • BTW you should not do `cout << n` in the case that `cin >> n` failed. It's be clearer to put `if ( cin >> n ) cout << n; else if ( cin.eof() ) break; else { cin.clear(); cin >> a; cout << a; }` – M.M Nov 11 '14 at 08:05
  • Oh yeah i added the else on cout << n after i posted the question in my code. – Damon Li Nov 11 '14 at 08:15
  • theres another question that i have. Why do i need to Control-D twice to end the program? Should just one Controld-D which is EOF end the program? or is there something wrong with my code – Damon Li Nov 11 '14 at 08:16

3 Answers3

1

What is happening is that cin >> n is consuming the +, because that could be part of a valid integer (e.g. +5); but then the integer input fails because it wasn't followed by a digit.

With stream input it cannot "look ahead", it has to make a decision on a character by character basis. This is why strtol, or std::stoi etc. are more reliable than reading an int via operator>>.

The C++14 text [facet.num.get.virtuals]/3 stage 2 specifies that the + should be consumed by the invalid read; however library implementations often don't exactly follow the standard with respect to reading numbers via operator>> because the standard is defective and changes a lot. So you may or may not find other compilers behaving differently.

To avoid this situation entirely I'd suggest taking a different approach; e.g. reading a string every time, and then making other checks such as std::stoi or otherwise to see if that string was an integer.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
-1

The plus sign can be parsed via char.

  char ch;
  cin >> ch;

and you will get the +-sign parsed.

Finally, you want probably get:

int a, b
char op;

cin >> a >> b >> op;

Afterwards you can reorder your output arbitrarily. E.g, std::cout << op << " " << a << " " << b

sfrehse
  • 1,062
  • 9
  • 22
  • That will fix this particular test-case; but the question implies that it should read a general sequence of integers and strings, not just `1 2 +`. – Mike Seymour Nov 11 '14 at 07:56
-1

change line 7 from

int n;

to

char n;

would solve the problem

seriousdk
  • 19
  • 3
  • You mean, read everything as single chars, rather than as integers and strings as the OP wants? How will that help? – Mike Seymour Nov 11 '14 at 07:54