Question: HELLO THIS IS MY CODE BUT IT KEEPS GETTING STUCK IN AN INFINITE LOOP WHEN RUNNING INPUT / OUTPUT REDIRECTION PLEASE HELP! #include / /
HELLO THIS IS MY CODE BUT IT KEEPS GETTING STUCK IN AN INFINITE LOOP WHEN RUNNING INPUTOUTPUT REDIRECTION PLEASE HELP!
#include
Function prototypes for operations
int addint a int b;
int subtractint a int b;
int multiplyint a int b;
int divideint a int b;
int modulusint a int b;
void printErrorchar message;
Main function
int main
char ch operator;
int operand operand result ;
char response;
do
Reset variables for each loop iteration
operand operand;
operator ;
printfEnter expression eg: ;
Read first operand
while ch getchar; Skip leading spaces
if ch && ch
operand ch ;
while ch getchar && ch
operand operandch ;
else
printErrorInvalid first operand.";
continue;
Read operator skip spaces before it
while ch ch getchar;
if ch ch ch ch ch
operator ch;
else
printErrorInvalid operator.";
continue;
Read second operand
while ch getchar; Skip spaces before operand
if ch && ch
operand ch ;
while ch getchar && ch
operand operandch ;
else
printErrorInvalid second operand.";
continue;
Perform the calculation
switch operator
case : result addoperand operand; break;
case : result subtractoperand operand; break;
case : result multiplyoperand operand; break;
case :
if operand
printErrorDivision by zero.";
continue;
result divideoperand operand;
break;
case :
if operand
printErrorModulus by zero.";
continue;
result modulusoperand operand;
break;
default:
printErrorUnknown error.";
continue;
Output the result
printfResult of expression: d c d d
operand operator, operand result;
Ask user if they want to continue
printfDo you want to enter another expression? YN: ;
while response getchar; Skip spaces
while getchar
; Clear input buffer
while response Y response y;
return ;
Operation functions
int addint a int b
return a b;
int subtractint a int b
return a b;
int multiplyint a int b
return a b;
int divideint a int b
return a b;
int modulusint a int b
return a b;
Function to print an error message
void printErrorchar message
printfError: s
message;
while getchar
; Clear input buffer
Programming In C
Lab B
Objective
To gain expertise in the use of repetition, character processing, functions, parameter passing, inputoutput redirection and command line.
Assignment
Write a program to perform a simple arithmetic calculation. The user is to enter a simple expression integer operator integer such as:
The program is to extract the operands and the operator one character at a time, perform the indicated calculation and display the result or print out an appropriate error message. For example:
Operators should include
Operands are positive integers, no sign
Use getchar function to read the expression
Use putchar function to output the expression
Expression will have variable spacing before the first operand, between operator and operands and after the second operand zero one or more spaces
Your program needs to be in a loop asking whether the user wants to enter another expression with a mathrmYmathrmN type of an answer one character
Code a function for each operator to do the appropriate operation functions
No global variables, arrays, string andor character handling functions may be used in this lab.
Your program needs to generate an appropriate error message if the expression is invalid.
You may ONLY use getchar and putchar functions for reading and writing the expression. You may use scanf and printf functions for user prompts, error messages, etc.
Input
One expression at a time with user prompts to repeat.
Output
The whole expression entered by the user EXACTLY as typed in including spaces the result or an appropriate error message. Need to show all test cases and scenarios for valid and invalid expressions there are many and how your program handled it
Run
Create an input file of all valid and invalid expressions and an
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
