if语句使用Linux Ncurses进行分段错误

Ime*_*eba 1 c++ linux ncurses

我的代码工作正常,直到第27行(if(answer == answer2)),当它到达程序崩溃并在我的终端写入"分段错误".

它应该工作,因为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;   
}
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 6

answer并且answer2是C++ std::string对象.你不能将它们用作任何类似函数的参数scanf.这将导致未定义的行为,因为"%s"格式需要指向a的指针char,即a char*.您的编译器应该向您发出警告.

如果要使用C++ std::string对象,那么一种解决方案是将它们调整为某个最大长度,然后获取指向字符串中第一个字符的指针(例如&answer[0]).

另一种解决方案是使用临时数组char作为目标,然后将std::string对象初始化为那些C风格的字符串.


虽然对于未定义的行为确实没有必要进行解释,但它会使您的程序格式错误且无效,这里有一个关于此处发生的事情的简短说明.

当您&answerscanw函数调用中使用eg 作为目标时,它使用指向字符串对象的指针作为char*,并将覆盖answer对象内部的内部数据.内部数据通常是指向实际字符串的指针,以及字符串的大小.当覆盖此数据时,指针将不再是预期的指针,它将指向您的进程可能无法使用的某些内存位置.这会在尝试取消引用指针时导致崩溃(在比较中完成).