I am currently creating a program and at the start of the program, it will asks the user to enter the size of the array. It accepts numbers and invalidates strings and characters, however when the user enters "5 Test Input", it accepts the first character which is the five.
Here is my current code for validating the input:
int array[1000];
int arrsize;
stringstream convert;
string arraysize1;
again:
cout << "\nEnter the Array Size: ";
cin >> arraysize1;
//Conversion
strcpy(arraysICE, arraysize1.c_str());
//Validating each array element
for (int i = 0; i < arraysize1.length(); i++){
//If the program detects a non-numeric input, it will ask the user to input again
if (!(isdigit(arraysICE[i])) || ((isspace(arraysICE[i])))){
cout << "Invalid input! Please try again!" << endl;
cin.clear();
cin.ignore(1000, '\n');
goto again;
}
//If it is a numeric input, the input will become the array size
else
continue;
}
convert << arraysize1;
convert >> arrsize;
array[arrsize];
The program invalidates the input whenever the user enters a character or a string, such as "Test 5", " Test", "T3st" and "5Test", but when the user enters a number at the first character then the proceeding character is a space, it accepts the first digit, but it shouldn't.
I am aware the I should be using LinkedList if I want to create a dynamic one, but our professor requires us to use Array. Sorry if I violate some rules here, I am new to Stack Overflow.