1

I am trying to figure out why I can't get this to run properly. I just want four inputs from the user, and run the calculation at the end.

#include <stdio.h>
#include <math.h>

int main(){
    double amount; /* amount on deposit */
    double principal; /* what's the principal */
    double rate; /* annual interest rate */
    int year; /* year placeholder and no. of total years */
    int yearNo;

    printf("What is the principal? ");
    scanf("%d", &principal);
    printf("What is the rate (in decimal)? ");
    scanf(" .2%d", &rate);
    printf("What is the principal? ");
    scanf(" %d", &principal);
    printf("How many years? ");
    scanf(" %d\n", yearNo);


    printf("%4s%21s\n", "Year", "Amount on deposit");

    /* calculate the amount on deposit for each of ten years */
    for (year = 1; year <= yearNo; year++){
        amount = principal * pow(1.0 + rate, year);
        printf("%4d%21.2f\n", year, amount);
    }
    return 0;
}

It properly asks for the principal and rate, but then skips over the question about Principal and asks for years. Then it just sits there waiting for a "ghost" entry?

I've been reading that the scanf() adds some whitespace when hitting enter but thought the space before the %d would fix that?

I also saw you could add do { c=getchar(); } while ( c != '\n'); after each scanf but that seems to crash the program (I added int c = 0; to the beginning too).

Thanks for any help or ideas!

EDIT:

When I change the erroneous format specifier from:

scanf(" .2%d", &rate);

to:

scanf(" %d", &rate);

I then get a crash after entering my values.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
user3718365
  • 515
  • 3
  • 6
  • 13
  • possible duplicate of [scanf Getting Skipped](http://stackoverflow.com/questions/14484431/scanf-getting-skipped) – Jonathon Reinhart Jan 23 '15 at 01:29
  • 2
    You're neglecting to check your return values. In C that's a capital offense. – John Zwinck Jan 23 '15 at 01:36
  • @JonathonReinhart - I tried the space before the %d and that isn't quite working. Well, it may have fixed the issue with new line, but now it's crashing after I input my four values. – user3718365 Jan 23 '15 at 01:44
  • @user3718365 Sounds like a different problem. – Jonathon Reinhart Jan 23 '15 at 02:08
  • 2
    @user3718365 A space before `"%d"` makes no difference on a successful scan into the `int`. All format specifiers consume leading white space expect 3: `"%c"`, `"%n"`, `"%["`. – chux - Reinstate Monica Jan 23 '15 at 05:25
  • 2
    @user3718365, it's not usually a good idea to change the question in such a way that it renders answers (or, in this case, parts of answers) incorrect. I've modified it to show a more "acceeptable" way, simply adding the extra information as an addendum. That way, answers still make sense and you still manage to get extra info in that you need. In any case, the crash is caused by using a non-pointer in the scanf, see my updated answer for details. – paxdiablo Jan 23 '15 at 06:40
  • 1
    @paxdiablo - thanks for that. I'll remember this for the future, I'm still getting a hang of Stackoverflow and the proper etiquette, so thanks! – user3718365 Jan 23 '15 at 07:45
  • 2
    The `scanf()` for `principal` is broken; `principal` is a `double` so the format should be `"%lf"`. See also [C — simple math argument not working](http://stackoverflow.com/questions/28102910/c-simple-math-argument-not-working/). – Jonathan Leffler Jan 23 '15 at 18:19

1 Answers1

5

.2%d is not a valid format string.

For a start, the % has to come first. In addition, if you're after a floating point value, d is not the right character - it's for integral values.

You should be using something like %f (you don't need width or precision modifiers).

On top of that, you've made a minor mistake of not using a pointer for one of your scanf calls:

scanf(" %d\n", yearNo);

That's probably going to cause a crash, and should be changed to:

scanf(" %d\n", &yearNo);

And, as a final suggestion, it's totally unnecessary to use whitespace before (or a newline after) %d or %f family of format specifiers. The scanner automatically skips whitespace before both of those.

So, the only two scanf format strings you need in this program are "%d" and "%lf" (f is for floats, lf is for doubles).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953