0

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.

  • Does this answer your question? [std::cin.getline( ) vs. std::cin](https://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin) – 273K May 11 '21 at 15:17
  • Okay let me try it. – ChrisJay106 May 11 '21 at 15:18
  • What's an example of the input it fails on? You left that out. – Connor Low May 11 '21 at 15:27
  • @S.M. it is now working! However, it creates 2 blank spaces below and the user has to press enter twice to repeat the prompt again. How do I avoid that? – ChrisJay106 May 11 '21 at 15:27
  • @ConnorLow It accepts these inputs "5 Test", "1 ", "2 Sample" and any input which the first character is a number then the proceeding characters are non-numerical values. – ChrisJay106 May 11 '21 at 15:29
  • Nitpick: The function call `isdigit(arraysICE[i])` will cause undefined behavior for negative character values. See [this question](https://stackoverflow.com/q/45003739/12149471) for further information. However, since you seem to be a beginner and this is a rather advanced topic, it would probably be best for you to ignore this issue, as it has nothing to do with your current problem. – Andreas Wenzel May 11 '21 at 15:40
  • @ChrisJay106: `"it is now working! However, it creates 2 blank spaces below and the user has to press enter twice to repeat the prompt again. How do I avoid that?"` -- Please provide a [mre] of the problem. – Andreas Wenzel May 11 '21 at 15:48
  • @AndreasWenzel ohh okay. What I meant was the getline function works and it is now invalidating string inputs that start with a numeric value such as "4 Test", "11 Sample", "8 Input" and other inputs that starts with a numeric value and the proceeding characters are not a numeric value, however I found another problem which creates 2 newlines when such inputs are entered and the user has to press enter again to be asked for the array size. – ChrisJay106 May 11 '21 at 15:55
  • @ChrisJay106: I repeat: Please provide a [mre] of the problem. The code you posted does not fulfill these requirements, as the code you posted does not even compile (`arraysICE` undeclared, no function `main`, no `#include` directives, etc.). Please verify that your code compiles and actually reproduces the problem before posting it. – Andreas Wenzel May 11 '21 at 16:09
  • Note that it is possible for you to edit the question to update the code. – Andreas Wenzel May 11 '21 at 16:29

0 Answers0