0

I'm working on a project for school and my recursive menu() function isn't working properly. It's supposed to stop to wait for a scanf() input, but when called, runs all the way through itself then goes again, this time stopping. Here is the code. All the other functions referenced here work correctly. The variables also all work and are declared and everything. So are the arrays.

//FUNCTION FOR MENU

void menu() {

  //DECLARE LOCAL VARIABLES

  char allowedMenuInputs[8] = {'U', 'u', 'A', 'a', 'O', 'o', 'Q', 'q'};

  //DISPLAY MENU
  
  printf("MENU\n");
  printf("u - Update stock price\n");
  printf("a - Output stocks above a price\n");
  printf("o - Output stock list\n");
  printf("q - Quit\n\n");

  //PROMPT USER
  
  printf("Choose an option:\n");

  //SCAN FOR INPUT

  scanf("%c", &menuInput);
  printf("\n"); //NEWLINE

  //CHECK INPUT

  if ((menuInput == allowedMenuInputs[0]) || (menuInput == allowedMenuInputs[1])) {
    stockUpdate();
    menu();
  }
  else if ((menuInput == allowedMenuInputs[2]) || (menuInput == allowedMenuInputs[3])) {
    priceCheck();
    menu();
  }
  else if ((menuInput == allowedMenuInputs[4]) || (menuInput == allowedMenuInputs[5])) {
    displayStocks();
    menu();
  }
  else if ((menuInput == allowedMenuInputs[6]) || (menuInput == allowedMenuInputs[7])) {
    return 0;
  }
  else {
    printf("Invalid option! Please try again!!\n");
    menu();
  }
}

int main(void) {

  //WELCOME MESSAGE

  printf("Welcome to the Stock Portfolio Program!\n\n");

  //USER INPUT

  for (i = 0; i < NUM_STOCKS; ++i) {
    printf("Enter Stock ID number: \n");
    scanf("%d", &(stockID[i]));
    printf("Enter Stock Price $:\n");
    scanf("%lf", &(stockPrice[i]));
    printf("\n\n"); //NEWLINE
  }

  //OUTPUT STOCK LIST

  displayStocks();

  //OPEN MENU

  menu();
  
  return 0;
}

Here is the output

pg 1 pg 2

As you can see, the menu function runs through itself without stopping for input. All help appreciated!

I have no idea what to try and what causes this, as this has never happened to me before.

  • The old chestnut: `scanf(" %c", &menuInput);` with that added space. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Oct 28 '22 at 16:37
  • @Daniel Perju Use scanf(" %c", &menuInput);. Pay attention to the leading space in the format string. It allows to skip white space characters. Also instead of recursively to call the function it is better to make a loop. – Vlad from Moscow Oct 28 '22 at 16:37
  • 1
    Did your evil teacher ask you to use recursion? Otherwise there is barely an excuse to not use a loop for what loops were invented for. – Gerhardh Oct 28 '22 at 16:44
  • Thank you. The issue was resolved by adding a space in the scanf("%c") function. I was not asked to use recursion, I just like it more than loops. Makes it much easier to parse for me. I made sure not to create an infinite recursive loop or memory overflow though. – Daniel Perju Oct 28 '22 at 16:50

0 Answers0