DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   help with conversion calculator (http://www.daniweb.com/forums/thread57328.html)

boujibabe Oct 7th, 2006 8:11 am
help with conversion calculator
 
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;

        }

Ancient Dragon Oct 7th, 2006 8:37 am
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
float num;
char buf[16];
printf("blabla");
// get input from user
fgets(buf,sizeof(buf),stdin);
// convert to float
num = atof(buf);

andor Oct 7th, 2006 10:28 am
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.

boujibabe Oct 8th, 2006 7:38 pm
Re: help with conversion calculator
 
yeah it was the semi colon, thx a bundle can i ask why it did that tho?

Ancient Dragon Oct 9th, 2006 1:52 am
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.

~s.o.s~ Oct 9th, 2006 1:06 pm
Re: help with conversion calculator
 
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.


All times are GMT -4. The time now is 8:31 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC