-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);
}}
}
我該怎麼做?我在做但它不工作。
最簡單的方式是'的std ::矢量<> ::空()' –
操作數和運算符之間的空格數組沒有「空」的概念。最好使用[標準容器](http://en.cppreference.com/w/cpp/container)。 –
請勿使用全局變量或C風格的數組。使用一個知道它的大小的向量。 – stark