Ktoś mi może pomóc bo już nie mam sił,czemu pętla przy pobieraniu x'a i y'a się od razu zapętla przy podaniu litery,a nie czeka na wprowadzenie następnej?
Program ma dzielić bądź mnożyć podane liczby w zależności od wyboru.Pisany w C.
Za wszelkie wskazówki bardzo dziękuję.
Kod:
#include <stdio.h>
float div(float x,float y);
float mul(float x,float y);
int main()
{
float x,y,result;
char ch;
printf("Choose what to do:\n\na. division\nb.multiplication\n\nI want: ");
while ((ch=getchar())!='a' && ch!='b')
{
printf("Wrong choice.try again.\n\nI want: ");
fflush(stdin);
}
printf("Type your first number: ");
while(1!=scanf("%f",&x))
{
printf("\nIt is not a number! Try again.\n");
}
printf("Type your second number: ");
while((1!=scanf("%f",&y))||(y==0 && ch=='a'))
{
if(y==0) printf("You can't divide by 0.Try again.\n");
else printf("\nIt is not a number! Try again.\n");
}
switch(ch)
{
case 'a':
result=div(x,y);
break;
case 'b':
result=mul(x,y);
break;
default:
printf("\n\nError!\n\n");
result=0;
}
printf("Your result: %.2f\n\n",result);
getchar();
return 0;
}
float div(float x,float y)
{
float result;
result=x/y;
return result;
}
float mul(float x,float y)
{
float result;
result=x*y;
return result;
}