 |
31-01-2008, 02:32 AM
|
#1 (permalink)
|
|
Banned
Join Date: May 2004
Location: Baudland
Posts: 2,433
|
?using single printf statement to print 2 answers?
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
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
31-01-2008, 06:35 AM
|
#2 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: ?using single printf statement to print 2 answers?
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
__________________
Harsh J
www.harshj.com
|
|
|
31-01-2008, 07:38 AM
|
#3 (permalink)
|
|
Host4Cheap.org
Join Date: May 2005
Location: Digit Forum
Posts: 2,102
|
Re: ?using single printf statement to print 2 answers?
Try adding space between
%f and \n
so it looks like
printf ("The answers are %f and %f \n", floatpt1, floatpt2);
__________________
★ Want to start your Website, No worries - here is how ★
http://www.thinkdigit.com/forum/showthread.php?t=66717
★ Host4Cheap - cPanel Webhosting & Reseller Plans ★
http://www.host4cheap.org/
|
|
|
31-01-2008, 03:36 PM
|
#4 (permalink)
|
|
Banned
Join Date: May 2004
Location: Baudland
Posts: 2,433
|
Re: ?using single printf statement to print 2 answers?
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
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
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 by slugger; 31-01-2008 at 04:17 PM.
|
|
|
31-01-2008, 03:44 PM
|
#5 (permalink)
|
|
Beware of the innocent
Join Date: Dec 2005
Posts: 1,024
|
Re: ?using single printf statement to print 2 answers?
^^ hey shubankarz... howdee.. ROFL. 
You missed the second path in the first image.
__________________
Life is too short. Have fun.
|
|
|
31-01-2008, 04:12 PM
|
#6 (permalink)
|
|
Banned
Join Date: May 2004
Location: Baudland
Posts: 2,433
|
Re: ?using single printf statement to print 2 answers?
Last edited by slugger; 01-02-2008 at 10:26 AM.
|
|
|
31-01-2008, 04:47 PM
|
#7 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: ?using single printf statement to print 2 answers?
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 \%, the item"
__________________
Harsh J
www.harshj.com
|
|
|
01-02-2008, 10:38 AM
|
#8 (permalink)
|
|
Banned
Join Date: May 2004
Location: Baudland
Posts: 2,433
|
Re: ?using single printf statement to print 2 answers?
Problem solved guys!!!
it appears that my compile can't handle % symbols if used as a character in the printf statement
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
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
|
|
|
01-02-2008, 03:02 PM
|
#9 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: ?using single printf statement to print 2 answers?
You could try using an escape for the symbol as I already mentioned.
__________________
Harsh J
www.harshj.com
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|