Thread: ascending triangles

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    ascending triangles

    i need to make a triangle like this
    ____*
    ___**
    __***
    _****
    *****
    _ indicates spaces
    heres what i have so far...when i compile it all it does is end the program...im quite stuck...im new to the programming thing..so any help will be appreicated
    #include <iostream>
    using namespace std;
    int main ()
    {
    int lines;
    int spaces;
    int dots ;
    cout << "How many lines do you want in the picture?\n";
    cin >> lines;
    for ( lines = 1; lines == 0; lines++){
    for (spaces = lines - 1; spaces <= 0; spaces-- ){
    cout << "_";
    for (dots = 1; dots <= lines; dots++ ){
    cout << '*';}
    cout << endl;}}
    return 0; }

    thank you

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    welcome to the boards, please read the rules and use code tags to format your code.

    your most evident problem lies in the first for loop:
    Code:
    for ( lines = 1; lines == 0; lines++)
    you are increasing 1 each time through the loop starting at 1, you will never get to 0....

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
    int lines;
    int spaces;
    int dots ;
    cout << "How many lines do you want in the picture?\n";
    cin >> lines;
    for ( lines ; lines == 0; lines--){
    	for (spaces = lines - 1; spaces <= 0; spaces-- ){
    	cout << "_";
    	for (dots = 1; dots <= lines; dots++ ){
    	cout << '*';}
    	cout << endl;}}
    return 0; }
    so i change that much and i need to use user in put for how many lines...but im not sure what is wrong with the code...no errors just wont do anything

  4. #4
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int
    main ( void ) {
       int asterixes = 1;
       int spaces = 5;
         
       while( spaces - asterixes > -1 ) {     
         for ( int i = 0; i < spaces - asterixes; ++i ) {
              cout << " ";
         }
         for ( int j = 0; j < asterixes; ++j ) {
              cout << "*";
         }   
         ++asterixes;
         cout << endl;
      }    
    }
    Maybe you can fix this to fit in your practice....
    -- Add Your Signature Here --

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    first of all use code tags to post code {code} //some code {/code}
    but instead of {} use [ & ]
    Quote Originally Posted by bluegoo06
    #include <iostream>
    using namespace std;
    int main ()
    {
    int lines;
    int spaces;
    int dots ;
    cout << "How many lines do you want in the picture?\n";
    cin >> lines;
    for ( lines = 1; lines == 0; lines++)
    {
    for (spaces = lines - 1; spaces <= 0; spaces-- )
    {
    cout << "_";
    for (dots = 1; dots <= lines; dots++ )
    {
    cout << '*';
    }
    cout << endl;
    }
    }
    return 0;
    }
    after you get the amount of lines you want from cin, don't change it. create a new int named 'i'. do for(i = 1; i <= lines; i++){}...
    your login for printing '_' and '*' is off....think about when and how many '_'s you need to print, and then when and how many '*'s to print

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    why is that italicised?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I try to edit it , although it's can running correctly now,
    but I am donn't master loop condition set like this ,
    somegurus, please give me some suggestion about
    Code:
    for(?;? ;?;){for(?;?;?).......}
    or give me some website contains some test like this.
    Thanks.


    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int lines;
        int spaces;
        int dots ;
        
        cout << "How many lines do you want in the picture?\n";
        cin >> lines;
        for (int n = 1; n < lines; n++)
        {
            for (spaces = n; spaces < lines ; spaces++ )  //this is my guss setting , not know why and how?
            {
                   cout << "_";
            }    
            for (dots = 1; dots < n+1; dots++ )
            {
                   cout << "*";
            }//end for dots
             cout << endl;
        }//end for lines
    
        system("pause");
        return 0; 
    }

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    for (int n = 1; n < lines; n++)
    above is similar to
    Code:
    int n = 1;
    while ( n < lines ) {
        // do something
    
        n++;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    for(initialize a variable (first time thru loop only) ; while a condition return true; change condition)

    for(i = 0; i <= x; i++) <<----- your basic for loop
    for loops are most commonly used when you know the number of times you want to go thru the loop. it says do the loop while i is less than or equal to x. after one iteration, make i closer to x. (assuming x > 0); master that and then try changing the statements in the for();

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    thank you all i finally got this to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2009, 10:13 AM
  2. Normals to triangles
    By disruptivetech in forum Game Programming
    Replies: 5
    Last Post: 04-30-2008, 07:32 PM
  3. Problem with ascending order Program
    By cashmerelc in forum C Programming
    Replies: 4
    Last Post: 09-21-2007, 05:36 PM
  4. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 07:14 PM
  5. 3 integers in ascending order-help
    By Allison23NY in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2001, 02:24 AM