I cannot output correctly when the number is not found in the array. I've tried multiple conditionals so it would only trigger when it is true. But nothing worked.
int main()
{
int abfrage;
cout << "Type a number between 0 and 1000 to find it's position in the array " << endl;
cin >> abfrage;
const int feld = 1001;
int zahlenfeld[feld];
int i = 0;
for (int i = 0; i < feld; i++)
{
zahlenfeld[i] = rand() % 1001;
if (zahlenfeld[i] == abfrage)
{
cout << "Your number" << abfrage << " " << "is at the position" << i << endl;
}
else if (cin.fail() || abfrage > 1000 || abfrage < 0)
{
cout << "Wrong input. Only positive integers are possible." << endl;
break;
}
else if (zahlenfeld[i] != abfrage)
{
cout << "The number you searched for is not in the array." << endl;
break;
}
}
}
Aucun commentaire:
Enregistrer un commentaire