0

so here is what i'm working with.

#include <iostream>

using namespace std;

int main()
{
    int i, h = -1;

    for (i = 0; i < 8; i++) {
        h = h + 1;
        cout << h << endl;
    } // while

    return 0;
} // main

I need my output to look like

1 2 3 4 5 6 7 

but I am getting

1
2
3
4
...

Is there anything besides endl you can use to print to the same line with spaces? thanks and sorry for the noob question. i'm am slowly learning c++.

hochl
  • 12,524
  • 10
  • 53
  • 87
user1291612
  • 21
  • 1
  • 1
  • 4
    Get a book about c++ and go through the samples. This way you'll learn c++ much faster. Read some manuals. Start here: http://www.cplusplus.com/reference/iostream/cout/, here: http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/ and here: http://www.cplusplus.com/reference/iostream/manipulators/endl/ – surfen Mar 25 '12 at 18:19
  • This question makes no sense, sorry. – Konrad Rudolph Mar 25 '12 at 18:23

3 Answers3

6

It sounds like you want to print every number with a space in between. If so then use an actual space instead of the end of line character

cout << h;
cout << ' ';

Then at the end of the loop explicitly add a new line

cout << endl;

Full Sample

  int i;
  for (i = 0; i < 8; i++)
  {
    h = h +1 ;
    cout << h << ' ';
  } 
  cout << endl;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

cout << h << " "; will do the trick

Yuuta
  • 414
  • 2
  • 13
0

endl is what prints the newline, so you should place that outside of the loop. Sample:

for (i = 0; i < 8; i++)
{
  h = h + 1;
  cout << h;
}
cout << endl;
herzbube
  • 13,158
  • 9
  • 45
  • 87