Thread: Why m I getting this error?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Why m I getting this error?

    So why is my switch not working. I am getting a error here
    switch (Sort_Type)
    error:'Sort_Type' : illegal use of this type as an expression


    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    #include<ctime>
    #include<cstdio>
    #include<fstream>
    #include"sortImplementation.cpp"
    //void CopyArray( int p[], int q[], int vec);
    const int vSize = 3;
    using namespace std;





    void CopyArray( int p[], int q[], int vec);//this is the function call.

    int main()
    {


    const int Size = 10;
    int vec[vSize]={6250,12500,25000};//,50000//,100000,200000,400000};
    enum Sort_Type{Quicksort,BubbleSort,SelectionSort,Inser tionsort};

    clock_t start,finish;
    clock_t startAll;

    int loBound=0;
    int hiBound=vSize-1;

    quick qq;
    int Numberstobesorted[] = {3,7,8,9,70,45,12,5,66,87};
    int Numbers=0;
    double duration;
    int j=0;
    int *p, *q;//p and q are the pointers to arrays.
    int iSize = 0;
    int i=0;
    int* a;
    // Size = Numberstobesorted[i];
    a = new int[Size];

    ifstream inFile;
    ofstream outFile;

    inFile.open("input1.txt");
    outFile.open("output1.txt");

    for(i =0; i<Size; i++)
    {
    qq.BubbleSort(a,iSize );




    }


    for(i =0; i<Size; i++)
    {
    qq.BubbleSort(a,iSize );

    outFile<<Numberstobesorted[i]<<" ";


    }



    cout<<" "<<"Bubble Sort"<<" "<<"Insertion"<<" "<<"Quick"<<" "<<"Selection"<<endl;


    //return 0;
    //}

    for(i=0; i<vSize; i++)
    {
    iSize = vec[i];
    p = new int[iSize];
    q = new int[iSize];
    for(int k = 0; k<vec[i]; k++)
    {
    p[k]=rand();
    }


    for(int ii=0; ii<=3;ii++)
    {
    memcpy(q, p, iSize*4);
    start=clock();


    switch (Sort_Type)
    {

    case 0:
    hiBound = iSize - 1;
    qq.Quicksort(q,loBound,hiBound);
    break;
    case 1:
    qq.BubbleSort(q, iSize );
    break;
    case 2:
    qq.SelectionSort(q, iSize);
    break;
    case 3:
    qq.Insertionsort(q, iSize);
    break;
    default: cout<<"Error"; break;
    }//end of switch function.

    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<" "<<setw(4)<<duration;

    }
    //Delete the array for the next sorting function.
    cout<<endl;
    delete[] p;
    delete[] q;

    }
    //cout<<duration<<endl;
    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<"Total Duration"<<duration<<endl;







    return 0;
    }//end of main
    //////////////////////////////////////////////////////////////////////
    //void CopyArray(int p[],int q[],int vec)//this is the function call.
    //{
    // int i;

    // for(i=0;i<vSize;i++)
    //p[i]=q[i];
    //}

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The way you've declared your enum indicates that you are going to use it as a type and you can't use types in conditional statements - switch(float) - only instances of these types.

    You can declare anonymous enums like -

    enum {Quicksort,BubbleSort,SelectionSort,Insertionsort} Sort_Type;


    so Sort_Type is now an instance of this enum and can be used in conditional statements.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    I tried this but I am getting errors for my output

    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    #include<ctime>
    #include<cstdio>
    #include<fstream>
    #include"sortImplementation.cpp"
    //void CopyArray( int p[], int q[], int vec);
    const int vSize = 3;
    using namespace std;





    void CopyArray( int p[], int q[], int vec);//this is the function call.

    int main()
    {

    enum {Quicksort,BubbleSort,SelectionSort,Insertionsort}
    Sort_Type;

    const int Size = 10;
    int vec[vSize]={6250,12500,25000};//,50000//,100000,200000,400000};

    clock_t start,finish;
    clock_t startAll;

    int loBound=0;
    int hiBound=vSize-1;

    quick qq;
    int Numberstobesorted[] = {3,7,8,9,70,45,12,5,66,87};
    int Numbers=0;
    double duration;
    int j=0;
    int *p, *q;//p and q are the pointers to arrays.
    int iSize = 0;
    int i=0;
    int* a;
    // Size = Numberstobesorted[i];
    a = new int[Size];

    ifstream inFile;
    ofstream outFile;

    inFile.open("input1.txt");
    outFile.open("output1.txt");

    for(i =0; i<Size; i++)
    {
    qq.BubbleSort(a,iSize );




    }


    for(i =0; i<Size; i++)
    {
    qq.BubbleSort(a,iSize );

    outFile<<Numberstobesorted[i]<<" ";


    }



    cout<<" "<<"Bubble Sort"<<" "<<"Insertion"<<" "<<"Quick"<<" "<<"Selection"<<endl;


    //return 0;
    //}

    for(i=0; i<vSize; i++)
    {
    iSize = vec[i];
    p = new int[iSize];
    q = new int[iSize];
    for(int k = 0; k<vec[i]; k++)
    {
    p[k]=rand();
    }


    for(int ii=0; ii<=3;ii++)
    {
    memcpy(q, p, iSize*4);
    start=clock();


    switch (Sort_Type)
    {

    case 0:
    hiBound = iSize - 1;
    qq.Quicksort(q,loBound,hiBound);
    break;
    case 1:
    qq.BubbleSort(q, iSize );
    break;
    case 2:
    qq.SelectionSort(q, iSize);
    break;
    case 3:
    qq.Insertionsort(q, iSize);
    break;
    default: cout<<"Error"; break;
    }//end of switch function.

    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<" "<<setw(4)<<duration;

    }
    //Delete the array for the next sorting function.
    cout<<endl;
    delete[] p;
    delete[] q;

    }
    //cout<<duration<<endl;
    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<"Total Duration"<<duration<<endl;







    return 0;
    }//end of main
    //////////////////////////////////////////////////////////////////////
    //void CopyArray(int p[],int q[],int vec)//this is the function call.
    //{
    // int i;

    // for(i=0;i<vSize;i++)
    //p[i]=q[i];
    //}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Repeating what zen said

    > enum {Quicksort,BubbleSort,SelectionSort,Insertionsort} Sort_Type;
    This just creates a new type - you can't switch types, only variables

    So do
    Sort_Type foo = Quicksort;

    Then you can do
    switch ( foo ) {
    &nbsp; case Quicksort:
    &nbsp; // etc

    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Would this syntax be correct?

    I am getting output with this:

    int Sort;
    enum {Quicksort,BubbleSort,SelectionSort,Insertionsort}
    Sort_Type;
    //Sort_Type Sort;
    Sort= Quicksort;



    Here is the program
    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    #include<ctime>
    #include<cstdio>
    #include<fstream>
    #include"sortImplementation.cpp"
    //void CopyArray( int p[], int q[], int vec);
    const int vSize = 3;
    using namespace std;





    void CopyArray( int p[], int q[], int vec);//this is the function call.

    int main()
    {

    int Sort;
    enum {Quicksort,BubbleSort,SelectionSort,Insertionsort}
    Sort_Type;
    //Sort_Type Sort;
    Sort= Quicksort;

    //Sort_Type

    const int Size = 10;
    int vec[vSize]={6250,12500,25000};//,50000//,100000,200000,400000};

    clock_t start,finish;
    clock_t startAll;

    int loBound=0;
    int hiBound=vSize-1;

    quick qq;
    int Numberstobesorted[] = {3,7,8,9,70,45,12,5,66,87};
    int Numbers=0;
    double duration;
    int j=0;
    int *p, *q;//p and q are the pointers to arrays.
    int iSize = 0;
    int i=0;
    int* a;
    // Size = Numberstobesorted[i];
    a = new int[Size];

    ifstream inFile;
    ofstream outFile;

    inFile.open("input1.txt");
    outFile.open("output1.txt");

    for(i =0; i<Size; i++)
    {
    qq.BubbleSort(a,iSize );




    }


    for(i =0; i<Size; i++)
    {
    qq.BubbleSort(a,iSize );

    outFile<<Numberstobesorted[i]<<" ";


    }



    cout<<" "<<"Bubble Sort"<<" "<<"Insertion"<<" "<<"Quick"<<" "<<"Selection"<<endl;


    //return 0;
    //}

    for(i=0; i<vSize; i++)
    {
    iSize = vec[i];
    p = new int[iSize];
    q = new int[iSize];
    for(int k = 0; k<vec[i]; k++)
    {
    p[k]=rand();
    }


    for(int ii=0; ii<=3;ii++)
    {
    memcpy(q, p, iSize*4);
    start=clock();


    switch (Sort)
    {

    case 0:
    hiBound = iSize - 1;
    qq.Quicksort(q,loBound,hiBound);
    break;
    case 1:
    qq.BubbleSort(q, iSize );
    break;
    case 2:
    qq.SelectionSort(q, iSize);
    break;
    case 3:
    qq.Insertionsort(q, iSize);
    break;
    default: cout<<"Error"; break;
    }//end of switch function.

    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<" "<<setw(4)<<duration;

    }
    //Delete the array for the next sorting function.
    cout<<endl;
    delete[] p;
    delete[] q;

    }
    //cout<<duration<<endl;
    finish=clock();
    duration=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<"Total Duration"<<duration<<endl;







    return 0;
    }//end of main
    //////////////////////////////////////////////////////////////////////
    //void CopyArray(int p[],int q[],int vec)//this is the function call.
    //{
    // int i;

    // for(i=0;i<vSize;i++)
    //p[i]=q[i];
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM