So, I have to make a program that shows a menu of a bank, with several options for the user to input.
- For an input
G
, the user enters a deposit, which is stored inside an array. - For an input
S
, it displays the sum of all deposits. - For
D
, it displays all deposits from higher to lower, A
displays the average of all deposits,L
displays the lowest value, andQ
quits the program.
The criteria is that I have to use arrays, a switch
statement, and a do
...while
loop.
I wrote the functions and they work pretty well (except the average function, I don't know to make it work for floating results), but the biggest problem is that when I tried to integrate all the functions in a switch
statement inside a do
...while
loop, somehow the first iteration occurs fine, but from the second iteration it repeats the actions twice, and the default case pops up every time too. I don't know if the problem is the switch
statement or the do
...while
loop.
This is my code:
#include <stdio.h>
void askForInput(int arr5[], int dep);
void fSum(int arr[], int n);
void sortDep(int arr2[], int n1);
void average(int arr3[], int n2);
void lowestVal(int arr4[], int n3);
int d = 0;
int deposits[] = {};
int main() {
char letter;
do {
printf("****BANK MENU****\n"
"[G]et a new deposit\n"
"[S]um of all deposits\n"
"[D]isplay deposits\n"
"[A]verage\n"
"[L]owest\n"
"[Q]uit\n"
"Please enter a letter: ");
scanf("%c", &letter);
switch (letter) {
case 'G':
askForInput(deposits, d);
printf("\n");
d++;
break;
case 'S':
fSum(deposits, d);
printf("\n");
break;
case 'D':
sortDep(deposits, d);
printf("These are your deposits:\n");
for (int k = 0; k < d; k++)
printf("%d ",deposits[k]);
printf("\n");
break;
case 'A':
average(deposits, d);
printf("\n");
break;
case 'L':
lowestVal(deposits,d);
printf("\n");
break;
case 'Q':;
default:
printf("Choose a valid option!\n");
}
} while (letter != 'Q');
return 0;
}
void askForInput(int arr5[], int dep) {
printf("Please enter a deposit:\n");
scanf("%d", &(arr5[dep]));
}
void fSum(int arr[], int n) {
int sum = 0;
for (int x = 0; x < n; x++)
sum = sum + arr[x];
printf("The sum of the deposits is %d", sum);
}
void sortDep(int arr2[], int n1) {
int i, j, temp;
for (i = 0; i < n1 - 1; i++)
for (j = 0; j < n1 - i - 1; j++)
if (arr2[j] < arr2[j + 1]) {
temp = arr2[j];
arr2[j] = arr2[j + 1];
arr2[j + 1] = temp;
}
}
void average(int arr3[], int n2) {
int sumation = 0;
int avg;
for (int u = 0; u < n2; u++) {
sumation += arr3[u];
}
avg = sumation / n2;
printf("The average of the deposits is %d", avg);
}
void lowestVal(int arr4[], int n3) {
int minIndex = 0;
for (int index = 1; index < n3; index++) {
if (arr4[minIndex] > arr4[index])
minIndex = index;
}
printf("The lowest deposit is %d", arr4[minIndex]);
}
The goal is that it shows all the options every time the user needs to make an input. But even though the code works for the most part, what I got was all the options repeating themselves twice, and the default case repeating every time too. I'd also like to know how to fix the average function so it gives a more precise result.