0

I am unable to compile my file and get many errors.

INPUT

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

int main(void)

//Prompt for the height of the pyramid
    int height;
    height = get_int ();
    printf("Pick a positive integer between 1 and 8.\n");
    
    do
    {
        //Takes input, assigns it to height and prints it
        height = GetInt();
        printf("Height: %d\n", height);
    }
    while (height<1 || height>8);

OUTPUT

~/ $ CD ~/pset1/mario/mario.c
bash: CD: command not found
~/ $ cd ~/pset1/mario
~/pset1/mario/ $ make mario
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    mario.c  -lcrypt -lcs50 -lm -o mario
mario.c:4:15: error: expected ';' after top level declarator
int main(void)
              ^
              ;
mario.c:8:5: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
    height = get_int ();
    ^
mario.c:8:23: error: too few arguments to function call, at least argument 'format' must be specified
    height = get_int ();
             ~~~~~~~  ^
/usr/include/cs50.h:82:1: note: 'get_int' declared here
int get_int(const char *format, ...) __attribute__((format(printf, 1, 2)));
^
mario.c:9:12: error: expected parameter declarator
    printf("Pick a positive integer between 1 and 8.\n");
           ^
mario.c:9:12: error: expected ')'
mario.c:9:11: note: to match this '('
    printf("Pick a positive integer between 1 and 8.\n");
          ^
mario.c:9:5: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
    printf("Pick a positive integer between 1 and 8.\n");
    ^
mario.c:9:5: error: conflicting types for 'printf'
/usr/include/stdio.h:318:12: note: previous declaration is here
extern int printf (const char *__restrict __format, ...);
           ^
mario.c:11:5: error: expected identifier or '('
    do
    ^
mario.c:17:5: error: expected identifier or '('
    while (height<1 || height>8);
    ^
9 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
~/pset1/mario/ $ 
  • Hi Niha, I see Stensal already sent you fixed code. I want to elaborate a bit on his answer to help you understand what you need. Main is a function, and as such, must always begin and end with curly braces { }. Secondly, get_int() is a function and one which you want to use to communicate with the user and ultimately store the desired height. Stensal moved this line to within the do-while loop because you want to be sure that if user inputs something outside the bounds of 1 and 8, the question is repeated, and repeated until the user inputs an appropriate value. – Chris Jun 23 at 18:57
  • There were a few things wrong with my code however, I managed to fix them I think. – Niha Rika Jun 28 at 18:46
1

I fixed it for you.

#include <cs50.h>
int main(void) {
//Prompt for the height of the pyramid
    int height;
    do
    {
        //Takes input, assigns it to height and prints it
        height = get_int("Pick a positive integer between 1 and 8:");
        printf("Height: %d\n", height);
    }
    while (height<1 || height>8);
    return 0;
}
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .