help with conversion calculator

Reply

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

help with conversion calculator

 
0
  #1
Oct 7th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with conversion calculator

 
0
  #2
Oct 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: help with conversion calculator

 
0
  #3
Oct 7th, 2006
Hm did U got corrupted code and your task is to fix it?
No?!?! Ok then just remove ; after while loop.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: help with conversion calculator

 
0
  #4
Oct 8th, 2006
yeah it was the semi colon, thx a bundle can i ask why it did that tho?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with conversion calculator

 
0
  #5
Oct 9th, 2006
>> 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with conversion calculator

 
0
  #6
Oct 9th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC