How do I fix a default error on my switch statement?

49 Views Asked by At

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, and
  • Q 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.

1

There are 1 best solutions below

0
On

scanf("%c", &letter); reads the next byte from the input stream. You want to ignore the pending newline left by the previous call to scanf() so you should add a space in the format string before the %c and test the return value to detect and handle the end of file:

   if (scanf(" %c", &letter) != 1)
       break;

Note that you should always test the return value of scanf() to detect invalid or missing input. You might also want to flush the pending input line with this function:

/* read and discard the current input line. return EOF at end of file */
int flush_stdin(void) {
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        continue;
    return c;
}

In your functions, you should append a \n to the format string to ensure proper output and avoid the need for extra printf("\n") at the call sites.

Here is a floating point version you can study for the average:

void average(int arr[], int n) {
    if (n <= 0) {
        printf("No deposits yet\n", avg);
    } else {
        double sum = 0.0;
        double avg;
        for (int i = 0; i < n; i++) {
            sum = sum + arr[i];
        }
        avg = sum / n;
        printf("The average of the deposits is %.2f\n", avg);
    }
}