0

In the below code on method called get_array() problem occurs because the capacity variable change to after first input given by user in the console and capacity value assign to what value given by user

I am not sure what's happening but it's kind of irritating me or did I missed some basic knowledge ? Please help

#Software

I am using CodeBlocks v17.12

#OS

Windows 10 Home

#include <iostream>

using namespace std;

class LinearSearch
{
    int my_arr[];
    int capacity, c, val;

public:
    LinearSearch()
    {
        this->capacity = 0;
        this->c = 0;
        this->val = 0;

    }
    void get_array()
    {
        cout << "Define number of elements in the array: ";
        cin >> this->capacity;

        for( int i = 0; i < this->capacity; i++ )
        {
            cout << "Enter value " << i+1 << " - " << this->capacity << " : ";
            cin >> this->my_arr[i];
        }

        for( int k = 0; k < capacity; k++ )
        {
            cout << my_arr[k] << endl;
        }

        cout << endl;
    }

    void search_value()
    {
        cout << endl << "Enter a value to search: ";
        cin >> this->val;

        for(int j = 0; j < capacity; j++)
        {
            if(my_arr[j]==val)
            {
                cout << endl << this->val << " value found in the array at index array[" << j << "] value number " << j+1 << "." << endl << endl;
                c++;
                break;
            }
        }

        if(this->c==0)
            cout<<endl<<this->val<<" value not found in the array."<<endl<<endl;
     }
};

int main()
{
    int choice;
    LinearSearch obj;

    while( true )
    {
        cout << "0) Exit\n1) Insert Data\n2) Search Data\n\nEnter Option: ";
        cin >> choice;

        switch( choice )
        {
        case 0:
            return 0;
        case 1:

            obj.get_array();

            break;
        case 2:

            obj.search_value();

            break;
        default:
            cout << "\nWrong Option!!\n\n";
            break;
        }
    }

    return 0;
}


Nobir
  • 11
  • 1
  • 1
    As I wrote in my answer, `int my_arr[];` is not valid C++ and neither valid C the way you wrote it. Which compiler did you use to manage to compile this? The major three that I tested all give errors: https://godbolt.org/z/oWakgD – walnut Feb 06 '20 at 22:38
  • GCC compiler not showing any error so I thought it's a valid . and also I know how to declare array without the size but I've try this way it doesn't show any error that's why I was thinking it's a valid one Now, my point is why change the `capacity` variable though it's a problem of an array declare ? – Nobir Feb 06 '20 at 22:39
  • 1
    You are right, older versions of GCC compile it without error, see https://stackoverflow.com/questions/38130887/flexible-array-error-with-gcc-6-but-not-with-gcc-4. It is still not allowed, though. That is a bug in the compiler. Best case scenario it will be considered a zero-length array, in which case you are accessing it out-of-bounds, causing [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). In C++, just because there is no error does not mean that the program is well-formed. – walnut Feb 06 '20 at 22:44
  • I recommned a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++. You *must* learn the language rules. Trial-and-error does not work in C++, because many rules will not result in any warning or error when violated and the program may *seem* to work even if it is wrong and has undefined behavior. – walnut Feb 06 '20 at 22:45
  • When you enter a capacity value, you should create array in memory. Something like `my_arr = new int[this->capacity];` Looks like your array has 0 elements. So when you try to fill it, you are actually corrupting data after my_arr variable. – ZeAL0T Feb 06 '20 at 23:01
  • Thank you so much :D for your reference – Nobir Feb 06 '20 at 23:02

1 Answers1

1

int my_arr[]; is not valid standard C++ and even in C it must appear only at the end of the member list as so-called flexible array member.

You should use std::vector<int> my_arr; instead. (Requires #include<vector>.)

You can then add new elements to it with my_arr.push_back(/*some number here*/); or you can resize the array with my_arr.resize(/*new size here*/);.

walnut
  • 21,629
  • 4
  • 23
  • 59