2017-04-05 759 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++;