执行我的代码时出现分段错误(核心转储)错误
Getting a Segmentation fault (core dumped) error when I execute my code
我正在为我的 C 编程做一个项目 class 一切都在进行,直到我在第 22 行添加了一个 while 循环以便 运行 一遍又一遍地打印代码基于用户输入的三角形。代码的第一次迭代仍然有效,但是当它需要输入来确定如何更新代码并再次 运行 时,我遇到了段错误,我不明白为什么。我的代码如下:
#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");
}
scanf(" %c", menuChoice);
显然不正确,你知道如何从你上传的代码中的其他地方调用它。你要
scanf(" %c", &menuChoice);
我没有发现任何其他类似的错误,并且此代码中没有其他指针构造,因此应该到此结束。
我正在为我的 C 编程做一个项目 class 一切都在进行,直到我在第 22 行添加了一个 while 循环以便 运行 一遍又一遍地打印代码基于用户输入的三角形。代码的第一次迭代仍然有效,但是当它需要输入来确定如何更新代码并再次 运行 时,我遇到了段错误,我不明白为什么。我的代码如下:
#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");
}
scanf(" %c", menuChoice);
显然不正确,你知道如何从你上传的代码中的其他地方调用它。你要
scanf(" %c", &menuChoice);
我没有发现任何其他类似的错误,并且此代码中没有其他指针构造,因此应该到此结束。