this promgram supposed to add, subtract, multiply or divide the input until the user presses 'r', and then after it will ask the user if he wants to go again.. please help me...
Code:
#include <stdio.h>
#include <float.h>
int main()
{
float num1=0.0, num2;
char oper, a = 0,exit;
char response[256] = {0};
bool final = false;
printf("Calculator is on.\n");
do
{
while ( 1 )
{
printf("result = %lf\n", num1);
do
{
scanf("%c", &oper);
} while ( oper == '\n' );
if ( oper == 'r' )
{
break;
}
num2 = FLT_MAX;
scanf("%f", &num2);
if ( num2 == FLT_MAX )
{
continue;
}
switch( oper )
{
case '+':
num1 += num2;
break;
case '-':
num1 -= num2;
break;
case '/':
num1 /= num2;
break;
case '*':
num1 *= num2;
break;
default:
printf("Unknown operator!\n");
break;
}
}
printf("Final result: %lf\n", num1);
num1 = 0;
while ( !final && response[0] != 'y' && response[0] != 'Y' )
{
printf("Again? (y/n): ");
scanf("%s", response);
if ( response[0] == 'n' || response[0] == 'N' )
{
final = true;
}
}
} while ( !final );
return 0;
}