Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ loops and questions

    Okay so iv not been programming for very long and I'm reading a learn cop book in 21 days and I came acros the loops section. The book doesn't give very good homework to do so I thought I'd try to make a little Christ's tree with *'s but I'm just trying to spit out one side of that Christmas tree with a loop or two. I'm really not so smart and need some help. I tryed using 2 for loops to get started but I get REALLY confused when I see nested loops and functions inside of others. Well I am completely new to this forum and I hope I can meet some helpful people who can teach me and me teach others anything helps!

    *
    **
    *** ect..

  • #2
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Learn cpp*

  • #3
    New Coder
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Can you post the code you've written so far?

  • #4
    New Coder
    Join Date
    May 2014
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Learning about loops is not so difficult. Try practicing and implement programs and know about how the loop works.

  • #5
    New Coder
    Join Date
    Feb 2014
    Location
    USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    #include<iostream.h>
    int main()
    {
       int i, j;
       for(i=1; i<=5; i++)
       {
           for(j=1; j<=5-i; j++)
              cout<<" ";
           for(j=1; j<=i; j++)
              cout<<j<<" ";
           cout<<"\n";
       }
       return 0;
    }
    Last edited by sage45; 06-18-2014 at 08:29 PM. Reason: Please use code blocks

  • #6
    New Coder
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tarquinhall View Post
    Code:
    #include<iostream.h>
    int main()
    {
       int i, j;
       for(i=1; i<=5; i++)
       {
           for(j=1; j<=5-i; j++)
              cout<<" ";
           for(j=1; j<=i; j++)
              cout<<j<<" ";
           cout<<"\n";
       }
       return 0;
    }
    <iostream.h>? You must be using an out of date compiler...

    Here's how I'd do this:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      std::cout << std::setfill('*');
      for (int i = 1; i < 5; ++i)
        std::cout << std::setw(i) << '*' << std::endl;
    }
    Microsoft MVP .NET Programming - (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer & Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •