How do i clear the data that i input in Serial Monitor before

I am trying to write a program that can read a string from Serial Monitor Input and count the size of characters as the input data and store the input to the array, but i have some trouble that the Serial monitor will remain the last input data for example if I input ABC it will show “Size of input data = 3 characters” then i input ABC again it will remain the last data that i input before, I already reset it =0 what mistake do i make?
Serial monitor showing:
Please input
Size of input data = 3 characters
ABC
Please input
Please input
Size of input data = 6 characters
ABC
ABC

Here’s my code:

[color=#242729]String Msg  ; [/color]
[color=#242729]char buf[1200]={0} ;     // this is an array[/color]
[color=#242729]char input;[/color]
[color=#242729]int num=0;[/color]
[color=#242729]void setup() {[/color]
[color=#242729]  // Initialize serial and wait for port to open:  [/color]
[color=#242729]  Serial.begin(115200);[/color]
[color=#242729]  while (!Serial) [/color]
[color=#242729]  {[/color]
[color=#242729]    ; // wait for serial port to connect. Needed for native USB port only[/color]
[color=#242729]  }[/color]
[color=#242729]   }[/color]
void loop() {
  while (Serial.available()) 
  {
    input = (char)Serial.read();
     if(input != '\n' ) 
    { 
      buf[num] = input;  
      Msg+=input;    
      num++;
     }
     else{
      buf[num++] = input;  // last character is newline
      buf[num] = 0;         // string array should be terminated with a zero
      Serial.println("Please input");
      Serial.print("Size of input data =  ");
      Serial.print(Msg.length());
      Serial.print(" characters");
      Serial.println("");
      Serial.println(Msg);
      Serial.println("Please input");
      Serial.println("");
      Serial.println("");
        for(int i=0; i<num ;i++){
        Msg[i]=0;
        buf[i] =0;
        }
        
       }
       num=0;
       }
    return;
         }

There are some point in your code:

  • For String object, you need to use String.remove() function instead of Msg = 0;[/li]
    • The best practice when using String object is to use String.reserve() function to avoid memory fragmentation.*
    • Inside your loop() function, you had better to use if statement instead of while loop*
  • [/list]*

IoT_hobbyist:
There are some point in your code:

  • For String object, you need to use String.remove() function instead of Msg = 0;[/li]
    • The best practice when using String object is to use String.reserve() function to avoid memory fragmentation.*
    • Inside your loop() function, you had better to use if statement instead of while loop*
  • [/list]*
  • [/quote]*
  • Thanks you so much it's work :)*

Here's my code:

Thank you for using code tags, but unfortunately due to a bug in the forum software if you have the WYSIWYG editor option turned on in your profile then meaningless tags are added to the code block as you can see. To prevent this please turn off the WYSIWYG editor option

The problem is a known one and has been reported but a fix is still awaited

        Msg[i]=0;

Setting the contents to 0 does not shorten a String object. Remove this line from the loop and before or after the loop, put:

     Msg = "";