Check if array is empty or not C++ -


please tell me how check whether array empty on not? see code after step of code modify comment.i want message "sorry no value added far".

#include<iostream> using namespace std; int count=0; //----------------------------------------------------------// //---------menu items list start position---------// //----------------------------------------------------------// void menu(int n){cout<<"\nenter option: \n";     cout<<"1- add new value\n2- search value\n3- modify value\n4- print value\n5- print sum of values\n6- quit/terminate\n"; } //-----------------------------------------------------------// //---------funtion add new values starts here--------// //-----------------------------------------------------------// void addnewvalue(int a[]){     cout<<"enter value\n";     cin>>a[count];  //taking input array     count++; } //------------------------------------------------------------------------// //---------function search value array starts here---------// //------------------------------------------------------------------------// void searchvalue(int a[]){     int num,jawad=0;     cout<<"enter number search\n";     cin>>num;     for(int starter=0;starter<count;starter++){         //starting loop 1st value of array         if (num==a[starter])         jawad=1;        //switching jawad 0 1 if value found     }     if(jawad==1)     cout<<"value exists @ "<<count<<"th position\n";     else cout<<"value not exist"; } //-------------------------------------------------------------------// //---------function modify value in array start here---------// //-------------------------------------------------------------------// void modifyvalue(int a[]){     int modification,position;     cout<<"enter position of number modify";     cin>>position;     cout<<"enter number modify";     cin>>modification;     a[position-1]=modification;         //calculating index enterd value , placing equal new number } //-----------------------------------------------------------// //---------function print values starts here---------// //-----------------------------------------------------------// void printvalue(int a[]){     cout<<"the stored values : ";     for(int c=0;c<count;c++)        //start loop , tak out values print them     {cout<<a[c]<<' ';     if (a[c]==0)     cout<<"jawad adil";     }  } //------------------------------------------------------------------------------// //----------function take sum of values of array starts here--------// //------------------------------------------------------------------------------// void printsum(int a[]){     int r=0,sum=0;     cout<<"the sum of values : ";     while(r<count){         sum=sum+a[r];           //taking sum of values using loop         r=r+1;     } cout<<sum<<'\n'; } //---------------------------------------------// //----------main body starts here---------// //---------------------------------------------// int main(){     int n;     int a[100];     while(n!=6){     menu(n);     cin>>n;     if (n==1){         addnewvalue(a);         //calling functions using if else statments     }     else if(n==2){         searchvalue(a);     }     else if(n==3){         modifyvalue(a);     }     else if(n==4){         printvalue(a);     }     else if(n==5){         printsum(a);     }}  } 

how can that? doing not working.

you should add "check" in "modify" function.

original:

void modifyvalue(int a[]){ int modification,position; cout<<"enter position of number modify"; cin>>position; cout<<"enter number modify"; cin>>modification; a[position-1]=modification; 

with "check":

void modifyvalue(int a[]){ //check if(count == 0) {    cout << "sorry no value added far";    return; //exit function }  int modification,position; cout<<"enter position of number modify"; cin>>position; cout<<"enter number modify"; cin>>modification; a[position-1]=modification; } 

also recommend use switch instead of "if else if"

if (n==1){     addnewvalue(a);         //calling functions using if else statments } else if(n==2){     searchvalue(a); } else if(n==3){     modifyvalue(a); } else if(n==4){     printvalue(a); } else if(n==5){     printsum(a); } 

like:

switch (n)     {        case 1:           addnewvalue(a);           break;       case 2:           searchvalue(a);           break;       case 3:           modifyvalue(a);;           break;       //and on...       default:           cout << "unknown option";     } 

also in code don't need arguments in

  void menu(int n) 

so can make

  void menu() 

instead.

also recommend place whitespaces between operands , operators (words)

cout << "enter value\n"; cin >> a[count];  //taking input array count++; 

instead

cout<<"enter value\n"; cin>>a[count];  //taking input array count++; 

Comments