Create an array of size 30. Take 2 indexes from user as input and multiply the value on those indexes and display the answer on screen.
#include <iostream>
using namespace std;
void getIndex(const char error_str[], int& x);
int main(void) {
int array[30] = { 51,78,3,6,42,60,9,8,57,64,76,7,98,75,8,97,0,9,84,27,29,53,98,37,6,4,9,78,65,46 };
int a, b;
cout << "Enter first index: ";
getIndex("Wrong index!\nRe-enter first index: ", a);
cout << "Enter second index: ";
getIndex("Wrong index!\nRe-enter second index: ", b);
cout << array[a] * array[b];
return 0;
}
void getIndex(const char error_str[], int& x) {
cin >> x;
while (!std::cin || x < 0 || x >= 30) {
cout << error_str;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> x;
}
}
Comments
Leave a comment