0

I'm trying to write a program that prints out a christmas tree that looks like this:

enter image description here

The user inputs the height, in this example the height is 6. If the input is in range from 0 to 3, the height should be 3, because otherwise it's not printable, and if the input is less than 0, the program should terminate.

My code for some odd reason is infinitely printing the 'Input height'. Where is the error?

Here's my code snippet:

#include <stdio.h>
    
void main(){
    int i, j, n, s;

    while (1){

        printf("Input height: ");
        scanf("%d", &n);

        if (n < 0) break;

        if (n == 0 || n == 1 || n == 2 || n == 3)
            s == 3;
        else
            s == n;

        for (i = 0; i < s; i++){
            for (j = 0; j < 2*s - 1; j++){
                if (j > s - (i - 1) && j < (s + (i - 1)) - 1)
                    printf("*.");
                if (j == s + (i - 1))
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }

        for (j = 0; j < 2*s - 1; j++){
            if (j == s - 1 || j == s || j == s + 1)
                printf("*");
            else
                printf(" ");
        }
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Daxon
  • 77
  • 7
  • 1
    "Some reason" usually shows up in the code. Step through with a debugger! – tadman Dec 28 '22 at 15:36
  • why do you have the input for n in the loop? – OldProgrammer Dec 28 '22 at 15:37
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Dec 28 '22 at 15:38
  • There are two too many `=` signs in `if (n == 0 || n == 1 || n == 2 || n == 3) s == 3; else s == n;` — you should have `s = 3;` and `s = n`. If your compiler is warning about statements with no effect or something similar, heed it. If it isn't, turn up the warnings until it does, or get a better compiler. If you use GCC, use compilation options `-Werror -Wall -Wextra` as a starting point. You can add fussier options to that list if you like. – Jonathan Leffler Dec 28 '22 at 15:40
  • got [some warnings here](https://godbolt.org/z/sj9hjdr7x) that need fixing – yano Dec 28 '22 at 15:42
  • `if (n == 0 || n == 1 || n == 2 || n == 3)` is plain silly. You have already checked for `n < 0`, so all you need there is `n < 3`. You need not check for == 3 because that is dealt with by n = 3 in the else block. – Clifford Dec 28 '22 at 15:56
  • Okay i fixed the issues, but now it's the whole program that's an error. It's outputting something that i can't even fathom how or why, and i thought the logic behind what i wrote is good. I'm not used to debugging programs, i could do it with some equations and such, where i can monitor the variables, but i don't know if i can even monitor what's being printed in my program – Daxon Dec 28 '22 at 16:06

3 Answers3

1

The lines: s == 3; and s == n; do absolutely nothing.
== is a comparison, not an assignment.

Here is much better code:

#include <stdio.h>

int main(void) {
    int n = 8;
    
    char row[2*n];
    for( int i=0; i<2*n-1; i+=2 )
    {
        strcpy(row+i, "*.");
    }

    for(int i=0; i<n; ++i)
    {
        printf("%*.*s\n", n+i+1, 2*i+1, row);
    }
    printf("%*s\n", n+2, "***");
    
    return 0;
}

Result:

Success #stdin #stdout 0s 5464KB

        *
       *.*
      *.*.*
     *.*.*.*
    *.*.*.*.*
   *.*.*.*.*.*
  *.*.*.*.*.*.*
 *.*.*.*.*.*.*.*
       ***

With a little creativity, I made the program even shorter with only a single for-loop.

#include <stdio.h>

int main(void) {
    int n = 8;
    
    char row[2*n];
    strcpy(row, "*");
    for( int i=0; i<n; ++i )
    {
        printf("%*s\n", n+i, row);
        strcat(row, ".*");
    }
    printf("%*s\n", n+1, "***");
    
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • I wrote a comment above where i mentioned my correction of those mistakes. Thanks for this, but can you explain the code you wrote a bit further into detail? I'm not that used to working with string functions like strcpy, and your printf functions are kind of confusing – Daxon Dec 28 '22 at 16:11
0

As mentioned by others there are some issues with you mixing up == and =. I will be posting a version that prints out the christmas tree but leaves out the . that you also want to include, as you should be able to finish it yourself.

#include <stdio.h>

int main()
{
    int i, j, n, s;

    while (1)
    {
        printf("Input height: ");
        scanf("%d", &n);

        // if input is negative, exit
        if (n < 0)
        {
            break;
        }

        // if input is 0,1,2 or 3 change to 3
        if (n == 0 || n == 1 || n == 2 || n == 3)
        {
            s = 3;
        }
        else
        {
            s = n;
        }

        // loop through each row
        for (i = 0; i < s; i++)
        {
            // loop through each column
            for (j = 0; j < 2 * s - 1; j++)
            {
                // if column is within the tree print a star
                if (j >= s - i - 1 && j <= s + i - 1)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }

        // print base of tree
        for (j = 0; j < 2 * s - 2; j++)
        {
            // if column is part of base print star
            if (j == s - 2 || j == s - 1 || j == s)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}
0

A simple solution:

#include <stdio.h>

int main(){
    int i=0, j=0, n=0, s=0;

    while (1){

        printf("Input height: ");
        scanf("%d", &n);
        printf("\n");
        
        if (n < 0) break;
        s = (n <= 3) ? 3 : n;

        for (i=0; i < s; ++i){  // rows
            for (j=0; j < s-i; ++j) // white spaces
                printf(" ");
            
            for (int k=0; k < i; ++k) // *.
                printf("*.");

            printf("*\n"); // always, unique or last *
        }
        
        for (i=0; i < s-1 ; ++i)
            printf(" ");
        printf("***\n\n");
        
    }
    return 0;
}
DSalomon
  • 39
  • 1
  • 3