Thread: Star Program not working?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Star Program not working?

    I don't know what I did wrong....thought it looked good. Please help.

    Code:
    #include <stdio.h> 
    
    int main ()
    {
    
    int star;
    int num;
    int a;
    int b;
    
    scanf("%d", &star);
    
    num = 0;
    while(num <= star){
     
    for (a=1; a<=star; a++)
    {
     printf("*");
    }
    printf("\n");
    star = star - 1;   
    num++;
    }
    return 0;
    }
    If I input 5 it outputs
    *****
    ****
    ***
    and then that is it.

    I need to know how come it won't output the last two
    **
    *

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your while loop has to go to a fixed number - star. Don't decrement star at all.
    In your for loop, you need to use another variable - not star. Maybe j or whatever.
    J is the variable you decrement after each for loop.

    It's also helpful if you work from 0 counters, rather than 1. Here, you start num with 0, but you start 'a' out at 1.

    You don't want to do that.

    Another tip, (use with the above), is to use just < or > as much as feasable. Not <= or >=. That decreases obo* errors, in general.

    *obo = off by one.
    Last edited by Adak; 01-19-2010 at 08:57 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    i am somewhat seeing what your saying. Could you give me an example....?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    nevermind, I just figured it out. Thank you Adak. Could someone explain how to do it in the opposite direction?

    ex. 4
    *
    **
    ***
    ****

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Count the other way.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure:

    Code:
      while (num < star) {
        for(i = 0; i < (star - num); i++) 
          putchar('*');
        putchar('\n');
        num++;
      }
    The quantity star - num removes the need for a separate variable.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Time for some fun.
    Code:
    #include<stdio.h>
    int main( void )
    {
        int star = 0;
        
        printf( "Enter a number from 1-9: " );
        fflush( stdout );
        while( scanf( "%d", &star ) != 1 || star > 9 || star < 1 )
            fgetc( stdin );
    
        do
        {
            switch( star )
            {
                case 9: printf( "*" );
                case 8: printf( "*" );
                case 7: printf( "*" );
                case 6: printf( "*" );
                case 5: printf( "*" );
                case 4: printf( "*" );
                case 3: printf( "*" );
                case 2: printf( "*" );
                case 1: printf( "*" );
                default: printf( "\n" );
            }
        }
        while( star-- );
    
        return 0;
    }
    And because you know you want to, someone make a recursive one.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    that's funny, i was wondering how about doing the recursive one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program stopping working
    By SterlingM in forum C++ Programming
    Replies: 24
    Last Post: 10-17-2009, 02:38 PM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM