11/10/22, 7:12 AM Insert update delete element in array cpp code ~ C++ Programming Tutorial for Beginners
You can use any C++ compiler.
C++ Programming Tutorial For Beginners
Cpp code
HOME
should contain
C++ SIMPLE EXAMPLES C++ PROJECTS C++ SHAPES CODE BUY C++ EBOOKS CPP PROGRAMMING TUTORIALS
Proper Menu Option Display. Hint use do while loop for
ABOUTmenu
US CONTACT US
Insert Values Function
Update Values Function
Delete Values Function
Proper message to every user action
This code includes the concept of
cpp array values management
Loops
functions
Recursive function calling
if else statement
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:
#include <iostream>
#include <stdlib.h>
Join Our Newsletter
using namespace std; Sign up today and be the first to get notified on new updates.
//Global Array Declaration
Enter your Email
int array[10];
void DisplayArray(){
Subscribe Now
for (int i=0;i<10;i++)
https://fahad-cprogramming.blogspot.com/2014/11/insert-update-delete-in-cpp-array-code.html 2/8
11/10/22, 7:12 AM Insert update delete element in array cpp code ~ C++ Programming Tutorial for Beginners
cout<< "Array [ "<<i<<" ] = "<<array[i]
C++ Programming
<<endl;
Tutorial For Beginners
HOME } C++ SIMPLE EXAMPLES C++ PROJECTS C++ SHAPES CODE BUY C++ EBOOKS CPP PROGRAMMING TUTORIALS
void SetDefaultValues(){
ABOUT US CONTACT US
cout<<"Defalult Values :"<<endl;
for(int i=0;i<10;i++)
{
array[i]=-1;
cout<<"array ["<<i<<"]"<<"= "<<array[i]
<<endl;
}
}
void InsertValues(){
cout<<"Enter 10 Values "<<endl;
for(int i=0;i<10;i++)
{
cin>>array[i];
}
cout<<"\n\t\t\tArray Values Inserted...
Successfully "<<endl;
}
void DeleteValues(){
cout<<"Enter the Index Number To Delete
Value :";
int index;
cin>>index;
if(index>9||index<0)
{
cout<<"Invalid Index Entered-> Valid
Range(0-9)"<<endl;
DeleteValues();// Recall The Function it
self
}
else
{
array[index]=-1;
}
Join Our Newsletter
cout<<"\n\t\t\tArray Value Deleted...
Sign up today and be the first to get notified on new updates.
Successfully "<<endl;
}
Enter your Email
void UpdateValues(){
cout<<"Enter Index Number to Update Value
Subscribe Now
:";
https://fahad-cprogramming.blogspot.com/2014/11/insert-update-delete-in-cpp-array-code.html 3/8
11/10/22, 7:12 AM Insert update delete element in array cpp code ~ C++ Programming Tutorial for Beginners
int index;
C++ Programming Tutorial For Beginners
cin>>index;
HOME C++if(index>9||index<0)
SIMPLE EXAMPLES C++ PROJECTS C++ SHAPES CODE BUY C++ EBOOKS CPP PROGRAMMING TUTORIALS
{
ABOUT US CONTACT US
cout<<"Invalid Index Entered-> Valid
Range(0-9)"<<endl;
UpdateValues();// Recall The Function it
self
}
else
{
cout<<"Enter the New Value For Index array[ "
<<index<<" ] = ";
cin>>array[index];
cout<<"\n\t\t\tArray Updated... Successfully
"<<endl;
}
}
int main()
{
char option;
SetDefaultValues();
do
{
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: ->
";
cin>>option;
if(option=='1')
{
cout<<"Insert Function Called"<<endl;
InsertValues();
cout<<"Inserted Values :"<<endl;
DisplayArray();
}
Join Our Newsletter
else if(option=='2') Sign up today and be the first to get notified on new updates.
{
Enter your Email
UpdateValues();
cout<<"Updated Array :"<<endl;
Subscribe Now
DisplayArray();
https://fahad-cprogramming.blogspot.com/2014/11/insert-update-delete-in-cpp-array-code.html 4/8
11/10/22, 7:12 AM Insert update delete element in array cpp code ~ C++ Programming Tutorial for Beginners
}
C++ Programming Tutorial For Beginners
else if(option=='3')
HOME C++ SIMPLE
{ EXAMPLES C++ PROJECTS C++ SHAPES CODE BUY C++ EBOOKS CPP PROGRAMMING TUTORIALS
DeleteValues();
ABOUT US CONTACT US
cout<<"Array After Deleting Values :"
<<endl;
DisplayArray();
}
else if(option!='e'&&option!='E')
{
cout<<"\n\n\t\t\tSelect A Valid Option From
Below\n\n";
}
}while(option!='e'&&option!='E');
system("cls");// To Clear The Screen
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;
return 0;
}
sample input outputs (codeBocks Console Screen Shots)
Program Menu Display
Update Array Values
Join Our Newsletter
Sign up today and be the first to get notified on new updates.
Exit Option Selection
Enter your Email
Subscribe Now
https://fahad-cprogramming.blogspot.com/2014/11/insert-update-delete-in-cpp-array-code.html 5/8
11/10/22, 7:12 AM Insert update delete element in array cpp code ~ C++ Programming Tutorial for Beginners
C++ Programming Tutorial For Beginners
HOME C++ SIMPLE EXAMPLES C++ PROJECTS C++ SHAPES CODE BUY C++ EBOOKS CPP PROGRAMMING TUTORIALS
ABOUT US CONTACT US
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
3 Comments:
Anonymous
November 4, 2014 at 8:56 AM
this example is very good. Not really heavy, without OOP but good.
Reply
Kabeer hussain
May 29, 2015 at 11:06 AM
//program to delete value
#include
#include
#include
using namespace std;
main()
{
int arr[5];
for(int i=0;i<5;i++)
{
cin>>arr[i];
} Join Our Newsletter
for(int i=0;i<5;i++)
Sign up today and be the first to get notified on new updates.
{
cout<>index_to_delete;
cout<<endl; Enter your Email
for(int i=0;i<5;i++)
{ Subscribe Now
if(arr[i]!=arr[index_to_delete])
https://fahad-cprogramming.blogspot.com/2014/11/insert-update-delete-in-cpp-array-code.html 6/8