C programming star count

This is a discussion on C programming star count within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> int DrawRow(int length) { int i, count=0; for(i = 1; i <= length; i++) { ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    C programming star count

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int DrawRow(int length)
    {
       int i, count=0;
       for(i = 1; i <= length; i++)
       {
          printf("*");
    
       }
       printf("\n");
       return count;
    }
    int DrawSquare(int length)
    {
         int j,i, count=0;
         for(j=1; j<=length; j++)
         {
           DrawRow(length);
           count++;
         }
         printf("\n");
         return count;
    }
    int DrawTriangle(int length)
    {
         int j,i,count=0; 
         for(j=1; j <=length; j++)
         {
            DrawRow(j);
            count++;
         }
         printf("\n");
         return count;
    }
    int DrawTriangles(int length)
    {
         int i,j,k,count=0; 
         for(k=1; k<=length; k++)
         {
           DrawTriangle(k);
           count=k*count;        
         }
         printf("\n");
         return count;
    }
    
    
    main()
    {
          int choice,x,count,total,total_stars;
        
          while(1)
          {
                   printf("enter a number greater than 0 here:\n", x);
                   scanf("%i",&x);  
                   
                   if (choice == 0) break;
                   
                   printf("choose 1(row of stars),2(square),3(triangle),4(triangles) or 0(quit)\n");
                   scanf("%i",&choice);
                  if (choice == 0) break;
                  if (choice == 1) total_stars=DrawRow (x);
                  if (choice == 2) total_stars=DrawSquare (x);
                  else if (choice ==3) total_stars=DrawTriangle (x);
                  else if (choice ==4) total_stars=DrawTriangles (x);
                  else if (choice >=5) break;
                  total_stars=x+total_stars;
          }
         printf("\n");
         printf("Numbers of stars are %i\n", total_stars);
          
    
    system("pause");      
    }



    I'm trying to have it count the total number of stars or * when i finish. So I would enter the number on the first line greater than 0, and then on the second line and it would produce a line, square or triangle, and then enter 0, and 0 again to end the loop. However i can only make it count certain number of stars. Any help on fixing the count so i can count all the * ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    {
       int i, count=0;
       for(i = 1; i <= length; i++)
       {
          printf("*");
    
       }
       printf("\n");
       return count;
    }
    'count' here is not actually used. You look like you've got the right idea though. Any time you have it spit out a star, increment your star counter.
    Code:
    int drawline( int length )
    {
        int stars = 0, x;
        for( x = 0; x < length; x++ )
        {
            spitoutastar( );
            stars++;
        }
        return stars;
    }

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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by quzah View Post
    Code:
    int drawline( int length )
    {
        int stars = 0, x;
        for( x = 0; x < length; x++ )
        {
            spitoutastar( );
            stars++;
        }
        return stars;
    }

    Quzah.
    I tested that out, but i would get an error undefined reference to spitoutstar.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    /facepalm

    That was just an example. You should replace that with whatever you plan on using to put a '*' on the screen.


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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    oh my bad, I'm new at this I just started C programming like a month ago.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 05:53 AM
  2. mandelbrot set program improvements
    By mad_muppet in forum Game Programming
    Replies: 3
    Last Post: 07-14-2010, 05:58 AM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  4. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  5. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21