For some reason my repetition structure is not working correctly. I do not get any compiler errors, but the calculation only happens the first time I choose one of my options. After the result is displayed the first time, the options repeat, I select an option and enter the data, then get an error instead of calculation.
I have used repetition before, and have not had this problem until now.
Can anyone help me resolve this issue?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxR 2
#define maxC 2
void doEval();
int doAdd();
int doSub();
char doQuit(char);
main()
{
doEval();
return 0;
}
void doEval()
{
char userInput[2048];
int row, col;
int arrayA[maxR][maxC];
int arrayB[maxR][maxC];
int arrayZ[maxR][maxC];
char optionA;
printf("Enter one of the following options:\n\
Q - Quit the program\n\
A - Add 2 matrices of size 5 x 5\n\
S - Subtract 2 matrices of size 5 x 5\n\
Enter option: ");
fgets(userInput,2048,stdin);
if((strstr(userInput,"Q") != NULL) ||
(strstr(userInput,"q") != NULL)) {
optionA = doQuit(optionA);
} else if ((strstr(userInput,"A") != NULL) ||
(strstr(userInput,"a") != NULL)) {
arrayZ[maxR][maxC] = doAdd(arrayZ[maxR][maxC]);
} else if ((strstr(userInput,"S") != NULL) ||
(strstr(userInput,"s") != NULL)) {
arrayZ[maxR][maxC] = doSub(arrayZ[maxR][maxC]);
} else {
printf("Not Valid, Pleas Try Again");
}
doEval();
return;
}
int doAdd(int arrayZ[maxR][maxC])
{
int row, col;
int arrayA[maxR][maxC];
int arrayB[maxR][maxC];
printf("Enter Matrices\n");
printf("Matrix 1: \n");
for (row = 0; row < maxR; row++) {
for (col = 0; col < maxC; col++) {
scanf("%d", &arrayA[row][col]);
}
}
printf("Matrix 2: \n");
for (row = 0; row < maxR; row++) {
for (col = 0; col < maxC; col++) {
scanf("%d", &arrayB[row][col]);
}
}
printf("\n");
printf("Sum of Matrices:\n");
for (row = 0; row < maxR; row++) {
for (col = 0; col < maxC; col++) {
arrayZ[row][col] = arrayA[row][col] + arrayB[row][col];
printf ("% 5d ", arrayZ[row][col]);
}
printf ("\n\n");
}
scanf("%*c");
return arrayZ[row][col];
}
int doSub(int arrayZ[maxR][maxC])
{
int row, col;
int arrayA[maxR][maxC];
int arrayB[maxR][maxC];
printf("Enter Matrices\n");
printf("Matrix 1: \n");
for (row = 0; row < maxR; row++) {
for (col = 0; col < maxC; col++) {
scanf("%d", &arrayA[row][col]);
}
}
printf("Matrix 2: \n");
for (row = 0; row < maxR; row++) {
for (col = 0; col < maxC; col++) {
scanf("%d", &arrayB[row][col]);
}
}
printf("\n");
printf("Difference of Matrices:\n");
for (row = 0; row < maxR; row++) {
for (col = 0; col < maxC; col++) {
arrayZ[row][col] = arrayA[row][col] - arrayB[row][col];
printf ("% 5d ", arrayZ[row][col]);
}
printf ("\n\n");
}
scanf("%*c");
return arrayZ[row][col];
}
char doQuit(char optionA)
{
exit(0);
}