0

I'm working on an algebra application, very similar to what graphing calculators can do.

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()
{
    struct term temp;
    printf("Enter the coefficient: ");
    temp.coefficient = NewQuotient();
    printf("Enter the variable name: \n");
    temp.varname = NewVarname();
    printf("Enter the power: ");
    temp.power = NewQuotient();
    return temp;
}

The program gets the quotients for the coefficiant and powers just fine, but there's a problem with getting the variable name. I'm thinking there's a null character stuck in the buffer after the scanf statements in NewQuotient but if there are are, I don't know how to find them or how to fix them. Any help is appreciated.

5
  • 4
    Shouldn't scanf("%c", temp.varname); be scanf("%c", &(temp.varname)); ?
    – Mahesh
    Commented Jul 15, 2012 at 16:13
  • Why are you using gets() in one place and scanf() in another? Beyond that, it might help if you were to highlight the parts that are your input so they're easier to distinguish. Commented Jul 15, 2012 at 16:24
  • @Aj_76 I formatted the question according to how i understand it; please revert and/or clarify if i didn't guess right.
    – anatolyg
    Commented Jul 15, 2012 at 16:41
  • 1
    If i use printf("%s", entry), it ends up dumping the core. Thanks anatolyg, I forgot to make the input/output clear. The important thing is that the program prompts for input for the power but the user doesn't have the chance to input anything
    – Aj_76
    Commented Jul 15, 2012 at 17:05
  • do not use gets() or scanf(). use fgets(). Your application may not be altered like this, but its a good practice. It will save you from all the future buffer overflow bugs and hair tearing.
    – Aftnix
    Commented Jul 15, 2012 at 18:27

1 Answer 1

2

In general, scanf doesn't go well with gets. It's not easy to use both in the same program. In your case, scanf reads exactly one character (x), while the user inputs 2 characters - x and end-of-line.

The end-of-line character remains in the input buffer, causing the following. gets reads the input until the nearest end-of-line character, which in your case appears to arrive immediately, even while the user doesn't have time to input anything.

To fix this, do all your input with either gets or scanf:


First option

struct term NewTerm()
{
    ....
    // Old code:
    // scanf("%c", &temp.varname);

    // New code, using gets:
    char entry[MAX];
    gets(entry);
    temp.varname = entry[0];
    ....
}

Second option

struct quotient NewQuotient()
{
    ....
    // Old code
    // gets(entry);

    // New code, using scanf:
    int x, y;
    scanf("%d/%d", &x, &y);
    ....
}

BTW if you choose the first option, you should use fgets instead of gets.

1
  • The execution problems are still the same even after changing it all to printf
    – Aj_76
    Commented Jul 18, 2012 at 19:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.