473,946 Members | 2,515 Online

# What does this line of code do: cout<<' '

20 New Member
I'm going through a book by JB Dixit on C++. One of the examples has a line of code that I can't understand. The program constructs a pyramid of asterisks. Here is the for loop:

for(i=1;i<=5;i+ +)
{
Feb 21 '07 #1
9 2873
squills
9 New Member
I'm going through a book by JB Dixit on C++. One of the examples has a line of code that I can't understand. The program constructs a pyramid of asterisks. Here is the for loop:

for(i=1;i<=5;i+ +)
{
it keeps adding 1 to i till i is equal t 5

so i =1
i = 2
i = 3
i = 4
i = 5

then the for loop is done..
Feb 21 '07 #2
Ganon11
3,652 Recognized Expert Specialist
If you were talking about the cout<<' ' in your thread title, this statement outputs a space. This is probably to help the asterisk pyramid center itself - the spaces in front will move the asterisks forward.
Feb 21 '07 #3
shk253
20 New Member
I meant the following: Sorry.

I'm going through a book by JB Dixit on C++. One of the examples has a line of code I can't understand (the 4th line in the following snippet). The program constructs a symmetrical pyramid of asterisks. Here is the for loop:

Expand|Select|Wrap|Line Numbers
1.         for(i=1;i<=5;i++)
2.         {cout<<"    ";
3.             for(j=1;j<=5-i;j++)
4.             cout<<' ';
5.                 for(j=1;j<=i;j++)
6.                 cout<<" *";
7.
I can't understand what category the 4th line of code falls under, and why it works. There is a single space between ' and '. The program gives an error code without it and the pyramid messes up if you add more spaces between them.

I hope I'm being clear. I just started taking a C++ class and have not yet come across something like this. Maybe if you could give me a keyword to search for on the internet and learn more about?

ps. I hope the format of the pasted code is not too horrible.

Thanks.
Feb 21 '07 #4
shk253
20 New Member
If you were talking about the cout<<' ' in your thread title, this statement outputs a space. This is probably to help the asterisk pyramid center itself - the spaces in front will move the asterisks forward.
Thanks Ganon. Do you know how I can read up on this category of, I guess "functions" ? (if thats the correct word for it)
Feb 21 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Functions are a big topic of beginning C++ programming. Your book will probably cover functions fairly soon - probably right after loops. I'm not sure what exactly you're misunderstandin g here, or I'd give you a better explanation. Are you confused with the single quotes ' ' or the cout<< or both or none?
Feb 21 '07 #6
shk253
20 New Member
Functions are a big topic of beginning C++ programming. Your book will probably cover functions fairly soon - probably right after loops. I'm not sure what exactly you're misunderstandin g here, or I'd give you a better explanation. Are you confused with the single quotes ' ' or the cout<< or both or none?
Yeah, I'm a bit confused by the single quotes but if it comes soon after loops, I'll search my textbook for it. Thanks.
Feb 21 '07 #7
Ganon11
3,652 Recognized Expert Specialist
The single quotes aren't involved with functions - they are used to separate strings from characters. Anything enclosed in double quotes "such as this" is a string. Even a single character is technically a string if it is enclosed in double quotes, so "c" is a string. But single-quotes are used for single characters to be interpreted as characters, so 'c' is a character, and 'this' will not work correctly. The two single quotes enclose a space, and cout will output that space for every execution of the loop.
Feb 21 '07 #8
markmcgookin
648 Recognized Expert Contributor
I'm going through a book by JB Dixit on C++. One of the examples has a line of code that I can't understand. The program constructs a pyramid of asterisks. Here is the for loop:

for(i=1;i<=5;i+ +)
{
hey buddy,

It's a simple loop.

i is now bound to the value of 1
for each step (that is increasing i by 1, i++) while i is less than 5
do ... whatever was after that line of code .. once (so 4 times in total, once for i=1, once for i=2 etc, but not for i=5 because then i is not less than 5)

Hope that helps
Feb 21 '07 #9
owestcot
7 New Member
If it would help you understand a little better, try replacing the space with a letter, and then the pyramid will be filled with that letter, rather than the spaces. Just gives a more visual understanding.
Feb 22 '07 #10