Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

CRUD using array with File Handling

This code is my project for this sem, but I don't know what to do in option 6 and 7. Please Help me!

If option 6 is selected, the program should shuffle the elements in the array. Using the display routine, the program then displays the elements in their new arrangement.

If option 7 is selected, the program would ask the user for the file name to which the elements are to be saved. After the file name input, the file must then be created in the directory in which the .cpp file is located. The file should contain all the elements in the array.

#include <fstream>
#include <iostream>

using namespace std;

bool isfull(int arrsize,int arr[]);
void add(int arrsize,int arr[],int val);
void disp(int arrsize,int arr[]);
bool isr(int arrsize,int arr[],int val);
void src(int arrsize,int arr[],int val);
void del(int arrsize,int arr[],int valdel);
void toedit(int arrsize,int arr[],int valedit);
/*
void shuf (int arrsize, int arr[],int val );
void save(int arrsize, int arr[], int val);
*/

int main(){

    int choice;
    int arrsize=20 ;
    int arr[arrsize];
    int valdel;
    int valedit;

    //to initialize all values to zero
    for(int x=0;x<arrsize;x++){
            arr[x]=0;

    }

    do{
        cout<<"\n";
        cout<<"Main Menu"<<endl;
        cout<<"[1]Add an element"<<endl; 
        cout<<"[2]Search"<<endl; // Display
        cout<<"[3]Display elements "<<endl; 
        cout<<"[4]Delete an element"<<endl; 
        cout<<"[5]Edit an element"<<endl;   
        cout<<"[6]Shuffle elements"<<endl;
        cout<<"[7]Save to file"<<endl;
        cout<<"[8]Exit"<<endl;


        cout<<"Enter your choice: ";
        cin>>choice;

        if(choice==1){
            int val=0;
            cout<<"Enter value to add: ";
            cin>>val;
            bool result=isfull(arrsize,arr);
            if(result==true){
                cout<<"Array is full";
                cout<<"\n";
            }else{
                add(arrsize,arr,val);
                cout<<"Successfully added";
                cout<<"\n";
            }
        }

        else if(choice==2){
            int val=0;
            cout<<"Enter value to search: ";
            cin>>val;
            bool result=isr(arrsize,arr,val);
            if(result==true){
                cout<<"Element is found at position ";
                src(arrsize,arr,val);
                cout<<"\n";
            }else{
                cout<<"The value does not exist in the array";
                cout<<"\n";
            }
        }       

        else if(choice==3){
            cout<<"Values in your array: ";
            disp(arrsize,arr);
            cout<<"\n";
        }

        else if(choice==4){
            del(arrsize,arr,valdel);
            cout<<"\n";
        }

        else if(choice==5){
            toedit(arrsize,arr,valedit);
            cout<<"\n";
        }

        else if (choice==6){

        }

        else if (choice==7){

        }


    }while(choice!=8);

    return 0;
}

//FOrDELETE
void del(int arrsize,int arr[],int valdel){
bool deleted=true;
int arr2[arrsize];
int count=0;
    cout<<"Enter Val to delete:";
    cin>>valdel;

    for(int x=0;x<arrsize;x++){
        if(arr[x]==valdel){
            arr[x]=0;
            cout<<"Succesfully Deleted!";
            deleted=true;           

    }       

        if(deleted==false){
            cout<<"Error!! The Value not existed!";
            break;  
        }   
    }

    for(int x=0; x<arrsize ;x++){
        if(arr[x]!=0){
                arr2[count]=arr[x];
                    count++;
    }
    }

//  for(int x=0; x<count2 ;x++){
        arr[arrsize]=arr2[arrsize];
        //cout<<arr2[x]<<",";
//} 
}

//editing Values
void toedit(int arrsize,int arr[],int valedit){
    int index=0;
    cout<<"Enter Index to Edit:";
    cin>>index;
    cout<<"Enter Val to Edit:";
    cin>>valedit;

    arr[index-1]=valedit;
}


//to determine if the array is full
bool isfull(int arrsize,int arr[]){
    bool punona=true;
    for(int x=0;x<arrsize;x++){
        if(arr[x]==0){
            punona=false;
            break;
        }
    }
    return punona; //PUNONA means 'FULL ALREADY'
}

//for adding value to array
void add(int arrsize,int arr[],int val){
    for(int x=0;x<arrsize;x++){
        if(arr[x]==0){
            arr[x]=val;
            break;
        }
    }

}

//to display all the value in the array
void disp(int arrsize,int arr[]){
    for(int x=0;x<arrsize;x++){
        if(arr[x]==0){
        }else{
            cout<<arr[x]<<", ";
        }
    }
}

//to determine if the value exist in the array
bool isr(int arrsize,int arr[],int val){
    bool found=false;
    for(int x=0;x<arrsize;x++){
        if(val==arr[x]){
            found=true;
        }
    }
    return found;
}

//for printing the index of the value searched
void src(int arrsize,int arr[],int val){
    for(int x=0;x<arrsize;x++){
        if(val==arr[x]){
            cout<<x+1<<", ";
        }
    }
}

Comments