C 执行代码时出现分段错误(内核转储)错误

C 执行代码时出现分段错误(内核转储)错误,c,while-loop,segmentation-fault,C,While Loop,Segmentation Fault,我正在为我的C编程类做一个项目,一切都在进行中,直到我在第22行添加了一个while循环,以便反复运行代码,根据用户输入打印一个三角形。代码的第一次迭代仍然有效,但是当它需要输入来确定如何更新代码并再次运行时,我得到一个seg错误,我不明白为什么。我的代码如下: #include <stdio.h> #include <string.h> int main() { int size = 0; char symbol = ' '; int i =

我正在为我的C编程类做一个项目,一切都在进行中,直到我在第22行添加了一个while循环,以便反复运行代码,根据用户输入打印一个三角形。代码的第一次迭代仍然有效,但是当它需要输入来确定如何更新代码并再次运行时,我得到一个seg错误,我不明白为什么。我的代码如下:

#include <stdio.h>
#include <string.h>

int main() {
    int size = 0;
    char symbol = ' ';
    int i = 0;
    int space = 0;
    int n = 0;
    int oldSize = 0;
    char oldSymbol = ' ';
    char menuChoice = ' ';
    
    printf("Please enter the size of the triangle (2-40): ");
    scanf("%d", &size);
    printf("Please enter the character you would like to use: ");
    scanf(" %c", &symbol);
    
    oldSize = size;
    oldSymbol = symbol;
    
    while (menuChoice != 'Q' || menuChoice != 'q') {
        if (size < 2 || size > 40) {
            size = 5;
            symbol = '*';
            printf("Error: Size outside bounds. Printing default triangle: \n");
        }
        for(i = 1; i <= size + 1; ++i, n = 0) {
            for(space = 1; space <= size + 1 - i; ++space) {
                printf(" ");
            }
            while (n != i - 1) {
                printf("%c ", symbol);
                ++n;
            }
            printf("\n");
        }
    
        printf("G: Grow the current triangle\n");
        printf("S: Shrink the current triangle\n");
        printf("N: Draw a new triangle\n");
        printf("Q: Quit\n");
    
        scanf(" %c", menuChoice);
    
        if (menuChoice == 'G' || menuChoice == 'g') {
            size = size + 1;
            
        } else if (menuChoice == 'S' || menuChoice == 's') {
            size = size - 1;

        } else if (menuChoice == 'N' || menuChoice == 'n') {
            size = oldSize;
            symbol = oldSymbol;
            
        } else {
            printf("Error: please enter a valid argument.");
        }
    }
    printf("Goodbye");
}
#包括
#包括
int main(){
int size=0;
字符符号=“”;
int i=0;
int空间=0;
int n=0;
int oldSize=0;
字符oldSymbol='';
char menuChoice='';
printf(“请输入三角形的大小(2-40):”;
scanf(“%d”,大小(&S);
printf(“请输入您要使用的字符:”;
scanf(“%c”&符号);
oldSize=大小;
oldSymbol=符号;
while(menuChoice!=“Q”| menuChoice!=“Q”){
如果(尺寸<2 | |尺寸>40){
尺寸=5;
符号='*';
printf(“错误:大小超出边界。打印默认三角形:\n”);
}
对于(i=1;i
显然是不正确的,并且您知道如何在上载的代码中的其他位置调用它。您想要

scanf(" %c", &menuChoice);

我没有发现任何其他类似的错误,在这段代码中也没有其他指针构造,所以应该到此为止。

scanf(“%c”,menuChoice);
=>
scanf(“%c”,menuChoice);
打开编译器警告,它们会为您捕捉到的。好吧,我在那里没有什么可以为自己辩护的。
scanf(" %c", &menuChoice);