lundi 29 janvier 2018

if else logic | pyramid error

I'm trying to build a c program that prints a right aligned pyramid ie:

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

I thought about approaching this using an if-else logic to test the user input. However, when I run my code it keeps giving me this error and also doesn't show the error when the input is not valid.

mario.c:25:2: else error: expected '}'
}
 ^
mario.c:7:1: note: to match this '{'
{

Example code below:

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


int main (void)
{
    int height;
    height = get_int("Enter a number between 0 and 23: "); // keep prompting user for valid input'

    if (height > 0 || height < 23) {
        for ( int levels= 1 ; levels <= height; levels++) {
            for (int spaces = height-levels; spaces > 0; spaces--){
                printf (" ");
            }
            for (int hash = 1; hash <= levels+1; hash++){
                printf ("#");
            }
            printf("\n");
        }
        else 
        {
            height = get_int("Please try again: ");
        }

    }
}

Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire