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 INPUT/OUTPUT REDIRECTION PLEASE HELP!
#include
// Function prototypes for operations
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
int modulus(int a, int b);
void printError(char* message);
// Main function
int main(){
char ch, operator;
int operand1=0, operand2=0, result =0;
char response;
do {
// Reset variables for each loop iteration
operand1= operand2=0;
operator ='\0';
printf("Enter expression (e.g.,15+30): ");
// Read first operand
while ((ch = getchar())==''); // Skip leading spaces
if (ch >='0' && ch ='9'){
operand1= ch -'0';
while ((ch = getchar())>='0' && ch ='9'){
operand1= operand1*10+(ch -'0');
}
}
else {
printError("Invalid first operand.");
continue;
}
// Read operator (skip spaces before it)
while (ch =='') ch = getchar();
if (ch =='+'|| ch =='-'|| ch =='*'|| ch =='/'|| ch =='%'){
operator = ch;
}
else {
printError("Invalid operator.");
continue;
}
// Read second operand
while ((ch = getchar())==''); // Skip spaces before operand
if (ch >='0' && ch ='9'){
operand2= ch -'0';
while ((ch = getchar())>='0' && ch ='9'){
operand2= operand2*10+(ch -'0');
}
}
else {
printError("Invalid second operand.");
continue;
}
// Perform the calculation
switch (operator){
case '+': result = add(operand1, operand2); break;
case '-': result = subtract(operand1, operand2); break;
case '*': result = multiply(operand1, operand2); break;
case '/':
if (operand2==0){
printError("Division by zero.");
continue;
}
result = divide(operand1, operand2);
break;
case '%':
if (operand2==0){
printError("Modulus by zero.");
continue;
}
result = modulus(operand1, operand2);
break;
default:
printError("Unknown error.");
continue;
}
// Output the result
printf("Result of expression: %d %c %d =%d
", operand1, operator, operand2, result);
// Ask user if they want to continue
printf("Do you want to enter another expression? (Y/N): ");
while ((response = getchar())==''); // Skip spaces
while (getchar()!='
'); // Clear input buffer
} while (response =='Y'|| response =='y');
return 0;
}
// Operation functions
int add(int a, int b){
return a + b;
}
int subtract(int a, int b){
return a - b;
}
int multiply(int a, int b){
return a * b;
}
int divide(int a, int b){
return a / b;
}
int modulus(int a, int b){
return a % b;
}
// Function to print an error message
void printError(char* message){
printf("Error: %s
", message);
while (getchar()!='
'); // Clear input buffer
} Programming In C
Lab 3B
Objective
To gain expertise in the use of repetition, character processing, functions, parameter passing, input/output 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: \(15+30\)
The program is to extract the 2 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: \(15+30=45\)
- 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 \(\mathrm{Y}/\mathrm{N}\) type of an answer (one character).
- Code a function for each operator to do the appropriate operation (5 functions).
- No global variables, arrays, string and/or 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
HELLO THIS IS MY CODE BUT IT KEEPS GETTING STUCK

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!