简体   繁体   English

访问冲突读取位置c ++

[英]access violation reading location c++

I'm writing a program that print the full english name of the number inputted by the user. 我正在写一个程序,打印用户输入的数字的全英文名称。 it's not a complete program but i keep getting an error: 这不是一个完整的程序,但我不断收到错误消息:

First-chance exception at 0x00b02c76 in Programming Challenge 14.1.exe: 0xC0000005: Access violation reading location 0xcccccd80. 编程挑战14.1.exe中0x00b02c76的第一次机会异常:0xC0000005:访问冲突读取位置0xcccccd80。 Unhandled exception at 0x00b02c76 in Programming Challenge 14.1.exe: 0xC0000005: Access violation reading location 0xcccccd80. 编程挑战14.1.exe中0x00b02c76的未处理异常:0xC0000005:访问冲突读取位置0xcccccd80。

I've tried looking around and couldn't find anything of use to me. 我尝试环顾四周,找不到对我有用的东西。 here this the program: 这里这个程序:

header file: 头文件:

#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

and this is the main program, im just using a constructor to assign a value to number to make debugging a little faster 这是主程序,我只是使用构造函数为number赋值,以加快调试速度

#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;
}

by the way this is vc++ for the compiler 顺便说一下,这是用于编译器的vc ++

ill answer any questions as this isnt really too organized 我不能回答任何问题,因为这真的不是太有条理

one容纳18个元素,但是您可以在其中放置19个元素。

const int SIZE1 = 18;

Valid array index for the array of SIZE1 are 0 to 17. In general, valid indexes for an array of size N are 0 to N-1 . SIZE1数组的有效数组索引为0到17。通常,大小为N的数组的有效索引为0N-1

I recommend using std::vector<std::string> . 我建议使用std::vector<std::string>

Two things here: 这里有两件事:

You are not calling "initializeArray()" at all. 您根本没有调用“ initializeArray()”。 So when you are trying to access the array there is nothing there. 因此,当您尝试访问阵列时,那里什么也没有。 I would recommend calling it in the constructor. 我建议在构造函数中调用它。 Like this: 像这样:

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

Second, is what the guys above said. 第二,上面的家伙说的是。 You have an incorrect value for the size of your array since you are trying to assign 19 values to an array of size 18. Just to be really sure lets make the size bigger than we expect and you can adjust later: 您正试图为大小为18的数组分配19个值,因此数组的大小值不正确。请务必确保使大小大于我们的预期,然后可以进行调整:

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

Additionally, See your determine()? 另外,请参见您的define()? instead of using a for loop why don't you go: 而不是使用for循环,为什么不呢?

string name = one[number];

EDIT: Wow there was another thing I missed...you have declared your array pointer variable twice and so it's actually going out of scope thinking you want to make some local versions. 编辑:哇,我还错过了另一件事...您已经两次声明了数组指针变量,因此它实际上超出了范围,以为您想创建一些本地版本。 Look at my adjusted implementation of your constructor above again. 再次在上方查看我对构造函数的调整后实现。 See how I've removed the "String *" from before the variable names. 看看我如何从变量名前面删除“ String *”。

The variable "one" and "ten" have been changed from string pointers to vectors holding strings. 变量“一”和“十”已从字符串指针更改为包含字符串的向量。 Called the initializeArray within the constructor. 在构造函数中调用了initializeArray。 Changed the way the name string was being assigned the new string. 更改了为名称字符串分配新字符串的方式。 Here is the working code. 这是工作代码。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM