I have been trying to figure out this error for almost an hour and am having trouble. I was reading this post: C compile error: Id returned 1 exit status and someone said that it could be because the program is still running. I have checked that and it is not running and I still get the error.
This is what the error looks like: C:\Program\collect2.exe [Error] ld returned 1 exit status.
I also looked at some more posts with answers such as this one: Id returned 1 exit status error in my C program and the top response was that it was a "linker error" and that the program is getting complied incorrectly. I tried creating a new project and copying the code but end up with the same error. I then went into the compiler options and I took a screenshot of the options. Do these options look okay?
Maybe its just an error in my code I am overlooking. I have to create a tail recursion function to add numbers. For example the user enters 4 and the function adds 4+3+2+1 to get 10.
I believe the error is in this piece of code in the while loop:
else{
printf("Answer = %d \n", addNum(num));
}
When I comment that out I can run it and enter negative numbers or zero and get the output.If my program is off as in the function wont output correctly I do not want you to correct it. I will tweak that stuff later and figure the answer out for myself, I am just looking for why I am getting this error.
#include <stdio.h>
#include <stdlib.h>
int addNum(int n);
int main(int argc, char *argv[]) {
int num;
int count = 1;
printf("Enter an integer greater than zero, (q to quit): \n");
while(count < 3){
printf("Enter a positive number: \n");
scanf("%d", &num);
if(num < 0){
printf("Enter a positive number: \n");
}
else if(num == 0){
printf("Answer = 0 \n");
}
else{
printf("Answer = %d \n", addNum(num));
}
}
int addNum(int n){
int answer;
if(n > 0){
answer = n += addNum(n - 1);
}
return answer;
}
return 0;
}