944,057 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1681
  • C RSS
Oct 7th, 2006
0

help with conversion calculator

Expand Post »
this code to convert pounds to barbados dollars given a constant rate the price cannot go over $100 when i first run it and input a number under 100 it tells me that it is invalid and sometimes i get nothing at all. Can neone tell me why?

  1. /* This Program converts Pounds Sterling to Barbados Dollars*/
  2. # include <stdio.h>
  3.  
  4. int main ()
  5.  
  6. {
  7. float Pds, Bds; /* Declare Variables*/
  8. const float Rate =3.75;
  9.  
  10. printf("Enter the Price in Pounds Sterling\n"); /*Prompt User*/
  11. scanf_s("%f", &Pds);
  12.  
  13. while(Pds >100);/* finds illegal value*/
  14. {
  15. printf("Error your price cannot be greater than $100\n");
  16. printf("Please Enter another\n");
  17. scanf_s("%f",&Pds);
  18. }
  19.  
  20. Bds=Pds*Rate;
  21.  
  22. printf("Price in Barbados Dollars=%.2f",Bds);
  23.  
  24.  
  25. return 0;
  26.  
  27. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 7th, 2006
0

Re: help with conversion calculator

stop using scanf() because it may corrupt the input buffer and leave keys in the keyboard buffer. use fgets() instead
  1. float num;
  2. char buf[16];
  3. printf("blabla");
  4. // get input from user
  5. fgets(buf,sizeof(buf),stdin);
  6. // convert to float
  7. num = atof(buf);
Last edited by Ancient Dragon; Oct 7th, 2006 at 9:38 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 7th, 2006
0

Re: help with conversion calculator

Hm did U got corrupted code and your task is to fix it?
No?!?! Ok then just remove ; after while loop.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 8th, 2006
0

Re: help with conversion calculator

yeah it was the semi colon, thx a bundle can i ask why it did that tho?
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Oct 9th, 2006
0

Re: help with conversion calculator

>> while(Pds >100);

that is an infinite loop because of the semicolon at the end. Your program will never stop executing that loop because the value os Pds never changes. This is a common error that everybody counters on occasion due to carelessness.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 9th, 2006
0

Re: help with conversion calculator

while ( Pds > 100 ) ; is equivalent to

  1. while ( Pds > 100 )
  2. {
  3. // sit back and enjoy baby
  4. }

Like Mr. Dragon said the variable which controls the run of your "While " loop never changes and so it goes on for infinity.
Last edited by ~s.o.s~; Oct 9th, 2006 at 2:07 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: End of file controlled loop
Next Thread in C Forum Timeline: calculator program in C -help needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC