Answer to Question #349688 in C++ for Magisk

Question #349688

Instructions:

  1. You are provided with an array that contains different characters.
  2. Ask the user for an integer input which represents the address.
  3. Then, print the element of the provided array using that integer as the index.

Given code:

#include <iostream>

#include <cstring>

using namespace std;


int main() {

  // NOTE: Do not edit this

  char location[100] = "CodeChumIsLoveAndProgrammingAsWellYey";

}


Example:

Enter the address: 1

Place at index 1 is o


1
Expert's answer
2022-06-10T18:34:16-0400
#include <iostream>
#include <cstring>
#include <limits>

using namespace std;

void printIndex(char str[]) {
    int i;
    cout << "Enter the address: ";
    cin >> i;
    while (!std::cin || i < 0 || i >= strlen(&str[0])) {
        cout << "Error! Wrong address!\nEnter the address: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> i;
    }
    cout << "Place at index " << i << " is " << str[i] << "\n";
}

int main() {

    // NOTE: Do not edit this

    char location[100] = "CodeChumIsLoveAndProgrammingAsWellYey";

    while (true) {
        printIndex(location);
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS