首页 > 解决方案 > 在 C++ 中访问指针类型 char 数组

问题描述

我正在开发一个键盘程序,它可以像旧翻盖手机一样将字符输入到数组中。对于这个任务,我需要使用指针类型,所以我不能只将数组更改为普通的 char 类型来解决问题。我遇到的问题是,在我输入第一个字符后,比如说“55”,程序正确地将“J”存储到“月”数组的第一个槽中。但是,下次我输入一个值时,它会运行正确的 switch case,但是它给了我访问第二个索引的访问冲突month[1]和休息。我确保我的输入在 switch 结束时被清除,并且它正在尝试运行正确的案例,所以我确信它与 switch 语句无关。我确信这与我对指针的误解有关。我不明白为什么它不输入第二个字符,但输入第一个字符没有问题。

我将在下面附上相关代码。

标题:

#include<string>
#include<iostream>
using namespace std;
class keypad
{

private:
    char* month[45];
    int* input;
    int index = 0;

public:
    keypad();
    ~keypad();
    void getinput(int);
    void getmonth();
    void print()const;
    void clear();


};

执行:

#include "keypad.h"

keypad::keypad() {
    *month = new char;
    input = new int;

}

keypad::~keypad() {
    delete month;
    delete input;
    input = NULL;
    *month = NULL;
}


void keypad::getinput(int a) {

    *input = a;

}


void keypad::getmonth() {

    switch (*input) {
    case(2):
        *month[index] = '2'; //The exception would be thrown right here if my second input is 2.
        index++;

        break;
    case(22):
        *month[index] = 'A';
        index++;

        break;
    case(222):
        *month[index] = 'B';
        index++;

        break;
    case(2222):
        *month[index] = 'C';
        index++;

        break;
    case(3):
        *month[index] = '3';
        index++;

        break;
    case(33):
        *month[index] = 'D';
        index++;

        break;
    case(333):
        *month[index] = 'E';
        index++;

        break;
    case(3333):
        *month[index] = 'F';
        index++;

        break;
    case(4):
        *month[index] = '4';
        index++;

        break;
    case(44):
        *month[index] = 'G';
        index++;

        break;
    case(444):
        *month[index] = 'H';
        index++;

        break;
    case(4444):
        *month[index] = 'I';
        index++;

        break;
    case(5):
        *month[index] = '5';
        index++;

        break;
    case(55):
        *month[index] = 'J';
        index++;

        break;
    case(555):
        *month[index] = 'K';
        index++;

        break;
    case(5555):
        *month[index] = 'L';
        index++;

        break;
    case(6):
        *month[index] = '6';
        index++;

        break;
    case(66):
        *month[index] = 'M';
        index++;

        break;
    case(666):
        *month[index] = 'N';
        index++;

        break;
    case(6666):
        *month[index] = 'O';
        index++;

        break;
    case(7):
        *month[index] = '7';
        index++;

        break;
    case(77):
        *month[index] = 'P';
        index++;

        break;
    case(777):
        *month[index] = 'Q';
        index++;

        break;
    case(7777):
        *month[index] = 'R';
        index++;

        break;
    case(77777):
        *month[index] = 'S';
        index++;

        break;
    case(8):
        *month[index] = '8';
        index++;

        break;
    case(88):
        *month[index] = 'T';
        index++;

        break;
    case(888):
        *month[index] = 'U';
        index++;

        break;
    case(8888):
        *month[index] = 'V';
        index++;

        break;
    case(9):
        *month[index] = 'W';
        index++;

        break;
    case(99):
        *month[index] = 'X';
        index++;

        break;
    case(999):
        *month[index] = 'Y';
        index++;

        break;
    case(9999):
        *month[index] = 'Z';
        index++;

        break;
    case(1):
        *month[index] = '1';
        index++;

        break;
    default:
        break;
    }
    *input = NULL;
}

void keypad::print()const{

    cout << *month[index - 1] << endl;

}

void keypad::clear() {
    delete month;
    *month = NULL;

}

主要的:

int main() {

    cout << "READ ME" << endl;
    cout << "For the purposes of this program, assume that ABC is on the number '2'." << endl;
    cout << "Also, assume that the first press of any number key will input the number itself." << endl;
    cout << "If your input is invalid, a character will not be saved, you may hit enter to restart the input for that character." << endl;
    cout << "You will input the number for a single character then press enter to save that character. These must be done in order." << endl;
    cout << "At any time after inputting a character, you may exit the program or clear the memory and input another word." << endl;
    int tempinput;

    keypad a;
    bool loop = true;
    bool endword = false;
    bool toclear = false;
    while (loop) {
        cout << "input the numeric code for your desired character, EX: 22 = 'A'." << endl;
        cin >> tempinput;
        a.getinput(tempinput);
        a.getmonth();
        a.print();
    }
}

主要问题发生在实现文件中,它将在为数组分配字符的行上引发异常。但是,这只发生在第二个字符上,第一个字符的存储没有问题。

这是 Visual Studio 中的错误

编辑:

我更改了构造函数代码以通过创建一个 for 循环将每个索引初始化为新字符来初始化所有索引,month而不仅仅是一个。month[i]

我现在有另一个问题,我觉得如果我只是在这里问会更聪明,因为它是同一个项目。什么可能导致无法写入 ofstream 对象?唯一一次使用它是我的主要,我最后关闭了它。

int tempinput;

    keypad a;
    ofstream outfile;
    outfile.open("output.txt");

    while (true) {
        cout << "input the numeric code for your desired character, EX: 22 = 'A'." << endl;
        cin >> tempinput;
        outfile << tempinput;
        a.getinput(tempinput);
        a.getmonth();
        a.print();

    }

    outfile.close();
}

标签: c++arrayspointers

解决方案


推荐阅读