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!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cstdio>

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.