I started my Calculator program. It compiles fine, but when it reaches "Enter a Math Operator: " it doesn't wait for the user to input an operator. Instead, it automatically skips to "Enter Your Second Number: " and waits there for input.
This is shown in this picture of the output:
Picture of Error
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float num1;
float num2;
float answer;
char math_operator;
char enter;
printf(
" ---------------------------------------------------------\n"
" Math Calculator\n"
" ---------------------------------------------------------\n\n"
" This is program is only intended for Math Operations\n"
" that only require 3 arguments, which in this case is\n"
" 2 numbers and a desired Math Operator. The operations\n"
" which can be performed by this program are explained in\n"
" the explantion of available Math Operators located below.\n\n\n"
" --| Explanation of Math Operators |---------------------|\n"
" | |\n"
" + | This operator returns the result of one |\n"
" | number added to another number. |\n"
" | |\n"
" - | This operator returns the result of one |\n"
" | number subtracted from another number. |\n"
" | |\n"
" / | This operator returns the result of one |\n"
" | number divided by another number. |\n"
" | |\n"
" * | This operator returns the result of one |\n"
" | number multiplied by another number. |\n"
" | |\n"
" ^ | This operator returns the result of the |\n"
" | first number inputed, raised to the power |\n"
" | of the second number. |\n"
" | |\n"
" ! | This operator returns the result of square |\n"
" | root of the first number, given the index, |\n"
" | which is the second number inputed. Use 2 |\n"
" | as the value of the second number for a |\n"
" | regular square root operation. |\n"
" --|-----------------------------------------------------|\n\n"
" Press ENTER to begin calculations.\n\n" );
scanf( "%c", &enter );
if( enter == '\n' )
{
printf( " Please Enter Your First Number: " );
scanf( "%f", &num1 );
}
printf( "\n Please Enter a Math Operator: " );
scanf( "%c", &math_operator );
printf( "\n Please Enter Your Second Number: " );
scanf( "%f", &num2 );
if( math_operator == '+' )
{
answer = num1 + num2;
}
if( math_operator == '-' )
{
answer = num1 - num2;
}
if( math_operator == '/' )
{
answer = num1 / num2;
}
if( math_operator == '*' )
{
answer = num1 * num2;
}
if( math_operator == '^' )
{
answer = pow( num1, num2 );
}
if( math_operator == '!' )
{
answer = pow( num1, ( 1 / num2 ) );
}
printf( "%f %c %f = %f", num1, math_operator, num2, answer );
return 0;
}
Let the corrections/learning begin.
Also, now that I think about it. Would it be at all useful to use a pointer for the variable "enter" since it does not need to be used throughout the rest of the program?