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