I am a beginner at C and I am writing a basic program that converts dollars to euros. For some reason the program is not running this line: “scanf(“%c”, &yn);”. If I remove the do while loop the program works fine.
Instead of stopping and waiting for the user to enter ‘y’ or ‘n’ the loop restarts and asks for the usd amount again.
#include<stdio.h>
main()
{
float usd = 0.00;
float euro = 0.00;
char yn;
const float conversion = 0.75;
do {
/*get amount to convert*/
printf("Please enter the amount of USD you want to convert to Euros: ");
scanf("%f", &usd);
/*convert amount*/
euro = (usd * conversion);
/*output results and ask to continue*/
printf("\n%.2f USD equals %.2f Euros. Do you want to convert another amount? (y/n): ", usd, euro);
scanf("%c", &yn);
printf("\n");
/*if yes, get new amount to convert. if no, program ends*/
} while (yn = 'y');
return 0;
}
Thanks in advance.
1 Answer