Create an array of size ten; get input from user as odd index has odd value [place a check that when user enters odd value then assign that value to the array element] and even indexes can have any value, show array element by element.
#include <iostream>
#include <limits>
int check();
int main() {
int array[10] = {};
for (int i = 0; i < 10; i++) {
if (i % 2) {
array[i] = check();
}
else {
std::cin >> array[i];
}
}
for (auto const& x : array) {
std::cout << x << std::endl;
}
return 0;
}
int check() {
int s;
std::cin >> s;
while (~s % 2) {
std::cout << "Enter odd value please!" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> s;
}
return s;
}
Comments
Leave a comment