There are various issues with the code. Why use a switch() and then have if statements? There's also an issue with the bubble sort. Note that c++ has a std::swap() function. There is also a std::sort() function that could be used unless you have to code your own
#include <iostream>
#include <utility>
usingnamespace std;
int main() {
int arr[20] {};
cout << "Enter 20 non-negative integers:\n";
for (int i = 0; i < 20; ) {
cout << "Element for " << i << ": ";
cin >> arr[i];
if (arr[i] < 0)
std::cout << "Number must not be negative\n";
else
++i;
}
for (char select {}; select != 'd'; ) {
cout << "\na. Display\n";
cout << "b. Sort\n";
cout << "c. Search\n";
cout << "d. Quit\n";
cout << "\nPlease select a choice above: ";
cin >> select;
switch (select) {
case'a':
cout << "\na. You chose to Display\n";
cout << "The elements are:\n";
for (int i = 0; i < 20; i++)
cout << arr[i] << " ";
cout << '\n';
break;
case'b':
cout << "b. You chose to Sort\n";
for (int i = 0; i < 19; ++i)
for (int j = 0; j < 19 - i; ++j)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
break;
case'c':
{
int n {};
cout << "C. You chose to Search";
cout << "\nEnter a number to be searched: ";
cin >> n;
bool fnd {};
for (int i = 0; i < 20; ++i)
if (arr[i] == n) {
std::cout << "Number found at index " << i << '\n';
fnd = true;
break;
}
if (!fnd)
std::cout << "Number not found!\n";
}
break;
case'd':
break;
default:
cout << "Invalid option\n";
break;
}
}
}
Also note that where a size of an array is used, this is usually coded as a const variable at the beginning of the program so if the size needs to be changed, there is only one place to change.