使用 Linux Ncurses 的 if 语句出现分段错误
Segmentation fault at if statement using Linux Ncurses
我的代码在第 27 行 (if(answer == answer2)
) 之前工作正常,当它到达那里时程序崩溃并在我的终端中写入 'segmentation fault'。
它应该可以工作,因为 if 语句比较两个字符串,如果这些字符串相同,它会执行一个操作,如果不相同,它会执行另一个操作,但不知道为什么它不起作用.
我是一名初级程序员。
代码如下:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ncurses.h>
using namespace std;
int main () {
char persentage = '%';
initscr();
curs_set(0);
string answer;
string answer2;
int tipi,tipi2;
move(8,0);
printw("Enter the phenotype of parent 1: ");
curs_set(1);
scanw("%s",&answer);
curs_set(0);
move(9,0);
printw("Is the phenotype fully dominant(AA) or partly(Aa)? (type '1' for AA, '2' for Aa): ");
curs_set(1);
scanw("%d",&tipi);
curs_set(0);
move(11,0);
printw("Enter the phenotype of parent 2: ");
curs_set(1);
scanw("%s",&answer2);
curs_set(0);
if(answer == answer2) {
move(12,0);
printw("Is the phenotype of parent 2 fully dominant(AA) or partly(Aa)? (type '1' for AA, '2' for Aa): ");
curs_set(1);
scanw("%d",&tipi2);
curs_set(0);
if (tipi == 1 && tipi2 == 1) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 100%c",answer,persentage);
}
if (tipi == 1 && tipi2 == 2) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 100%c",answer,persentage);
}
if (tipi == 2 && tipi2 == 1) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 100%c",answer,persentage);
}
if (tipi == 2 && tipi2 == 2) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 75%c",answer,persentage);
move(16,0);
printw("Persentage of a child's phenotype to be recessive phenotype is 25%c",persentage);
}
}
else {
if (tipi == 1) {
move(13,0);
printw("Persentage of a child's phenotype to be %s is 75%c",answer,persentage);
move(14,0);
printw("Persentage of a child's phenotype to be %s is 25%c",answer2,persentage);
}
if (tipi == 2) {
move(13,0);
printw("Persentage of a child's phenotype to be %s is 50%c",answer,persentage);
move(14,0);
printw("Persentage of a child's phenotype to be %s is 50%c",answer2,persentage);
}
}
}
refresh();
getch();
endwin();
return 0;
}
answer
和 answer2
是 C++ std::string
对象。您不能将它们用作任何 scanf
类函数的参数。这将导致 未定义的行为 ,因为 "%s"
格式需要一个指向 char
的指针,即 char*
。你的编译器应该已经向你发出警告。
如果你想使用 C++ std::string
对象,那么一个解决方案是你 resize 它们达到某个最大长度,然后获取指向 第一个字符的指针 在字符串中(例如 &answer[0]
)。
另一种解决方案是使用 char
的临时数组作为目标,然后将 std::string
对象初始化为那些 C 风格的字符串。
虽然 undefined behavior 除了使您的程序 格式不正确 和无效外,确实不需要任何解释,但这里是对此处发生的情况的简短解释。
当您使用例如&answer
作为 scanw
函数调用的目标,它使用指向字符串对象的指针作为 char*
,并将覆盖 answer
对象内的内部数据。该内部数据通常是指向实际字符串和字符串大小的指针。当这个数据被覆盖时,指针将不再是预期的,它会指向一些你的进程可能不可用的内存位置。尝试取消引用指针(在比较中完成)时,这将导致崩溃。
我的代码在第 27 行 (if(answer == answer2)
) 之前工作正常,当它到达那里时程序崩溃并在我的终端中写入 'segmentation fault'。
它应该可以工作,因为 if 语句比较两个字符串,如果这些字符串相同,它会执行一个操作,如果不相同,它会执行另一个操作,但不知道为什么它不起作用.
我是一名初级程序员。
代码如下:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ncurses.h>
using namespace std;
int main () {
char persentage = '%';
initscr();
curs_set(0);
string answer;
string answer2;
int tipi,tipi2;
move(8,0);
printw("Enter the phenotype of parent 1: ");
curs_set(1);
scanw("%s",&answer);
curs_set(0);
move(9,0);
printw("Is the phenotype fully dominant(AA) or partly(Aa)? (type '1' for AA, '2' for Aa): ");
curs_set(1);
scanw("%d",&tipi);
curs_set(0);
move(11,0);
printw("Enter the phenotype of parent 2: ");
curs_set(1);
scanw("%s",&answer2);
curs_set(0);
if(answer == answer2) {
move(12,0);
printw("Is the phenotype of parent 2 fully dominant(AA) or partly(Aa)? (type '1' for AA, '2' for Aa): ");
curs_set(1);
scanw("%d",&tipi2);
curs_set(0);
if (tipi == 1 && tipi2 == 1) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 100%c",answer,persentage);
}
if (tipi == 1 && tipi2 == 2) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 100%c",answer,persentage);
}
if (tipi == 2 && tipi2 == 1) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 100%c",answer,persentage);
}
if (tipi == 2 && tipi2 == 2) {
move(15,0);
printw("Persentage of a child's phenotype to be %s is 75%c",answer,persentage);
move(16,0);
printw("Persentage of a child's phenotype to be recessive phenotype is 25%c",persentage);
}
}
else {
if (tipi == 1) {
move(13,0);
printw("Persentage of a child's phenotype to be %s is 75%c",answer,persentage);
move(14,0);
printw("Persentage of a child's phenotype to be %s is 25%c",answer2,persentage);
}
if (tipi == 2) {
move(13,0);
printw("Persentage of a child's phenotype to be %s is 50%c",answer,persentage);
move(14,0);
printw("Persentage of a child's phenotype to be %s is 50%c",answer2,persentage);
}
}
}
refresh();
getch();
endwin();
return 0;
}
answer
和 answer2
是 C++ std::string
对象。您不能将它们用作任何 scanf
类函数的参数。这将导致 未定义的行为 ,因为 "%s"
格式需要一个指向 char
的指针,即 char*
。你的编译器应该已经向你发出警告。
如果你想使用 C++ std::string
对象,那么一个解决方案是你 resize 它们达到某个最大长度,然后获取指向 第一个字符的指针 在字符串中(例如 &answer[0]
)。
另一种解决方案是使用 char
的临时数组作为目标,然后将 std::string
对象初始化为那些 C 风格的字符串。
虽然 undefined behavior 除了使您的程序 格式不正确 和无效外,确实不需要任何解释,但这里是对此处发生的情况的简短解释。
当您使用例如&answer
作为 scanw
函数调用的目标,它使用指向字符串对象的指针作为 char*
,并将覆盖 answer
对象内的内部数据。该内部数据通常是指向实际字符串和字符串大小的指针。当这个数据被覆盖时,指针将不再是预期的,它会指向一些你的进程可能不可用的内存位置。尝试取消引用指针(在比较中完成)时,这将导致崩溃。