char operation;
bool CorrectOrNot = false;
do {
CorrectOrNot = true;
printf("Enter your first number: ");
if (scanf_s("%d", &firstnum) != 1)
{
printf("\nError reading first number!\n");
return 0;
}
printf("Enter your second number: ");
if (scanf_s("%d", &secondnum) != 1)
{
printf("\nError reading second number!\n");
return 0;
}
printf("Enter your operation: ");
fflush(stdin);
if (scanf_s("%c", &operation, sizeof(char)) != 1)
{
printf("\nError reading operation!\n");
return 0;
}
switch (operation)
{
case '+':
answer = firstnum + secondnum;
break;
case '-':
answer = firstnum - secondnum;
break;
case '*':
answer = firstnum * secondnum;
break;
case '/':
answer = firstnum / secondnum;
break;
default:
printf("Error!\n");
CorrectOrNot = true;
break;
}
} while (CorrectOrNot);
return answer;
}
float DottedNumbers()
{
float answer;
float firstnum;
float secondnum;
char operation;
bool CorrectOrNot = false;
do
{
CorrectOrNot = true;
printf("Enter your first number: ");
if (scanf_s("%f", &firstnum) != 1)
{
printf("\nError reading first number!\n");
return 0;
}
printf("Enter your second number: ");
if (scanf_s("%f", &secondnum) != 1)
{
printf("\nError reading second number!\n");
return 0;
}
printf("Enter your operation: ");
if (scanf_s("%c", &operation, sizeof(char)) != 1)
{
printf("\nError reading operation!\n");
return 0;
}
switch (operation)
{
case '+':
answer = firstnum + secondnum;
break;
case '-':
answer = firstnum - secondnum;
break;
case '*':
answer = firstnum * secondnum;
break;
case '/':
answer = firstnum / secondnum;
break;
default:
printf("\nError!\n");
CorrectOrNot = true;
break;
}
} while (CorrectOrNot);
return answer;
}
int main()
{
float fanswer = 0.0;
int answer;
int type;
bool CorrectOrNot;
do
{
printf("1.Dotted numbers\n");
printf("2.Whole numbers\n");
printf("1 or 2 ?\n");
if (scanf_s("%i", &type) != 1)
{
printf("Error reading type!\n");
return 0;
}
if (type == 1)
{
answer = DottedNumbers();
CorrectOrNot = true;
}
else if (type == 2)
{
answer = WholeNumbers();
CorrectOrNot = true;
}
else
{
printf("Error! Try again!\n");
CorrectOrNot = false;
}
}while (CorrectOrNot);
printf("The result is: %d\n", answer);
printf("The result is: %f\n", fanswer);
return 0;
}
так надо if (scanf_s("%f\n", &firstnum) != 1)
Вот же подсказали.
Обсуждают сегодня