-2

I have a strange problem, simple program, f.e.

main()
{
    int i=1;
    std::cout << i;
}

Results with

1% 

on screen. I can't get rid of that "%". Does anyone know what is going on? I am using g++ (GCC) 4.8.0 20130502 on Arch Linux.

johnchen902
  • 9,531
  • 1
  • 27
  • 69

2 Answers2

5

The '%' is not from the program - It is from the shell that you are running it from.

Try

std::cout << i << std::endl;
user123
  • 8,970
  • 2
  • 31
  • 52
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • (Removed the `;` in the middle of the statement) – user123 May 19 '13 at 11:04
  • @WayneUroda - just curious - why a +1 for `endl`? – user93353 May 19 '13 at 11:14
  • @user93353 - People tend to forget! – Ed Heal May 19 '13 at 11:15
  • We all have a "senior" moment once in a while! – Ed Heal May 19 '13 at 11:25
  • @user93353 I thought I knew (I thought it was "more correct" C++, also I thought it was more platform aware [linux uses \n for line endings, windows uses \r\n, some older crazy systems used \r...]) but after looking into it now, I'm not so sure. In this case it flushes the `cout` stream immediately whereas `'\n'` or `"\n"` doesn't. See this question: http://stackoverflow.com/questions/213907/c-stdendl-vs-n – Wayne Uroda May 20 '13 at 01:21
  • @WayneUroda - I know - that's why I asked. endl isn't more correct or more platform aware. – user93353 May 20 '13 at 02:47
  • @user93353 well as they say, 'you learn something new every day", so thanks for pointing that out. Still, I think I would prefer endl in my own code, just a style choice more than anything (unless there was some performance critical aspect). – Wayne Uroda May 20 '13 at 03:00
2

Is the % your shell prompt?

If so, change your cout line to

std::cout << i <<'\n';

Run echo $PS1 in your shell to see what's your prompt.

Your original program prints 1 & then the shell prints the % prompt.

user93353
  • 13,733
  • 8
  • 60
  • 122