Problem with functions returning a value.

I was hoping someone could tell me where I went wrong with this code i'm doing for school, the error message that keeps poping up says "undefined reference to getX(); and i don't know where to begin..Thanks a lot!!!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include void welcome(); void displayEqn(float a, float b, float c); void displayResults(float X, float result); float getX(); int main() { welcome(); float termA, termB, termC, result, X; printf(" Please enter the coefficient for the degree 2 term: "); scanf("%g", &termA); printf(" Please enter the coefficient for the degree 1 term: "); scanf("%g", &termB); printf(" Please enter the coefficient for the degree 0 term: "); scanf("%g", &termC); displayEqn(termA, termB, termC); result = (termA*X*X) + (termB*X) + (termC); X = getX(); displayResults(X, result); return 0; } void welcome() { printf("You will be asked to enter the three!\n"); printf("coefficients for a degree 2 polynomial. The equation"); printf("will then be displayed and you will be given the"); printf("opportunity to apply the polynomial to a data value."); } void displayEqn(float termA,float termB,float termC) { printf("f(X) = %.2fX^2 + %.2fX + %.2f\n", termA, termB, termC); } void displayResults(float X, float result) { printf("f(%.2f) = %.2f", X, result); } float getX(float X) { printf("Please enter a value for X: "); scanf("%g", &X); return X; } ``````
float getX(float X) <-- remove the second "float X" and put it inside your function's { }.
Thanks! that definitely fixed the error message
Another thing: `scanf` returns a value, you should make use of it to see whether the `scanf` worked. Not doing so is a recipe for a segmentation fault, or trying to use garbage or non-initialised variables.

If the scanf reads only one variable, you can use an if statement - a return value of 1 evaluates to true, 0 is false. If there is more than one being read in, then a switch might be better.

Good luck !!
Thanks for the reply! But this assignment is based on certain guidelines that the teacher has given.. and we're not at if statements yet lol.

I've gotten a bit further in the code but im now getting an error message saying "undefined reference to readCoeffs(float&, float&, float&)"..

I've played around with it cause it seems like a similar error to the previous one but still nothing..Thanks again for all the help!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include void welcome(); void displayEqn(float a, float b, float c); void displayResults(float x, float result); float getX(); void readCoeffs(float &a, float &b, float &c); float calcResults(float a, float b, float c, float x); int main() { welcome(); float a, b, c, result, x; displayEqn(a, b, c); readCoeffs(a, b, c); x = getX(); result = calcResults(a, b, c, x); displayResults(x, result); return 0; } void welcome() { printf("You will be asked to enter the three \n"); printf("coefficients for a degree 2 polynomial. The equation\n"); printf("will then be displayed and you will be given the\n"); printf("opportunity to apply the polynomial to a data value.\n"); } void displayEqn(float a,float b, float c) { printf("f(X) = %.2fX^2 + %.2fX + %.2f\n", a, b, c); } float calcResults(float a, float b, float c, float x) { float result; result = (a*x*x) + (b*x) + (c); } void displayResults(float x, float result) { printf("f(%.2f) = %.2f", x, result); } float getX() { float x; printf("Please enter a value for X: "); scanf("%g", &x); return x; } void readCoeff(float &a, float &b, float &c) { printf(" Please enter the coefficient for the degree 2 term: "); scanf("%g", &a); printf(" Please enter the coefficient for the degree 1 term: "); scanf("%g", &b); printf(" Please enter the coefficient for the degree 0 term: "); scanf("%g", &c); }``````
Nevermind! I misplaced an s at the end of it
when i run the program, it seems good up until when it asks for a value for X..if i put it a 5 for X, the program says f(6.00) = 1.#R

Im not sure why this is happening, i've tried playing around with it for a while and can't seem to fix the problem..If anyone can please help with this, that would be awesome
Topic archived. No new replies allowed.