1

i'm currently new to C++ and the whole programming thing and i have no prior knowledge of any programming language except C, which i'm still learning. I bought an E-book on amazon to learn C++ and i'm currently practicing some of the examples from the book on Dev C++ on Windows. I'm trying to output a space in between numbers from this simple code but i have no idea D:

#include <iostream>

using namespace std;
int main()
{
    int x;
    for (x=0; x<=5; x++)
    {
    cout << x;
    }
}

so, when i execute the above code, i get the output 012345. How do you add spaces in between the numbers so the output looks like this 0 1 2 3 4 5?

tried

cout << x << endl;

but it output results on the next line, i'm trying to output spaces in between the numbers. i tried \ backslash to add an escaping character

cout << x\t;

but i get an error [Error] ";" expected before 't'. So, what code should i type to output the spaces? Thanks!

Creeper
  • 39
  • 7
  • 4
    a "space" is a literal so you need to add `"` around it. Like this: `cout << x << " ";` – Hayt Aug 31 '16 at 11:02
  • While you're beginning, let's [get that `using namespace std;` out of the way](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). Welcome to SO, and have fun with C++ :) – Quentin Aug 31 '16 at 11:38
  • I think that next time you need search guide in web before you write here question. (it's very base question) – Elidor Aug 31 '16 at 11:44
  • i did actually, and i found a couple of answers related to the same question but i find it difficult to understand since i'm very new to programming and koga and a few others actually helped me understand better :) sorry if the same question had already been answered before. – Creeper Aug 31 '16 at 12:39

3 Answers3

2
#include <iostream>

using namespace std;
int main()
{
    int x;
    for (x=0; x<=5; x++)
    {
    cout << x << " ";  // Add a trailing whitespace
    }
}
Koga
  • 523
  • 4
  • 13
0

You need to let compiler know that you mean character. x\t is not valid c++. Use this instead:

cout << x << '\t'

xinaiz
  • 7,744
  • 6
  • 34
  • 78
  • Didn't OP ask for spaces and not tabs? – Koga Aug 31 '16 at 11:03
  • @Koga I fixed what he tried. Now, if he uses simple `' '`, then he won't get to know the what the `'\t'` is and does what. – xinaiz Aug 31 '16 at 11:05
  • i had that confused with C ^^ but its just like i wanted the code to output and that's useful. Thanks for the help :) – Creeper Aug 31 '16 at 11:13
0

you are between space that try this line:

cout << x <<" ";

but next number print new line that try this line:

cout << x <<"\n";
Bhadresh Kathiriya
  • 3,147
  • 2
  • 21
  • 41
  • so, basically endl; and \n does the same thing? – Creeper Aug 31 '16 at 11:23
  • 1
    So. endl and "\n" do not do the same thing. Endl prints "\n" and then flushes the output stream. You almost certainly don't want endl unless you require to bypass the buffering. – jcoder Aug 31 '16 at 11:39