在动态数组中搜索

时间:2016-01-25 13:24:52

标签: c++ arrays list dynamic

当我在动态数组中搜索整数时,搜索功能不能正常工作,因为它始终显示其位于1.数据是否实际存在。

我实际上要做的是使用动态数据结构,我正在添加数据。删除,搜索并保存到txt文件。并加载回来。但问题是搜索。我使用了switch case,搜索是在Case 4。

#include<iostream>
#include<string>
#include<fstream> //to save file in text

using namespace std;

int main()
{
    int *p1;
    int size = 0;
    int counter = 0;
    p1 = new int[size];


    int userchoice;
    int i;
    int position;


    while (1)
    {
        cout << "Please enter your choice " << endl;
        cout << endl;
        cout << "To insert Press '1'" << endl;
        cout << "To Delete press '2'" << endl;
        cout << "To View press '3'" << endl;
        cout << "To Search press '4'" << endl;
        cout << "To Save Press '5'" << endl;
        cout << "To Load Previously saved Data press '6'" << endl;
        cout << "To Exit press '7'" << endl;
        cout << endl;
        cout << "Enter your choice: ";
        cin >> userchoice;


        switch (userchoice)  // User's selection from the menu
        {
        case 1: //Insert Number
            cout << "Enter a Number:  ";
            cin >> p1[size];
            counter++;
            size++; //Add's memory space
            break;

        case 2: //Delete Number

            int udelete;

            cout << "Enter a number to delete:  ";
            cin >> udelete; //User enters Number to be deleted

                            //Checking if the number is in an array.
            for (position = 0; position<size; position++)
            {
                if (p1[position] == udelete)
                    break;
            }
            if (position>size)
            {
                cout << "The number is not in the memory:  ";
                cout << endl;
                break;
            }
            for (i = position; i<size; i++) {
                p1[i] = p1[i + 1];
            }
            size--;
            cout << "Successfully Deleted!!! ";
            cout << endl;
            break;
        case 3: // View


            for (i = 0; i<size; i++)
            {
                cout << "Your data" << " " << i << " " << "-->" << p1[i] << endl;
            }
            break;

        case 4:
        {
            int usearch;

            cout << "Please enter the figure you would like to search ";
            cout << "->";
            cin >> usearch;

            for (i = 0; i>size; i++)
            {
                if (p1[size] == usearch)
                    break;
            }
            if (usearch == size)
            {
                cout << "not found. ";
            }
            cout << "Position at: " << i + 1 << endl;
            break;

        }



        case 5: // Save

        {
            ofstream save;
            save.open("Dynamicdata.txt", ofstream::out | ofstream::app);

            for (i = 0; i<size; i++)
            {
                save << p1[i] << endl;
            }

            save.close();
            cout << "File Saved " << endl;

            break;
        }

        case 6: //Read from File
        {


            string read;

            ifstream file_("Dynamicdata.txt");

            if (file_.is_open())
            {
                while (getline(file_, read))
                {
                    cout << read << "\n";
                }
                file_.close();
            }
            else
                cout << "File Not open" << endl;
            cin.get();
            break;

        }

        case 7:
        {
            return 0;
        }

        }
    }
}

2 个答案:

答案 0 :(得分:2)

您的问题是,您的数组大小为0.在此,您将size设置为0,然后将size设置为p1

int size=0;
int counter=0;
p1 = new int[size];

您需要将大小设置得更大,以便您可以在p1中实际存储元素,或者使用std::vector而不是使用数组和动态内存分配,并让它为您处理。< / p>

答案 1 :(得分:1)

代码具有未定义的行为,因为指针p1指向的动态分配的数组最初没有元素

int size=0;
^^^^^^^^^^
//...
p1 = new int[size]; // size is equal to 0

因此,在以下代码片段中,将数据写入p1[size]的尝试导致未定义的行为

case 1: //Insert Number
        cout<<"Enter a Number:  ";
        cin>>p1[size]; // undefined behaviour
        ^^^^^^^^^^^^^
        counter++;
        size++; //Add's memory space
        break;

您需要重新分配数组以为添加的新元素保留内存。

考虑到例如这个循环

        for (i = 0; i>size; i++)
                    ^^^^^^^
        {
            if (p1[size] == usearch)
                break;
        }

永远不会迭代,因为设置为零的变量i不能大于size,至少等于零。

编写

在逻辑上更正确
            if (p1[i] == usearch)
                ^^^^^

而不是

            if (p1[size] == usearch)
                ^^^^^^^^

因此这个if语句

        if (usearch == size)
            ^^^^^^^
        {
            cout << "not found. ";
        }

应替换为语句

        if (i == size)
            ^^
        {
            cout << "not found. ";
        }