Joshua Knoeb
Joshua Knoeb

Reputation: 1

I am receiving a segmentation fault error yet do not understand where the segmentation fault is occuring

I believe the error is occurring somewhere before line 16, the first cout statement is not shown when ran after compiling. My most confusing point is that not even the first cout statement in main is shown. Could the error have started somewhere that i have not noticed or is it somewhere in my include statements? I am at a loss here because it seems very odd to have a seg fault before any code is truly ran especially in main.

      2 #include<iostream>
      3 #include<iomanip>
      4 #include<sstream>
      5 #include<fstream>
      6 using namespace std;
      8 void Show_Menu(int);
      9 string* getScreen(int);
     13
     14 int main()
     15 {
     16     cout <<"Thank You for Choosing Taco Bell";
     17     string* menu = nullptr;
     18     menu = getScreen(1);
     19     int input = 1;
     20     Show_Menu(input);
     21     return 0;
     22 };
     23
     24 string* getScreen( int input)
     25 {
     26     string taco_burrito[] = {"Tacos and Burritos","Beef_Burrito_Supreme", "Bean_Burrito", "Beefy_Five_Layer_Burrito", "Shredded_Chicken_Burrito", "Seven_Layer_Burrito", "Taco", "Soft_ta    co", "Shredded_Chicken_Soft_Taco", "Taco_Supreme", "Soft_Taco_Supreme", "Grilled_Steak_Soft_Taco", "Nacho_Cheese_Doritos_Locos_Taco" };
     27     string special_items[] = {"Special Items","Cheesy_Roll_Up", "Mexican_Pizza", "Chicken_Quesadilla", "Beef_Chalupa", "Chicken_Power_Bowl", "Cheesy_Gordita_Crunch", "Crunchwrap_Supreme    ", "Beef_Quesarito", "Beefy_Nacho_Griller", "Fiesta_Taco_Salad"};
     28     string nacho_sides_desserts[] = {"Nachos And Desserts","Chips_And_Nacho_Cheese", "Chips_And_Guacamole", "Nachos_Supreme", "Nachos_Bell_Grande", "Chips_And_Pico", "Pintos_And_cheese"    , "Fiesta_Potatos", "Black_Beans_And_Rice", "Black_Beans", "Rice", "Cinnamon_Twist", "Cinnabon_Two_Pack", "Cinnabon_Twelve_Pack"};
     29     string dollar_cravings[] = { "Dollar Cravings","Beefy_Frito_Burrito", "Cheesy_Bean_Rice", "Spicy_Potato_Soft_Taco", "Shredded_Chicken_Quesadilla_Melt", "Spicy_Tostada", "Cheesy_Roll    _Up", "Triple_Layer_Nachos", "Cinnamon_Twist", "Cinnabon_Two_Pack"};
     30         switch(input){
     31             case 1:
     32                 return taco_burrito;
     33                 break;
     34             case 2:
     35                 return special_items;
     36                 break;
     37             case 3:
     38                 return nacho_sides_desserts;
     39                 break;
     40             case 4:
     41                 return dollar_cravings;
     42             default:
     43                 cout << "Invalid Entry" <<endl;
     44             }
     45 };
     46
     47 void Show_Menu(int input = 1)
     48 {
     49     string* screen;
     50     screen = getScreen(input);
     51     cout <<"Current Screen is: " << screen[0] << endl;
     52     int size;
     53     size = sizeof(screen);
     54     for(int i =1; i < size; i++)
     55     {
     56         cout << i << ". " << screen[i] << endl;
     57     }
     58 };

Upvotes: 0

Views: 63

Answers (2)

justinfiled
justinfiled

Reputation: 23

In "string* getScreen( int input)" function, the "taco_burrito" is a local variable which will be stored in stack. So it will be destoried when thread leaves, it means you can't pass it to other variable. If you want use "taco_burrito[]" and other arrays, you can put them on top your code outside "main", maybe you want to use "constant" to make them unchangeable. I'am agree that you don't see the cout because the cout buffer doesn't fush. You better use it ends with endl. I'am glad if it will be helpful to you!

Upvotes: 0

Kyle Knoepfel
Kyle Knoepfel

Reputation: 1698

Couple issues:

  • cout buffers its output. That means that the error is likely occurring after the first cout statement, but because cout has not yet been flushed to the screen, you don't see it. If you want cout to immediately print to the screen, then you should end the line with << std::endl. Otherwise, use cerr, which does not buffer output and will automatically print to the screen.
  • As @Eljay reported, getScreen returns a pointer to an object that is destroyed. If you want to return a dynamic-length array of strings, then you are better off changing the function signature to vector<string> getScreen(int input).

Upvotes: 1

Related Questions