Input 5 elements into array, check validation then output C++ -
i'm working on project required me input 5 numbers 20-101 one-dimensinal array. after that, shows output of 5 numbers. if enter non-numeric/float number/duplicate/out-range, reject , ask user input again. have try , catch codes. also, program able shows bad input @ end. here got far , got stuck, i'm totally lost.
check out-range numbers
int arrayreader::checkinput(int number) { if (number >= 20 && number <= 101) { return number; } else { throw invalid_argument("out-range-number entered"); } }
main code
#include <iostream> #include <stdexcept> #include <iomanip> #include "arrayreader.h" using namespace std; int main() { arrayreader value; int number = 0; int dup; int array[5]; int currentarray = 0; cout << "enter number 20 - 101: "; (int = 0; < 5; i++) { cin >> number; dup = 0; if (number >= 20 && number <= 101) { (int value = 0; value < currentarray; value++) { if (number == array[value]) { dup == true; dup = 1; cout << "number " << number << " used. try again"; break; } } if (dup == false) { array[currentarray++] = number; i++; } } else { try { int result = value.checkinput(number); cout << "valid input: " << number << endl; } catch (invalid_argument &x) { cout << "error: " << x.what() << endl; } } } (int = 0; < 5; i++) { cout << "valid numbers entered" << " " << array[i] << endl; } }
my problems are:
for now, when run program, doesn't let me enter 5 numbers (only 3). checked duplicated numbers , out-range number. output chain of weird numbers
i have no ideas how reject non-numeric input , float numbers.
i'm beginner of c++, please me!
change function debug:
int arrayreader::checkinput(const int& number) { cout << '\n checkinput number: ' << number << '\n'; if (number >= 20 && number <= 101) { return number; } else { throw invalid_argument("out-range-number entered"); } return -1; }