Insert update delete element in array cpp code

Insert update delete element in array cpp code
Di Posting Oleh : Simple Learning
Kategori : array database delete insert update values

Write a c++ program which manages data in array having insert, update and delete value functions.
 Program should be menu based properly asking for user to enter an option and on wrong or invalid input program should display proper message and ask user to enter value again.
You can use any C++ compiler.

Cpp code should contain
  • Proper Menu Option Display. Hint use do while loop for menu
  • Insert Values   Function
  • Update Values Function
  • Delete Values Function
  • Proper message to every user action
This code includes the concept of

 Code Explanation
  • This code is managing a small database in array like insert, update and delete integer values in array
  • Program has global array declaration of size 10. Global declaration allows it accessible inside any function
  • A function name "Default values" is initializing all array indexes by default value of -1
  • A function name "Display Array" displays the array values using for loop
  • To manage the menu option do while is used and few if else statement within the loop to call a specific function on user selection
  • To restrict user to enter valid option recursion is used in two functions so if user enter invalid option For example array size is 10 and user select index number 11 then a message will be displayed function will call itself and user has to enter the option again
 Compiler used: CodeBlocks C++ Compiler

cpp code:
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. //Global Array Declaration
  5. int array[10];
  6. void DisplayArray(){
  7. for (int i=0;i<10;i++)
  8.      cout<< "Array [ "<<i<<" ] = "<<array[i]<<endl;
  9. }
  10. void SetDefaultValues(){
  11.     cout<<"Defalult Values :"<<endl;
  12. for(int i=0;i<10;i++)
  13.         {
  14.          array[i]=-1;
  15.      cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
  16.         }
  17. }
  18. void InsertValues(){
  19.     cout<<"Enter 10 Values "<<endl;
  20. for(int i=0;i<10;i++)
  21.         {
  22.         cin>>array[i];
  23.         }
  24.    cout<<"\n\t\t\tArray Values Inserted...  Successfully "<<endl;
  25. }
  26. void DeleteValues(){
  27.     cout<<"Enter the Index Number To Delete Value :";
  28.     int index;
  29.     cin>>index;
  30.     if(index>9||index<0)
  31.     {
  32.         cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
  33.         DeleteValues();// Recall The Function it self
  34.     }
  35.     else
  36.     {
  37.         array[index]=-1;
  38.     }
  39.     cout<<"\n\t\t\tArray Value Deleted...  Successfully "<<endl;
  40. }
  41. void UpdateValues(){
  42.     cout<<"Enter Index Number to Update Value :";
  43.     int index;
  44.     cin>>index;
  45.     if(index>9||index<0)
  46.     {
  47.         cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
  48.         UpdateValues();// Recall The Function it self
  49.     }
  50.     else
  51.     {
  52.  cout<<"Enter the New Value For Index array[ "<<index<<" ] = ";
  53.         cin>>array[index];
  54.   cout<<"\n\t\t\tArray Updated...  Successfully "<<endl;
  55.     }
  56. }
  57. int main()
  58. {
  59.     char option;
  60.     SetDefaultValues();
  61.   
  62.     do
  63.     {
  64.     cout<<"\t\t\tEnter 1 to Enter  Values\n\t\t\tEnter 2 to Update Values\n\t\t\tEnter 3 to Delete Values\n\n\t\t\t or Enter E to EXIT\n\n\t\t\t  Enter Option: ->  ";
  65.     cin>>option;
  66.     if(option=='1')
  67.       {
  68.    cout<<"Insert Function Called"<<endl;
  69.        InsertValues();
  70.        cout<<"Inserted Values :"<<endl;
  71.        DisplayArray();
  72.       }
  73.     else if(option=='2')
  74.       {
  75.        UpdateValues();
  76.        cout<<"Updated Array :"<<endl;
  77.        DisplayArray();
  78.       }
  79.     else if(option=='3')
  80.       {
  81.        DeleteValues();
  82.        cout<<"Array After Deleting Values :"<<endl;
  83.        DisplayArray();
  84.       }
  85.     else if(option!='e'&&option!='E')
  86.       {
  87.    cout<<"\n\n\t\t\tSelect A Valid Option From Below\n\n";
  88.       }
  89.     }while(option!='e'&&option!='E');
  90.     system("cls");// To Clear The Screen
  91.     cout<<"\n\n\n\n\n\n\n\n\n\n\t\tProgram Ended Press Any Key To Exit Screen.....\n\n\n\n\n\n\n\n\n\n\n\n"<<endl;
  92.     return 0;
  93. }

sample input outputs (codeBocks Console Screen Shots)
Program Menu Display
simple array database cpp code with menu


Update Array Values
update value in array cpp
Exit Option Selection
insert update delete program end screen cpp code


This program is helpful to manage a menu base database using array and shows how to prevent from a wrong input. Its is of very basic level to introduce the simplest database management using array. It is recommended to copy the code make changes in it and examine output.

See more simple examples here. Cpp Code Example

0 Response to "Insert update delete element in array cpp code"

Post a Comment