2017-04-05 761 views
-2

请告诉我如何检查数组是否为空? 看到我的代码后,代码修改comment.I想要一条消息“对不起,没有增值迄今为止”的步骤。检查数组是否为空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); 
    }} 
} 

我该怎么做?我在做但它不工作。

+2

最简单的方式是'的std ::矢量<> ::空()' –

+5

操作数和运算符之间的空格数组没有“空”的概念。最好使用[标准容器](http://en.cppreference.com/w/cpp/container)。 –

+4

请勿使用全局变量或C风格的数组。使用一个知道它的大小的向量。 – stark

回答

1

您应该在“修改”功能中添加一个“检查”。

原文:

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; 

随着 “检查”:

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,而不是 “如果否则,如果”

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++;