'why is this c program not working with loops




int main(){


int totcoinsau;
int totcoinsus;
int totcoinseu;
int totcoins;
int amount;
int currency;


printf("Pick a number between 1 and 95 inclusive.\n");
scanf("%d",&amount);


    while(amount != 0);{
      if ((50 - amount) > 0)
      {
          totcoinsau =+ 1;
          amount = amount -50;
      }
      else if ((20 - amount) > 0)
      {
          totcoinsau =+ 1;
          amount = amount -20;
      }
      else if ((10 - amount) > 0)
      {
          totcoinsau =+ 1;
          amount = amount -10;
      }
      else if ((5 - amount) > 0)
      {
          totcoinsau =+ 1;
          amount = amount -5;
      }
      else
        return(0);}

 

printf("%d",amount);
return(0);

}

Anyone have any idea why this is not working? whenver i run it i get to input a value for amount but then it just doesnt do anything. I beleive their might be something wrong with the loop i just cant figure out what.

thanks in advance

c


Solution 1:[1]

The main issue is here:

while(amount != 0);

That ending semi-colon is the body of the loop. It's equivalent to

while(amount != 0)
{
}

Which will be an infinite loop if amount begins with a value different from zero.

You solve this problem simply by removing the semi-colon.


Then when you fix that, you have another possible problem, and it's the

return(0);

at the end of the loop. That will return from the main function, and terminate your program, immediately. I don't think that's the purpose.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1