【问题标题】:Program (C) crashing when using switch statement and functions使用 switch 语句和函数时程序 (C) 崩溃
【发布时间】:2014-06-25 19:56:38
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
void play();
void quit();

int main()
{
    int x;
    printf("1) play\n2) quit\n");
    scanf("%d", x);
    switch(x) {
    case(1): {
        play();
        break;
    }
    case(2): {
        quit();
        break;
    }
    default:{
        printf("BAD INPUT");
        break;
    }

    /*if x is equal to 1, play
      if x is equal to 2, quit
      otherwise, tell the user, BAD INPUT*/
}

return 0;
}

void play(void){
    printf("play");
}

void quit(void){
   printf("quit");
}

所以基本上,我试图得到它,如果用户输入 1,它说播放,如果用户输入 2,它说退出。它打印播放并退出,但只要我输入一个数字并按下输入,整个事情就会崩溃。我进行了广泛的搜索,但我无法弄清楚为什么会发生这种情况。任何帮助将不胜感激

【问题讨论】:

  • This 将帮助您找到问题。
  • 你应该得到编译器警告...For example
  • 哇,谢谢 XD。我现在觉得自己像个白痴。

标签: c function crash switch-statement


【解决方案1】:

scanf的误用

线路:scanf("%d", x);

应该是scanf("%d", &amp;x);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多