访问冲突读取位置c ++

时间:2013-09-06 01:24:51

标签: c++

我正在编写一个程序,用于打印用户输入的数字的完整英文名称。它不是一个完整的程序,但我一直收到错误:

编程挑战14.1.exe中0x00b02c76处的第一次机会异常:0xC0000005:访问冲突读取位置0xcccccd80。 编程挑战14.1.exe中0x00b02c76处的未处理异常:0xC0000005:访问冲突读取位置0xcccccd80。

我试过环顾四周,找不到任何有用的东西给我。这是程序:

头文件:

#ifndef NUMBERS_H
#define NUMBERS_H

#include <string>

using namespace std;
const int SIZE1 = 18;
const int SIZE2 = 8;

class Numbers
{
private:
    int number;
    string hundred;
    string thousand;
    string * one;
    string * ten;


public:
    Numbers(int num)
    {
        number = num;
        hundred = "hundred";
        thousand = "thousand";
        string * one = new string[SIZE1];
        string * ten = new string[SIZE2];
    }

    void initializeArray()
    {
        // Intialize array "one"
        one[0] = "zero";
        one[1] = "one";
        one[2] = "two";
        one[3] = "three";
        one[4] = "four";
        one[5] = "five";
        one[6] = "six";
        one[7] = "seven";
        one[8] = "eight";
        one[9] = "nine";
        one[10] = "eleven";
        one[11] = "twelve";
        one[12] = "thirteen";
        one[13] = "fourteen";
        one[14] = "fifteen";
        one[15] = "sixteen";
        one[16] = "seventeen";
        one[17] = "eighteen";
        one[18] = "nineteen";

        // Initialize the ten array

        ten[0] = "ten";
        ten[1] = "twenty";
        ten[2] = "thirty";
        ten[3] = "forty";
        ten[4] = "fifty";
        ten[5] = "sixty";
        ten[6] = "seventy";
        ten[7] = "eighty";
        ten[8] = "ninety";  
    }

    string determine()
    {
        string name = "";

        for (int i = 0; i <= number; i++)
        {
            if (number == i)
            {
                name = one[i];
            }
        }

        return name;
    }

    ~Numbers()
    {
        delete [] one;
        delete [] ten;
    }
};

#endif

这是主程序,我只是使用构造函数为数字赋值,使调试更快一点

#include <iostream>
#include "Numbers.h"

using namespace std;


int main()
{


    Numbers n(5);
    string name = n.determine();

    cout << "The number is " << name << endl;

    cin.ignore();
    cin.get();

    return 0;
}

顺便说一下这是编译器的vc ++

没有回答任何问题,因为这不是太有条理

4 个答案:

答案 0 :(得分:2)

one拥有18个元素,但你在其中放置了19个元素。

答案 1 :(得分:2)

const int SIZE1 = 18;

SIZE1数组的有效数组索引为0到17.通常,大小 N 的数组的有效索引 0 N-1

我建议使用std::vector<std::string>

答案 2 :(得分:1)

这里有两件事:

你根本不是在调用“initializeArray()”。因此,当您尝试访问阵列时,那里什么都没有。我建议在构造函数中调用它。像这样:

Numbers(int num)
{
    number = num;
    hundred = "hundred";
    thousand = "thousand";
    one = new string[SIZE1];
    ten = new string[SIZE2];
    initializeArray();
}

其次,上面的人说的是什么。由于您尝试将19个值分配给大小为18的数组,因此数组大小的值不正确。只是为了确保让大小超出预期,您可以稍后进行调整:

const int SIZE1 = 20;
const int SIZE2 = 20;

此外,请参阅您的确定()?而不是使用for循环你为什么不去:

string name = one[number];
编辑:哇,还有另外一件事我错过了......你已经将你的数组指针变量声明了两次,所以它实际上已超出范围,认为你想制作一些本地版本。再看看上面构造函数的调整后的实现。看看我如何从变量名之前删除“String *”。

答案 3 :(得分:0)

变量“one”和“ten”已从字符串指针更改为包含字符串的向量。在构造函数中调用initializeArray。更改了为新字符串分配名称字符串的方式。这是工作代码。

class Numbers
{
private:
    int number;
    string hundred;
    string thousand;
    vector<string> one;
    vector<string> ten;


public:
    Numbers(int num)
    {
        number = num;
        hundred = "hundred";
        thousand = "thousand";
        initializeArray();
    }

    void initializeArray()
    {

        one.push_back("zero");
        one.push_back("one");
        one.push_back( "two");
        one.push_back("three");
        one.push_back("four");
        one.push_back("five");
        one.push_back("six");
        one.push_back("seven");
        one.push_back("eight");
        one.push_back("nine");
        one.push_back("eleven");
        one.push_back("twelve");
        one.push_back("thirteen");
        one.push_back("fourteen");
        one.push_back("fifteen");
        one.push_back("sixteen");
        one.push_back("seventeen");
        one.push_back("eighteen");
        one.push_back("nineteen");

        // Initialize the ten array

        ten.push_back("ten");
        ten.push_back("twenty");
        ten.push_back("thirty");
        ten.push_back("forty");
        ten.push_back("fifty");
        ten.push_back("sixty");
        ten.push_back("seventy");
        ten.push_back("eighty");
        ten.push_back("ninety");  
    }

    string determine()
    {
        string name("");
        for (int i = 0; i <= number; i++)
        {
            if (number == i)
            {
               auto iter = one.begin();
               iter += i;
               name.assign(*iter);
            }
        }

        return name;
    }

    ~Numbers()
    {

    }
};


int main()
{

    Numbers n(5);
    string name = n.determine();

    cout << "The number is " << name << endl;

    cin.ignore();
    cin.get();

    return 0;
}