0

I wrote this program to build a number diamond. The issue is that when I compile the program, it throws the error

build2.c:(.text+0x5): undefined reference to `get_input'

collect2: error: ld returned 1 exit status

I've tried for hours to figure out what exactly the problem is (e.g. if there is a spelling mistake or something similar), but the function call looks identical. I have attempted to rename it, write it as both a prototype and as an implementation, and nothing seems to work. Is there an issue that I'm not seeing?

//Define prior to main

int is_valid(int);
int get_input(void);
void print_pattern(int);

//Main
int main(void){
        int diamond_size;
        //diamond_size = get_input();

//value from get imput method used for diamond size

        print_pattern(get_input());

        return 0;
}

void print_pattern(int size){
int length, num, i, j;

//beginning of new diamond

printf("\n");

//Define each integer to work in layout of diamond
//First for loop fans out

for(i=1; i  <= size; i += 2){
        length = size-i+1;
        num =  1;
        printf("%*s", length," ");
        for(j = 0; j < i; j++){
                printf("%d ", num);
                num++;
        }
        printf("\n");
}

//second for loop fans in

for(i=size-2; i >= 1; i -= 2){
        length = size-i+1;
        num =   1;
        printf("%*s", length," ");
          for(j = 0; j < i; j++){
                printf("%d ", num);
                num++;
        }
        printf("\n");
}




int is_valid(int value){
int rem;

//uses remainder to determine if it is odd or even; an even number will not have a reaminder in this case

rem = value % 2;

if (rem == 0){
printf("You've entered a even number. Please try again.\n");
return (0);
}

//greater than 9 cnd
if (value > 9){
printf("You have entered a number greater than 9. Please try again.\n");
return (0);
}

//less than 1 cnd

if (value < 1){
printf("You have entered a number less than 1. Please try again.\n");
return (0);
}

return (1);

}

int get_input()
{
int cont, number, valid;
cont = 1;
while (cont = 1)
{
        printf("Enter an odd number less than 9 and greater than 0 < ");
        scanf("%d", &number);
        valid = is_valid(number);
        if (valid == 1)
        {
        cont = 0;
        }

}
return number;
}
}
2
  • Fix your indenting - you will then see that print_pattern does not have enough closing braces } so the other functions are inside that. I am surprised it compiles. Commented Feb 21, 2017 at 3:57
  • 2
    Check your {} pairs - You've put get_input and is_valid inside print_pattern, they need to be outside it. - repl.it/FrDD/0 Commented Feb 21, 2017 at 3:58

2 Answers 2

2

You seem to have nested functions; this is (a) a non-standard GCC extension, and (b) I presume the scope of the nested get_input() function is the enclosing function, not the file scope. The solution is to move get_input() to file scope. At the end of print_pattern() add an extra }, and delete the final } at the end of the file.

Also, please format your code - most IDEs these days have options to tidy it up, and with correct indentation you may have seen your problem earlier.

Oh, and as a bonus bug fix, you also have in get_input():

while (cont = 1)

This will always be true - use this instead:

while (cont == 1)
Sign up to request clarification or add additional context in comments.

1 Comment

Okay. Wow. I can't believe I missed that. Thank you for that and the bonus bug! Much appreciated.
1

The function print_pattern is not terminated at proper place but instead at the very end of the file:

void print_pattern(int size){
 ...
... end of the loop
}

... more functions
...
... end of print_pattern
}

This results into defining nested functions instead of global level.

It's generally good habit to indent the blocks, in which case you would realized the mistake very quickly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.