Thread: help! I cant even get this simple program..

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    help! I cant even get this simple program..

    how would you do a program that gave this output.. okay so im really new to all this......
    the code is supposed to have 2 nested for loops.....


    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *


    -jbkt


    this is what i have, but as you can see does not workint main()
    {

    for (char stars = 0; stars < 10; stars++)
    cout << '*'<< endl;
    {
    for (char stars = 0; stars < ; stars -= 1)
    cout << '*';
    }


    return 0;
    }
    Last edited by jbkt; 11-08-2001 at 10:54 PM.

  2. #2
    Using two nested loops is an innefective way of doing this but as you apparently need it for homework you probably *have* to have it done this way... Okay them:

    Code:
    for(short rows = 0; rows < 10; rows ++)
    {
    
       for(short length = 0; length < rows; length ++)
       {
          printf("*");
       }
    
       printf("\n");
    }
    That looks like it would work.... Use cout if you'd like but i prefer printf.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Actually

    Originally posted by lightatdawn
    Code:
    for(short rows = 0; rows < 10; rows ++)
    {
    
       for(short length = 0; length < rows; length ++)
       {
          printf("*");
       }
    
       printf("\n");
    }
    Since he wants the longest string first, I think it should be:

    Code:
    for(short rows = 0; rows < 10; rows ++)
    {
    
       for(short length = 0; length < 10-rows; length ++)
       {
          printf("*");
       }
    
       printf("\n");
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    As an exercise, you may want to look again at this line and spot the errors...

    for (char stars = 0; stars < ; stars -= 1)

    ... finding those will help you understand for loops and a few other things.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Huh?

    Originally posted by adrianxw
    As an exercise, you may want to look again at this line and spot the errors...

    for (char stars = 0; stars < ; stars -= 1)

    ... finding those will help you understand for loops and a few other things.
    Well, first of all you don't have a condition how long the loop will last (stars < ).

    Secondly, I believe char is automatically unsigned when not specifically signed/unsigned so stars -= 1 will screw up the calculation (will probably give it the value 255 instead of -1).

    That's all, I believe... except that there is no code that is looped
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Errr, I meant he should, (the original poster), look at his program...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM