CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2002
    Location
    New York City
    Posts
    9

    Please help me C++ classes & object problem!

    Hi All,
    This is Ravi. I was trying one searching and sorting problem using classes, functions and objects. I am not getting any errors but i am getting some warnings. Please check my problem when you feel free. Please help me in this regard. Please let me know where did i mistake? I'll be appreciated your help in this regard. Take care.Bye for now.
    with best regards
    Ravi.
    Below the problem is :


    // Searching and Sorting Array Elements Using Classes and Objects:
    #include <iostream.h>
    #include <iomanip.h>



    class searchSort {
    private:
    int choice;
    int choiceCreate;
    int i, size;

    public:
    void CreateMainMenu();
    void IntArray();
    void FloatArray();
    void DoubleArray();
    void CharArray();
    void ArraySearch();
    void ArraySort();
    void ArrayExit();
    int get_size();
    int linearSearch(int[], int, int);
    };


    void searchSort::CreateMainMenu()
    {
    cout << "1 Creates an array depends on its data type" << endl;
    cout << "2 Search for the elements of an array" << endl;
    cout << "3 Sorting an exsting array" << endl;
    cout << "4 Exit from the program" << endl;

    int choice;
    cin >> choice;

    if(choice = 1)
    {
    int choiceCreate ;
    cout << "\t\t Array Menu" << endl;
    cout << "\t\t1 create int array" << endl;
    cout << "\t\t2 create float array" << endl;
    cout << "\t\t3 create double array" << endl;
    cout << "\t\t4 create char array" << endl;
    cout << "\t\t Enter Selection :\t";
    cin >> choiceCreate;
    //checking choice bounding
    if(choiceCreate <1 || choiceCreate>4)
    {
    cerr << "Invalid entry \n";
    //return 1;
    }
    }
    }


    void searchSort::IntArray()
    {
    if(choiceCreate == 1)
    cout << endl << "You entered integer values" << endl;

    int *x;
    size=get_size();
    x = new int[size];
    for(i=0; i<size; i++)
    {
    cout << " Enter Element " << i+1 <<":\t";
    cin >> x[i];
    }

    for(i=0; i<size; i++)
    cout << x[i] << "\t ";
    cout << "The size of the array is =" << size << endl ;
    delete [] x;
    }


    void searchSort::FloatArray()
    {
    if(choiceCreate == 2)
    cout << endl << "You entered float values" << endl;

    float *y;
    size=get_size();
    y = new float[size];
    for(i=0; i<size; i++)
    {
    cout << " Enter Element " << i+1 <<":\t";
    cin >> y[i];
    }

    for(i=0; i<size; i++)
    cout << y[i] << "\t ";
    cout << "The size of the array is =" << size << endl ;
    delete [] y;
    }


    void searchSort:oubleArray()
    {
    if(choiceCreate == 3)
    cout << "You entered double values" << endl;

    double *z;
    size=get_size();
    z = new double[size];
    for(i=0; i<size; i++)
    {
    cout << " Enter Element " << i+1<<":\t";
    cin >> z[i];
    }
    for(i=0; i<size; i++)
    cout << z[i] << "\t ";
    cout << "The size of the array is =" << size << endl ;
    delete [] z;
    }

    void searchSort::CharArray()
    {
    if(choiceCreate == 4)
    cout << "You entered character values" << endl;

    char *c;
    size=get_size();
    c = new char[size];
    for(i=0; i<size; i++)
    {
    cout << " Enter Element " << i+1<<":\t";
    cin >> c[i];
    }

    for(i=0; i<size; i++)
    cout << c[i] << "\t ";
    cout << "The size of the array is =" << size << endl ;
    delete [] c;

    }

    void searchSort::ArraySearch()
    {
    if(choice = 2)
    {
    int *z;
    int size,searchKey;
    cout << "Search for an element in the array:" << endl;
    size=get_size();
    z= new int[size];
    for(int i=0; i<size; i++)
    {
    cout << " Enter Element " << i+1<<":\t";
    cin >> z[i];
    }
    cout << "Enter integer search key:" << endl;
    cin >> searchKey;
    int element = linearSearch(z, searchKey, size);

    if(element >0)
    cout << "Found value at index \t" << element << endl;
    else
    cout << "Value not found" << endl;
    }
    }

    void searchSort::ArraySort()
    {
    if(choice = 3)
    {
    int i, *a;
    int hold;
    int arraySize;

    cout << "Enter the array size:" << endl;
    cin >> arraySize;

    cout << "Enter elements for sorting" << endl;
    a = new int[arraySize];
    for(i=0; i<arraySize; i++)
    {
    cout << "Enter element:" << i+1 << ":\t";
    cin >> a[i];
    }

    cout << "Data items in origional order" << endl;
    for(i = 0; i<arraySize; i++)
    cout << setw(4) << a[i];

    for(int pass =1; pass <arraySize; pass++)
    for(i=0; i<arraySize-1; i++)
    if(a[i] > a[i+1])
    {
    hold = a[i];
    a[i] = a[i+1];
    a[i+1] = hold;
    }
    cout << endl << "Data items in ascending order" << endl;
    for(i=0; i<arraySize; i++)
    cout << setw(4) << a[i];
    cout << endl;
    // return 0;
    }

    }

    void searchSort::ArrayExit()
    {
    if(choice = 4)
    {
    cout << " You typed 4" << endl;
    cout << " Exit from the program" << endl;
    }
    }


    int searchSort::get_size()
    {
    int size;
    cout << "Enter the size of the array " << endl;
    cin >> size;
    return size;
    }


    int searchSort::linearSearch(int array[], int key, int sizeOfArray)
    {
    for(int n=0; n < sizeOfArray; n++)
    if(array[n] == key)
    return n;
    return -1;
    }


    int main()
    {
    int *z, searchKey, size;
    searchSort ss;
    ss.CreateMainMenu();
    ss.IntArray();
    ss.FloatArray();
    ss.DoubleArray();
    ss.CharArray();
    ss.ArraySearch();
    ss.ArraySort();
    ss.ArrayExit();
    ss.get_size();
    ss.linearSearch(z, searchKey, size);
    return 0;
    }
    Ravi Billakanti
    New York
    203-530-7200 (M)

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    instead of re-inventing the whole loop and writing the code on your own why don't you use the STL which is absolutely suitable for this type of stuffs..
    Paresh

  3. #3
    Join Date
    Dec 2002
    Posts
    1,050
    Hi Ravi

    Is it warning you that variables are being used before being
    initialized ??

    int *z, searchKey, size;
    These should be initialized before being used.

    As they are passed to ..
    int linearSearch(int[], int, int);
    they all must have some value. Certainly the last two
    parameters MUST have some value, or there is no sence in using
    them as parameters, they are solely IN values.

    mdmd

  4. #4
    Join Date
    Apr 1999
    Location
    Potsdam
    Posts
    110
    You should always initialize variables before using them. You should also create a contructor to initialize the member variables in your classes, even if they are just build-in types.
    Then consider following lines:

    void searchSort::ArraySearch()
    {
    if(choice = 2) // assignment == TRUE
    ...

    The if-clause is always true in this case, since it is an assignment not a comparision. I'm sure you wanted to write:

    if (choice == 2)

    You also made this here:

    void searchSort::ArraySort()
    {
    if(choice = 3) // assignment == TRUE

    void searchSort::ArrayExit()
    {
    if(choice = 4) // assignment == TRUE

    You also forgot to delete your allocated memory in functions ArraySearch() and ArraySort().

  5. #5
    Join Date
    Aug 2002
    Location
    VA, USA
    Posts
    137
    You should always initialize variables before using them. You should also create a contructor to initialize the member variables in your classes, even if they are just build-in types.
    I just wanted to emphasize what was stated above. Its the
    POD types that absolutely need initialization in the container
    class constructor. Some class member variables don't need
    explicit initialization because it is taken care of in their own
    constructors when the contianer class is created. The POD types
    aren't initialized in their constructors. I can think of a couple
    reasons why they aren't off the top of my head: efficiency
    concerns, and the question of what is the right initialization value
    for a POD.

    regards, willchop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured