Search code examples
cloopsif-statementnested-loopscs50

Why does my code replace all chars in my loop?


I am trying to make a a loop that would make a stair case or half a pyramid if you will. but whenever I print it all my # Chars get replaced by the $ chars returning solid rows of chars.

what Am I doing wrong in my (Int a) loop. How do I make it return something like this:

        #
       ##
      ###
     ####
    #####
   ######
  #######
 ########

instead of solid blocks like this:

$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void){
   int ask = 0;
   char line[9];

   while ( ask < 1 || ask > 8 ) {
      ask = get_int( "Height: ");
   }
   for (int i = 0 ; i < ask; i++)
   {
      for (int j = 0; j < ask; j++)
      {
        line[j] ='#';
         }
         for (int a = 0; a <= strlen(line); a++)
         {
            if (line[a] == '#'){
               line[a] = '$';
            }
         printf("%c", line[a]);
      }
      printf("\n");
   }
}

Solution

  • I have not done such an assignment already for a very long time.:)

    For starters there is no need to use an array to output the pattern.

    Your array is uninitialized. So using the function strlen invokes undefined behavior.

    And it is entirely unclear why there is present the symbol '$'.

    The program can be written without using an array and the magic numbers 9 and 8.

    Here you are.

    #include <stdio.h>
    #include <limits.h>
    
    int main( void )
    {
        const char c = '#';
    
        while ( 1 )
        {
            printf( "Enter the height (0 - exit): " );
    
            unsigned int n;
    
            if (scanf( "%u", &n ) != 1 || n == 0) break;
    
            if (INT_MAX < n) n = INT_MAX;
    
            putchar( '\n' );
    
            for (unsigned int i = 0; i < n; i++)
            {
                printf( "%*c", n - i, c );
                for (unsigned int j = 0; j < i; j++) putchar( c );
                putchar( '\n' );
            }
    
            putchar( '\n' );
        }
    }
    

    The program output might look like

    Enter the height (0 - exit): 10
    
             #
            ##
           ###
          ####
         #####
        ######
       #######
      ########
     #########
    ##########
    
    Enter the height (0 - exit): 9
    
            #
           ##
          ###
         ####
        #####
       ######
      #######
     ########
    #########
    
    Enter the height (0 - exit): 8
    
           #
          ##
         ###
        ####
       #####
      ######
     #######
    ########
    
    Enter the height (0 - exit): 7
    
          #
         ##
        ###
       ####
      #####
     ######
    #######
    
    Enter the height (0 - exit): 6
    
         #
        ##
       ###
      ####
     #####
    ######
    
    Enter the height (0 - exit): 5
    
        #
       ##
      ###
     ####
    #####
    
    Enter the height (0 - exit): 4
    
       #
      ##
     ###
    ####
    
    Enter the height (0 - exit): 3
    
      #
     ##
    ###
    
    Enter the height (0 - exit): 2
    
     #
    ##
    
    Enter the height (0 - exit): 1
    
    #
    
    Enter the height (0 - exit): 0
    

    As for your question

    Why does my code replace all chars in my loop?

    then what you do is what you get

    if (line[a] == '#'){
       line[a] = '$';
    }
    printf("%c", line[a]);