[英]Check if array is empty or not 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);
}}
}
我怎樣才能做到這一點? 我在做,但是沒有用。
您應該在“修改”功能中添加一個“檢查”。
原版的:
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 if 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);
}
喜歡:
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++;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.