Не понимаю в чём проблема и почему программа не хочет работать после ввода 2 данных
#include <stdio.h>
int main()
{
char opt;
int n1, n2;
int res1;
float res;
printf ("podaj liczbe nr 1:");
if (scanf("%d", &n1) != 1)
{
printf("Incorrect input");
return 1;
}
printf ("podaj liczbe nr 2:");
if (scanf("%d", &n2) != 1)
{
printf("Incorrect input");
return 1;
}
printf ("podaj wazny operator( + , * , - , / ):");
scanf ("%c", &opt);
if (opt == '+')
{
res1 = n1 + n2;
printf ("wynik = %d",res1);
}
else if (opt == '-')
{
res1 = n1 - n2;
printf ("wynik = %d",res1);
}
else if (opt == '*')
{
res1 = n1 * n2;
printf ("wynik = %d",res1);
}
else if (opt == '/')
{
if (n2 == 0)
{
printf("Operation not permitted");
return 2;
}
else
{
res = n1/n2;
printf("wynik = %.2f\n",res);
}
}
return 0;
}
Нельзя использовать switch, программа почему то работает только если выбор знака будет первым, а потому уже ввод цифр. Но мне для выполнения задания нужно наоборот. Помогите кто сможет!
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Читаете \n...
Попробуйте добавить пробел:
scanf (" %c", &opt);
Вот программа целиком: https://ideone.com/cp5byI