gpt4 book ai didi

c++ - 我收到段错误,但不明白段错误发生在何处

转载 作者:行者123 更新时间:2023-11-28 04:01:42 26 4
gpt4 key购买 nike

我相信错误发生在第 16 行之前的某处,编译后运行时没有显示第一个 cout 语句。我最困惑的一点是,甚至没有显示 main 中的第一个 cout 语句。错误可能是从我没有注意到的某个地方开始的,还是在我的 include 语句中的某个地方?我在这里不知所措,因为在任何代码真正运行之前出现段错误似乎很奇怪,尤其是在 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 };

最佳答案

几个问题:

  • cout缓冲其输出。这意味着错误可能发生在第一个 cout 之后声明,但因为 cout还没有被刷新到屏幕上,你看不到它。如果你想要cout立即打印到屏幕,那么你应该以 << std::endl 结束该行.否则,使用 cerr ,它不会缓冲输出并会自动打印到屏幕上。
  • 据@Eljay 报道,getScreen返回指向已销毁对象的指针。如果你想返回一个动态长度的字符串数组,那么你最好将函数签名更改为 vector<string> getScreen(int input) .

关于c++ - 我收到段错误,但不明白段错误发生在何处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59418907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com