检查数组是否为空C ++

时间:2017-04-05 18:58:57

标签: c++

请告诉我如何检查数组是否为空? 在代码修改评论的步骤之后看到我的代码。我想要一条消息“抱歉没有添加任何值到目前为止”。

#include<iostream>
using namespace std;
int count=0;
//----------------------------------------------------------//
//---------menu items list start from this position---------//
//----------------------------------------------------------//
void menu(int n){cout<<"\nEnter an option: \n";
    cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
}
//-----------------------------------------------------------//
//---------Funtion to add new values starts from here--------//
//-----------------------------------------------------------//
void AddNewValue(int a[]){
    cout<<"Enter a value\n";
    cin>>a[count];  //taking input to array
    count++;
}
//------------------------------------------------------------------------//
//---------Function to search a value from array starts from here---------//
//------------------------------------------------------------------------//
void SearchValue(int a[]){
    int num,jawad=0;
    cout<<"Enter a number to search\n";
    cin>>num;
    for(int starter=0;starter<count;starter++){         //starting loop from 1st value of array
        if (num==a[starter])
        jawad=1;        //switching jawad from 0 to 1 if value found
    }
    if(jawad==1)
    cout<<"value exists at "<<count<<"th position\n";
    else cout<<"Value does not exist";
}
//-------------------------------------------------------------------//
//---------Function to modify value in array start from here---------//
//-------------------------------------------------------------------//
void ModifyValue(int a[]){
    int modification,position;
    cout<<"Enter the position of a number to modify";
    cin>>position;
    cout<<"Enter a number to modify";
    cin>>modification;
    a[position-1]=modification;         //calculating index from enterd value and placing that equal to new number
}
//-----------------------------------------------------------//
//---------Function to Print values starts from here---------//
//-----------------------------------------------------------//
void PrintValue(int a[]){
    cout<<"The stored values are : ";
    for(int c=0;c<count;c++)        //start loop and tak out all the values then print them
    {cout<<a[c]<<' ';
    if (a[c]==0)
    cout<<"jawad adil";
    }

}
//------------------------------------------------------------------------------//
//----------Function to Take sum of the values of array starts from here--------//
//------------------------------------------------------------------------------//
void PrintSum(int a[]){
    int r=0,sum=0;
    cout<<"The sum of all the values is : ";
    while(r<count){
        sum=sum+a[r];           //taking sum of all the values using loop
        r=r+1;
    }
cout<<sum<<'\n';
}
//---------------------------------------------//
//----------main body starts from 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);
    }}
 }

我该怎么做?我在做,但它没有用。

1 个答案:

答案 0 :(得分:2)

你应该添加一个&#34;检查&#34;在你的&#34;修改&#34;功能

原件:

void ModifyValue(int a[]){
int modification,position;
cout<<"Enter the position of a number to modify";
cin>>position;
cout<<"Enter a number to modify";
cin>>modification;
a[position-1]=modification;

使用&#34;检查&#34;:

void ModifyValue(int a[]){
//Check
if(count == 0)
{
   cout << "Sorry no value added so far";
   return; //Exit from function
}

int modification,position;
cout<<"Enter the position of a number to modify";
cin>>position;
cout<<"Enter a number to modify";
cin>>modification;
a[position-1]=modification;
}

另外,我建议你使用switch代替&#34;如果是&#34;

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);
}

像:

switch (n)  
  {  
     case 1:  
        AddNewValue(a);  
        break;

     case 2:  
        SearchValue(a);  
        break;

     case 3:  
        ModifyValue(a);;  
        break;

     //And so on...

     default:  
        cout << "Unknown option";  
  }

同样在此代码中,您不需要

中的任何参数
  void menu(int n)

所以你可以制作

  void menu()

代替。

我还建议你在操作数和运算符(单词)之间放置空格

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

代替

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