将值从文本文件放入动态数组时的垃圾值

时间:2015-09-18 23:10:50

标签: c++ arrays dynamic

我的动态数组存在问题。我正在尝试将文本文件中的值输入到长度为动态数组,其中size是我的文本文件的大小。

出于某种原因,我正在重复-842150451,idk如果它的内容很简单,因为它没有正确地初始化它,或者我正在使用<<以错误的方式。

PS:我尝试过调试,即使打印出单个索引,我仍然可以获得该值。

我的代码:

    cout << "\nEnter a command and file ";
    getline(cin, line);
    command = line.substr(0, line.find(" "));
    names = line.substr(line.find(" ")+1);
    ifstream readText;
    readText.open(names += ".txt");
    int size;
    readText >> size;
    int *someArray = new int[size];


    if (command == ("Start"))
    {
        for (int i = 0; i < size; i++)
        {
            readText >> someArray[i];

        }
        for (int i = 0; i < size; i++)
        {
            cout << someArray[i] << endl;
        }

    }

    else if (command == "do this")
    {

        //used to test to see if it works
        cout << someArray[0] << endl;
        cout << someArray[1] << endl;
        cout << someArray[2] << endl;
     }

内容只是

之类的值
  

94032

     

233332

     

2121

     

112

     

423424

0 个答案:

没有答案