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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shk253
    New Member
    • Feb 2007
    • 20

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

    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+ +)
    {
  • squills
    New Member
    • Feb 2007
    • 9

    #2
    Originally posted by shk253
    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..

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      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.

      Comment

      • shk253
        New Member
        • Feb 2007
        • 20

        #4
        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:

        Code:
                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.

        Comment

        • shk253
          New Member
          • Feb 2007
          • 20

          #5
          Originally posted by Ganon11
          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)

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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?

            Comment

            • shk253
              New Member
              • Feb 2007
              • 20

              #7
              Originally posted by Ganon11
              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.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                • markmcgookin
                  Recognized Expert Contributor
                  • Dec 2006
                  • 648

                  #9
                  Originally posted by shk253
                  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

                  Comment

                  • owestcot
                    New Member
                    • Feb 2007
                    • 7

                    #10
                    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.

                    Comment

                    Working...