User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,369 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,076 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1380 | Replies: 5
Reply
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

help with conversion calculator

  #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?

/* This Program converts Pounds Sterling to Barbados Dollars*/
# include <stdio.h>

int main ()

{
	float Pds, Bds; /* Declare Variables*/
	const float Rate =3.75;

	printf("Enter the Price in Pounds Sterling\n"); /*Prompt User*/
	scanf_s("%f", &Pds);

   while(Pds >100);/* finds illegal value*/
   {
    printf("Error your price cannot be greater than $100\n");
	printf("Please Enter another\n");
	scanf_s("%f",&Pds);
   }

     Bds=Pds*Rate;

     printf("Price in Barbados Dollars=%.2f",Bds);

	
	return 0;

	}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help with conversion calculator

  #2  
Oct 7th, 2006
stop using scanf() because it may corrupt the input buffer and leave keys in the keyboard buffer. use fgets() instead
float num;
char buf[16];
printf("blabla");
// get input from user
fgets(buf,sizeof(buf),stdin);
// convert to float
num = atof(buf);
Last edited by Ancient Dragon : Oct 7th, 2006 at 8:38 am.
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: help with conversion calculator

  #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  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: help with conversion calculator

  #4  
Oct 8th, 2006
yeah it was the semi colon, thx a bundle can i ask why it did that tho?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help with conversion calculator

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: help with conversion calculator

  #6  
Oct 9th, 2006
while ( Pds > 100 ) ; is equivalent to

while ( Pds > 100 )
{
   // sit back and enjoy baby 
}

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 1:07 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 12:17 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC