Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 31-01-2008, 02:32 AM   #1 (permalink)
Banned
 
slugger's Avatar
 
Join Date: May 2004
Location: Baudland
Posts: 2,433
Default ?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
slugger is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 31-01-2008, 06:35 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 31-01-2008, 07:38 AM   #3 (permalink)
Host4Cheap.org
 
Sukhdeep Singh's Avatar
 
Join Date: May 2005
Location: Digit Forum
Posts: 2,102
Default 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/
Sukhdeep Singh is offline  
Old 31-01-2008, 03:36 PM   #4 (permalink)
Banned
 
slugger's Avatar
 
Join Date: May 2004
Location: Baudland
Posts: 2,433
Default 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.
slugger is offline  
Old 31-01-2008, 03:44 PM   #5 (permalink)
Beware of the innocent
 
ilugd's Avatar
 
Join Date: Dec 2005
Posts: 1,024
Default 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.
ilugd is offline  
Old 31-01-2008, 04:12 PM   #6 (permalink)
Banned
 
slugger's Avatar
 
Join Date: May 2004
Location: Baudland
Posts: 2,433
Default Re: ?using single printf statement to print 2 answers?


Last edited by slugger; 01-02-2008 at 10:26 AM.
slugger is offline  
Old 31-01-2008, 04:47 PM   #7 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 01-02-2008, 10:38 AM   #8 (permalink)
Banned
 
slugger's Avatar
 
Join Date: May 2004
Location: Baudland
Posts: 2,433
Default 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
slugger is offline  
Old 01-02-2008, 03:02 PM   #9 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print print-protected pdf manosynu Software Q&A 1 28-10-2007 05:19 PM
What is a null statement in C language? fun2sh Programming 11 13-07-2007 07:36 PM
Need Some answers for Nokia N73 guhanath Mobiles and Tablets 8 05-01-2007 05:37 PM
How to print threads in one particular section alongwith answers Ramakrishnan QnA (read only) 7 31-10-2006 03:44 PM
Clarification of a Problem Statement... Important. Thor QnA (read only) 1 28-09-2006 07:44 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:49 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2