Print the size of the input data and input data of integer

Hello I am writing a program that to Define a new int array of 500 integers to store the input data,also Convert and store the input integers to the array and Display the size of the input data and display the input data as well.
I face the problem that i can't show my input data correctly on the Serial Monitor.For example if i input 123 it will show input data = 3 integers and my input data will become the unknown data "⸮" .But i want myint[0]=123 not myint[0]=1, myint[1]=2, myint[2]=3 so the size should be just count for 1 integers.
I also cannot show what i input the integer on the Serial Monitor.Also how can i not count the space as the size of input data for example:if i input "123 234" it will just show
"Size of input data = 2 integers"
"123 234"

This is the Serial Monitor showing when i input 123:
Please input
Size of input data = 3 integers


Please input

Here's my code:

int myint[499] ;     // this is an array
char input;
int i;
    
void setup() {
  // Initialize serial and wait for port to open:  
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  while (Serial.available() > 0) {
    input = Serial.read();
    if (input != '\n') { 
      myint[i++] = input; // last character is newline
      myint[i] = 0;       // string array should be terminated with a zero
    } else{
      input = Convertinteger(input);
      myint[i++] = input;  
      myint[i] = 0;         
      Serial.println("Please input");
      Serial.print("Size of input data =  ");
      Serial.print(i-1);
      Serial.print(" integers");
      Serial.println("");
      Serial.print(input);
      Serial.println("");
      Serial.println("");
      Serial.println("Please input");  
      i = 0;
    }
  }
}

int Convertinteger(char x) {
  if (x <= 0x39 && x >= 0x30)
    return (x - 0x30);
}

Hello I am writing a program that to Define a new int array of 500 integers

int myint[499] ; Oops

hanssama628:
Here's my code:

It looks like your code is reading characters - for example "123" is being treated as '1' and '2' and '3' - rather than converting the 3 characters to the number one hundred and twenty three.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Also have a look at Serial.readBytesUntil() and atoi().

I think this will do what you need using my SafeString library
Also check out my tutorial Serial Text I/O for the Real World

#include "SafeString.h" // my SafeString library from the Arduino library manager

const size_t maxLineLength = 80; // length of largest command to be recognized, can handle longer lineInput but will not lineTokenize it.
createSafeString(lineInput, maxLineLength + 1); //  to read lineInput cmd, large enough to hold longest cmd + leading and trailing delimiters
createSafeString(lineToken, maxLineLength + 1); // for parsing, capacity should be >= lineInput
bool skipToDelimiter = false; // bool variable to hold the skipToDelimiter state across calls to readUntillineToken()
// set skipToDelimiter = true to skip initial data upto first delimiter.
// skipToDelimiter = true can be set at any time to next delimiter.

int myint[500] ;     // this is an array 0 to 499 == 500 locations
int i;
bool enteringInts = false;
bool finished = false;
int numberOfInts = 0;

void setup() {
  Serial.begin(115200);    // Open serial communications and wait a few seconds
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  // first run without any outputting any error msgs or debugging
  //SafeString::setOutput(Serial); // enable error messages and debug() output to be sent to Serial

  Serial.println(F(" Set Newline for Arduino Monitor"));
  Serial.println(F(" Enter number of integers to be entered : "));
}

void printCurrentIdx() {
  Serial.print(F("You have entered ")); Serial.print(i); Serial.print(F(" ints of the ")); Serial.print(numberOfInts); Serial.println(F(" requried."));
}

void loop() {
  if (lineInput.readUntilToken(Serial, lineToken, "\r\n", skipToDelimiter, true )) { // change true to false to suppress echo
    //                                  change the ,true to false to suppress echo of lineInput
    lineToken.debug();
    if (!enteringInts) {
      // collect number to be entered
      if (!lineToken.toInt(numberOfInts)) {
        Serial.println(" Invalid number, enter number of ints to be entered");
      } else {
        if ((numberOfInts < 1) || (numberOfInts > 500)) {
          Serial.println(F("Number out of range: Number of Ints must be between 1 and 500 inclusive"));
        } else {
          enteringInts = true;
          Serial.print(" Enter "); Serial.print(numberOfInts); Serial.println(" integers");
          Serial.println(F(" You can enter multiple numbers separated by spaces (up to 80 chars per line, longer lines are ignored) "));
          Serial.println(F(" Current status displayed at the end of every line."));
        }
      }
    } else {
      if (!finished) {
        // add a final delimiter
        lineToken += ' ';
        // parse lineToken for number separate by spaces
        cSF(sfNumber, 20);
        while (!finished) { // more numbers to parse
          if (lineToken.nextToken(sfNumber, " ")) { 
            sfNumber.debug();
            if (!sfNumber.toInt(myint[i])) {
              // does not change myInt if error
              Serial.print(F(" Error:")); Serial.print(sfNumber); Serial.println(F(" is not a valid integer"));
              Serial.print(F(" Ignoring the rest of this line"));
              Serial.println(lineToken);
              printCurrentIdx();
              break;
            }
            i++;
            if (i >= numberOfInts) {
              finished = true;
              break;
            }
          } else {
            // no more numbers
            printCurrentIdx();
            break;
          }
        }
        if (finished) {
          Serial.println(F(" All numbers entered"));
          printCurrentIdx();
        }
      }
    } // else lineToken is empty
  }
}