446,305 Members | 1,628 Online
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

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

 P: 20 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 Replies

 P: 9 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

 Expert 2.5K+ P: 3,652 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

 P: 20 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         for(i=1;i<=5;i++)         {cout<<"    ";             for(j=1;j<=5-i;j++)             cout<<' ';                 for(j=1;j<=i;j++)                 cout<<" *";   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

 P: 20 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

 Expert 2.5K+ P: 3,652 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 misunderstanding 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

 P: 20 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 misunderstanding 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

 Expert 2.5K+ P: 3,652 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

 Expert 100+ P: 648 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

 P: 7 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