Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If I enter any other number or character, I would like it to give me an error an

ID: 3823012 • Letter: I

Question

If I enter any other number or character, I would like it to give me an error and say try again. Please help out. Also If I enter any other letter other then A R S E , it still gives me an output.

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

int main() {

   srand(time(0));

   int arr[10], i, sum;

   bool found;

   char choice;

   for (i = 0; i < 10; i++) {

       arr[i] = rand() % 40 + 60;

   }

   cout << "The random generated array is: ";

   for (i = 0; i < 10; i++) {

       cout << arr[i] << " ";

   }

   cout << " R[everse] [A]dd [S]earch E[xit] Select an option: ";

   cin >> choice;

   switch (choice) {

   case 'R':

   case 'r':

       cout << " The reversed array is: ";

       for (i = 9; i >= 0; i--) cout << arr[i] << " ";

       cout << endl << endl << "-----------------------------------------------------------------" << endl;

       main();

   case 'A':

   case 'a':

       cout << " The sum of the array elements is: ";

       sum = 0;

       for (i = 0; i < 10; i++) sum += arr[i];

       cout << sum << " ";

       cout << endl << endl << "-----------------------------------------------------------------" << endl;

       main();

   case 'S':

   case 's':

       cout << " Please insert an element to find: ";

       int find;

       found = false;

       cin >> find;

       for (i = 0; i < 10; i++) {

           if (arr[i] == find) {

               if (i == 0) cout << " The number " << find << " has been found at the 1st position" << endl;

               else if (i == 1) cout << " The number " << find << " has been found at the 2nd position" << endl;

               else if (i == 2) cout << " The number " << find << " has been found at the 3rd position" << endl;

               else cout << " The number " << find << " has been found at the " << i + 1 << "-th position" << endl;

               found = true;

           }

       }

       if (!found) cout << " Element not found ";

       cout << endl << endl << "-----------------------------------------------------------------" << endl;

       main();

   case 'E':

   case 'e':

       break;

   }

   return 0;

}

Explanation / Answer

Hi

I have updated the code for handling other letters and hoghlighted the code changes below.


#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

int main() {
srand(time(0));
int arr[10], i, sum;
bool found;
char choice;
for (i = 0; i < 10; i++) {
arr[i] = rand() % 40 + 60;
}
cout << "The random generated array is: ";
for (i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
cout << " R[everse] [A]dd [S]earch E[xit] Select an option: ";
cin >> choice;
switch (choice) {
case 'R':
case 'r':
cout << " The reversed array is: ";
for (i = 9; i >= 0; i--) cout << arr[i] << " ";
cout << endl << endl << "-----------------------------------------------------------------" << endl;
main();

case 'A':
case 'a':
cout << " The sum of the array elements is: ";
sum = 0;
for (i = 0; i < 10; i++) sum += arr[i];
cout << sum << " ";
cout << endl << endl << "-----------------------------------------------------------------" << endl;
main();

case 'S':
case 's':
cout << " Please insert an element to find: ";
int find;
found = false;
cin >> find;
for (i = 0; i < 10; i++) {
if (arr[i] == find) {
if (i == 0) cout << " The number " << find << " has been found at the 1st position" << endl;
else if (i == 1) cout << " The number " << find << " has been found at the 2nd position" << endl;
else if (i == 2) cout << " The number " << find << " has been found at the 3rd position" << endl;
else cout << " The number " << find << " has been found at the " << i + 1 << "-th position" << endl;
found = true;
}
}
if (!found) cout << " Element not found ";
cout << endl << endl << "-----------------------------------------------------------------" << endl;
main();

case 'E':
case 'e':
break;
default: cout<<"try again."<<endl;
}
return 0;
}

Output:

sh-4.2$ main                                                                                                                                                                                                                                                             

The random generated array is: 62 71 84 68 69 90 69 75 67 74                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                         

R[everse]       [A]dd   [S]earch        E[xit]                                                                                                                                                                                                                           

Select an option: 1                                                                                                                                                                                                                                                      

try again.                                                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                             

The random generated array is: 82 95 89 91 65 65 80 86 72 61                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                         

R[everse]       [A]dd   [S]earch        E[xit]                                                                                                                                                                                                                           

Select an option: l                                                                                                                                                                                                                                                      

try again.                                                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                             

The random generated array is: 86 68 91 96 98 93 87 91 79 69                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                         

R[everse]       [A]dd   [S]earch        E[xit]                                                                                                                                                                                                                           

Select an option: S                                                                                                                                                                                                                                                      

                                                                                                                                                                                                                                                                         

Please insert an element to find: 2                                                                                                                                                                                                                                      

                                                                                                                                                                                                                                                                         

Element not found                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

                                                                                                                                                                                                                                                                         

-----------------------------------------------------------------                                                                                                                                                                                                        

The random generated array is: 99 77 62 90 67 66 61 81 86 81                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                         

R[everse]       [A]dd   [S]earch        E[xit]                                                                                                                                                                                                                           

Select an option: e

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote