0

the following code gives me a stack overflow error and I can't seem to find why.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

char main_choice();

int main() 
{
  //case_b();
  // check_if_num1();
  char mainchoice [300];

  printf("the capital is: %c\n", main_choice(mainchoice));
  return 0;
}


char main_choice()
{
  int flag1=0;
  int flag2=0;
  char choice;
  printf("Select one of the following commands: \n");
  printf("B) - Binary Mathematical Operations, such as addition and subtraction.\n");
  printf("U) - Unary Mathematical operations, such as square root, and log.\n");
  printf("A) - Advances Mathematical Operations, using variables, arrays.\n");
  printf("V) - Define variables and assign them values.\n");
  printf("E) - Exit\n");
  scanf(" %[^\n]%*c",&choice); 

  
while (1)
  {
    if (strlen(&choice) == 1)
    {
      do
        {
          if (choice == 'b')
          {
            choice = 'B';
            return choice;
            flag1 = 1;
            //flag2 = 1;
            break;
          }
          else if (choice == 'u')
          {
            choice = 'U';
            return choice;
            flag1 = 1;
            //flag2 = 1;
            break;
          }
          else if (choice == 'a')
          {
            choice = 'A';
            return choice;
            flag1 = 1;
            //flag2 = 1;
            break;
          }
          else if (choice == 'v')
          {
            choice = 'V';
            return choice;
            flag1 = 1;
            //flag2 = 1;
            break;
          }
          else if (choice == 'e')
          {
            choice = 'E';
            return choice;
            flag1 = 1;
            //flag2 = 1;
            break;
          }
            
            // if (strlen(&choice) == 1 && choice != 'b' && choice != 'u' && choice != 'a' && choice != 'v' && choice != 'e')
          else
            {
              //flag2 =0;
              printf("Invalid entry\n");
              printf("----------------------------------------------------------\n\n");
              printf("Select one of the following commands: \n");
              printf("B) - Binary Mathematical Operations, such as addition and subtraction.\n");
              printf("U) - Unary Mathematical operations, such as square root, and log.\n");
              printf("A) - Advances Mathematical Operations, using variables, arrays.\n");
              printf("V) - Define variables and assign them values.\n");
              printf("E) - Exit\n");
              scanf(" %[^\n]%*c",&choice);
            }
        }while(flag1);
      //break;
    }
    else
    {
      printf("Invalid entry\n");
      printf("----------------------------------------------------------\n\n");
      printf("Select one of the following commands: \n");
      printf("B) - Binary Mathematical Operations, such as addition and subtraction.\n");
      printf("U) - Unary Mathematical operations, such as square root, and log.\n");
      printf("A) - Advances Mathematical Operations, using variables, arrays.\n");
      printf("V) - Define variables and assign them values.\n");
      printf("E) - Exit\n");
      scanf(" %[^\n]%*c",&choice); 
      
    }
    //break;
  }
  //return choice;
}

so I'm trying to test exception handling and when I input the following in order "p" (gives me an error and asks me to input again, which is correct) then, "b" (it displays 'the capital is: B' which is correct)

but, when I start with a string length greater than 1 say "awd" (it gives me an error and asks to input again which is correct)

then I input a valid option like "b" (it gives me a stack smashing error)

1 Answers1

0

The %[^\n] part of your format string matches a series of characters in the input that are not newlines. The scanf implementation will attempt to store all of those characters in the string pointed to be the corresponding pointer, but that pointer is &choice which just points to a single character. You could change choice into an array of characters.

David Grayson
  • 84,103
  • 24
  • 152
  • 189