?using single printf statement to print 2 answers?

Status
Not open for further replies.

slugger

Banned
I am trying to print out two calculated values using a single printf statement, but am unable to do so.
The part of the program where I am trying to do this looks like this

Code:
printf ("The answers are %f and %f\n", floatpt1, floatpt2);
where the 2 float point values have alredy been claculated earlier.

However instead of taking both the values and printing them, in the output only the first %f is being replaced with the value assigned to floatpt1 whereas the other %f is printed as it is.

Can anybody please tell me how to do I ensure that the second %f is also replaced with the valued assigned to floatpt2 in the printf statement

Thanks
 

QwertyManiac

Commander in Chief
This is odd, cause this expression works fine here :?

What compiler are you using? And does the second float variable show up if you use a different printf statement to display it?

My test:
Code:
#include<stdio.h>

int main()
{
	float floatp1=7.0,floatp2=floatp1+9.0;
	printf("The answers are %f and %f\n", floatp1, floatp2);
	return 0;
}

Outputs fine as:
Code:
pasquales@pysnakums:~$ ./float
The answers are 7.000000 and 16.000000
 
OP
slugger

slugger

Banned
Strange thing happened.
Heres a small program that I used to try out the single printf, multiple output thing
Heres the original code

Code:
/*The program will calcuate the cost of an item that currently costs a user input amount 
in user input years time taking into account inflation, also input by the user and also 
estimate its cost same number of years back assuming the same rate of inflation*/
#include <stdio.h>
#include <math.h>
int main (void)
{
    float inflation, price;
    int years;
    printf("Enter the current price of the object\n");
    scanf("%f", &price);
    printf("Enter the predicted rate of inflation\n");
    scanf("%f", &inflation);
    printf("Enter the number of years of price projection\n");
    scanf("%d", &years);
    printf("Taking into account inflation of %f %, the item would cost $%f in %d years time. \n", inflation, (price*(pow((1+(inflation/100)), years))), years);
    printf("Taking into account inflation of %f %, the item would have cost $%f %d years back. \n", inflation, (price/(pow((1+(inflation/100)), years))), years);
    return 0;  
}

The output I got looked like this

*img140.imageshack.us/img140/7615/op1uy4.gif

So I broke up the printf statements to print out each answer (copy-pasted the calculations, made no changes at all)
Code:
/*The program will calcuate the cost of an item that currently costs a user input amount
 in user input years time taking into account inflation, also input by the user and also 
 estimate its cost same number of years back assuming the same rate of inflation*/
#include <stdio.h>
#include <math.h>
int main (void)
{
    float inflation, price;
    int years;
    printf("Enter the current price of the object\n");
    scanf("%f", &price);
    printf("Enter the predicted rate of inflation\n");
    scanf("%f", &inflation);
    printf("Enter the number of years of price projection \n");
    scanf("%d", &years);
    printf("Taking into account inflation of %f %, ", inflation);
    printf("the item would cost $%f ", (price*(pow((1+(inflation/100)), years))));
    printf("in %d years time.\n", years);
    printf("Taking into account inflation of %f %, ", inflation);
    printf("the item would have cost $%f ", (price/(pow((1+(inflation/100)), years))));
    printf("%d years back. \n", years);
    return 0;  
}
The output I got now was this
*img219.imageshack.us/img219/493/op2my5.gif

By just putting extra printf statements, the answer was displayed correctly

I have been using the Borland C++ 5.5 compiler with SciTE Editor for editing and compiling the code.
 
Last edited:

ilugd

Beware of the innocent
^^ hey shubankarz... howdee.. ROFL. :)
You missed the second path in the first image. :)
 

QwertyManiac

Commander in Chief
Appears to me as a compiler issue, cause I just compiled your first program and got what you want, with the second %f getting evaluated successfully.

Code:
pasquales@pysnakums:~$ ./inflation
Enter the current price of the object
31212
Enter the predicted rate of inflation
3
Enter the number of years of price projection
34
Taking into account inflation of 3.000000 %, the item would cost $85268.228083 in 34 years time. 
Taking into account inflation of 3.000000 %, the item would have cost $11424.993411 34 years back.
I'm using the GNU C Compiler.

Try adding an escape for the % after the %f you have there.
I mean like this:
Code:
"into account inflation of %f [B]\%[/B], the item"
 
OP
slugger

slugger

Banned
Problem solved guys!!!

it appears that my compile can't handle % symbols if used as a character in the printf statement :mad:

thru trial and error, i narrowed it down to this and deleated the % symbol
so the code that is now runnin properly is this

Code:
/*The program will calcuate the cost of an item that currently costs a user input amount
in user input years time taking into account inflation, also input by the user and also
estimate its cost same number of years back assuming the same rate of inflation*/
#include <stdio.h>
#include <math.h>
int main (void)
{
    float inflation, price;
    int years;
    printf("Enter the current price of the object\n");
    scanf("%f", &price);
    printf("Enter the predicted rate of inflation\n");
    scanf("%f", &inflation);
    printf("Enter the number of years of price projection\n");
    scanf("%d", &years);
    printf("Taking into account inflation of %f, the item would cost $%f in %d years time.\n", inflation, (price*(pow((1+(inflation/100)), years))), years);
    printf("Taking into account inflation of %f, the item would have cost $%f %d years back.\n", inflation, (price/(pow((1+(inflation/100)), years))), years);
    return 0; 
}

The output looks like this

*img509.imageshack.us/img509/8574/trialja8.gif


Notice the absence of the % after the inflation number in the output

Compiled the program in Bloodshed also. It simply ignores the % symbol and proceeds with the whole thing

Thanks a lot guyz
 
Status
Not open for further replies.
Top Bottom