for(int j=0;j<5;j++)
if (num!=arr[j])
{
cout<<"not found"<<endl;
break;
}
return 0;
}
I want to enter number and show its order on the screen ... but if this number is not exist ... display mmessage " not found"
I have a bug in that code that when I find the number in the array ... the program doesn't stop ... it goes to the second for loop ... ho can I stop that?
You really don't have to have two loops when you can easily merge the two:
1 2 3 4 5 6 7 8 9
for(int I_(0); I_ < 5; ++I_)
{
if(Num_ == Arr_[I_])
{
// Are the same...
}
// Not the same...
}
To me, it sounds like you want to indicate whether the said number was found within the array. For this, I recommend using bool; a type that has two distinct values: true & false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
bool Found_(false); // Local to "main()".
for(int I_(0); I_ < 5; ++I_)
{
if(Num_ == Arr_[I_])
{
// Are the same...
Found_ = true;
break;
}
// Not the same...
}
if(!Found_)
{
// ... Not found...
}