0

I'm working on an algebra application, here's the code

struct quotient
{
    int numerator;
    int denominator;
};

struct term
{
    struct quotient coefficient;
    char varname;
    struct quotient power;
};

struct function
{
    struct term* terms;
    char* operators;
    struct quotient coefficient;
    struct quotient power;
};

//Constructor Functions
struct quotient NewQuotient()
{
    struct quotient temp;
    printf("Enter the numerator\n");
    scanf("%d", &temp.numerator);
    printf("Enter the denominator\n");
    scanf("%d", &temp.denominator);
    return temp;
}

char NewVarname()
{
    char temp;
    printf("Enter the variable letter: \n");
    scanf("%c", &temp);
    return temp;
}

struct term NewTerm()
{
    //broken, won't let you enter a variable name, sets it to x by default until that's     resolved
    struct term temp;
    printf("Enter the coefficient: ");
    temp.coefficient = NewQuotient();
    printf("Enter the variable name: \n");
    temp.varname = NewVarname();
    temp.varname = 'x';
    printf("Enter the power: ");
    temp.power = NewQuotient();
    return temp;
}

void NewFunction(struct function* func, int size)
{
    //so far so good
    unsigned i;
    func->terms = (struct term*)calloc(size, sizeof(struct term));
    //loop to initialize each term
    for(i = 0; i < size; i++)
    {
        func->terms[i] = NewTerm();
    }
    return;
}

int main(){
    struct function fofx;
    NewFunction(&fofx, 2);
    DisplayFunction(&fofx, 2);
    DeleteFunction(&fofx);

    return 0;
}

Here is the output:

Enter the numerator:
1
Enter the denominator:
2
Enter the numerator:
3
Enter the denominator:
4
....

etc, until the end of the loop.

Half of the statements in NewTerm don't seem to be executing at all, but the program seems to successfully allocate and initialize a new function. Any help is very much appreciated, I am very confused about this. I didn't include the display and delete function, they work fine, but if they'd be helpful I can add them on here.

Aj_76
  • 39
  • 2
  • 12

3 Answers3

1

You're not giving the right size to calloc, that should be sizeof (struct term) rather than sizeof (int). This might be the problem, depending on the actual size of struct term and what value you have for size.

Regarding NewTerm not being called, that's probably because you're not calling it.

lxop
  • 7,596
  • 3
  • 27
  • 42
  • That was part of it, that was causing the core dump. The scanf statements are still behaving badly though. – Aj_76 Jul 19 '12 at 03:16
  • You're going to have to do better than "behaving badly" or "not working correctly" to get a useful answer about that – lxop Jul 19 '12 at 03:18
  • yes indeed, it was recompiled and run with the same input and produced the same output. – Aj_76 Jul 19 '12 at 04:13
  • So when you run it, it doesn't even output "Enter the coefficient"? What is your main function ? – lxop Jul 19 '12 at 04:19
1

When using scanf, you generally want to get the Return key as well as the number.

You have:

scanf("%d", &temp.numerator);

You really want:

scanf("%d\n", &temp.numerator);
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • When I run this myself, I find that `scanf("\n%d", %temp.numerator);` works, while putting the `\n` at the end doesn't. – lxop Jul 22 '12 at 21:35
  • Whichever way you do it, just be sure you capture ALL the characters the user types in. [Return] isn't captured by %d, but needs to captured somewhere/somehow. – abelenky Jul 22 '12 at 23:50
0

You should also use validation to make sure only numbers are entered or you'll get wacky results.

Luca
  • 443
  • 2
  • 8
  • 17