printf() problems

Ok I kinda feel bad for asking this, but I'm really having trouble. I have a project im working on and our teacher wants us to use printf, and I'm having some problems with it. When I compile I get like a thousand errors, mostly having to do with the numbers in the following code (8.2f). It also has too many arguments (according to the compiler).

Here's the code:
1
2
3
4
5
6
7
8
9
void Manager::printYearly(int yearsIn) {
    float yldFund = ((avgFund - startValue)/startValue) * 100;
    float yldBond = ((avgBond - startValue)/startValue) * 100;
    printf("-%*i -%*i -%*8.2f -%*8.2f -%*8.2f -%*3.1f%% -%*8.2f -%*8.2f -%*8.2f -%*3.2f%% -%*8.2f",
           10, yearsIn, 10, (int)(inflationRate*100), '%', 10, (float)avgFund, 10,
           (float)minValFund->getValue(), 10, (float)maxValFund->getValue(), 10,
           yldFund, 10, (float)avgBond, 10, (float)minValBond->getValue(),
           10, (float)maxValBond ->getValue(), 10, yldBond, 10, (float)avgBury);
}


This is the output I hope to achieve minus the dollar signs. Also all of those numbers are gonna be in columns that are ten chars wide.

1 3% $10524.50 $7893.38 $12103.18 10.0% $10185.00 $7638.75 $11712.75 5.0% $9700.00

can anyone see what's my problem.
I'd recomend printing each value across multiple printf's to avoid having so many arguments. If you didn't have to use printf then cout would work alot better in this case.
You also have too many arguments (23 following the format specifier, instead of 22).
If each column is always ten characters wide, why bother passing the width as arguments? You are trying to specify two different widths using two different methods. Pick one.

1
2
3
4
5
6
7
8
9
10
11
12
13
    printf( "%8i  %8i  %8.2f  %8.2f  %8.2f    %3.1f%%  %8.2f  %8.2f  %8.2f    %3.2f%%  %8.2f",
        yearsIn,
        (int)(inflationRate*100),
        (float)avgFund,
        (float)minValFund->getValue(),
        (float)maxValFund->getValue(),
        yldFund,
        (float)avgBond,
        (float)minValBond->getValue(),
        (float)maxValBond->getValue(),
        yldBond,
        avgBury
        );

Be careful with spaces, mind you.

Hope this helps.
Ok I just tried this, but im still getting errors.

source:
1
2
3
4
5
6
void Manager::printYearly(int yearsIn) {
    float yldFund = ((avgFund - startValue)/startValue) * 100;
    float yldBond = ((avgBond - startValue)/startValue) * 100;
    printf("-%*i", 10, yearsIn);
    printf("-%*3.2f%%", 10, inflationRate);
}

Warning:

D:\Andy\My Dro...|420|warning: unknown conversion type character '3' in format|
D:\Andy\My Dro...|420|warning: too many arguments for format|
Yes, because you cannot specify the width two ways. You must pick one.

printf( "-%10.2f%%", inflationRate );

printf( "-%*.2f%%", 10, inflationRate );

Make sure to read the documentation carefully.
http://linux.die.net/man/3/printf
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

[edit] BTW. Why did you not try the code I gave you?
Last edited on
Topic archived. No new replies allowed.