Arduino and C++ Serial communication I am using this header: http://playground.arduino.cc/Interfacing/CPPWindows

My main:

#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;tchar.h&gt;
#include "SerialClass.h"
#include &lt;string&gt;

int main(int argc, _TCHAR* argv[])
{
    Serial* SP = new Serial("\\\\.\\COM4");

    if (SP-&gt;IsConnected())
        printf("We're connected\n");

    std::string tryStr;
    int value;
    DWORD bytesSend;



    /* Loop */
    while(SP-&gt;IsConnected())
    {
         /* Take a value from user */
         printf("Value: "); scanf("%d",&amp;value); 

         /* Send Arduino */
         BOOL retVal = WriteFile(SP-&gt;hSerial, &amp;value, sizeof(value), &amp;bytesSend, NULL);

        /* I have no idea what is this for? (SP-&gt;status.cbInQue) */
        ClearCommError(SP-&gt;hSerial, &amp;SP-&gt;errors, &amp;SP-&gt;status);

        /* Read string from arduino */
        DWORD bytesRead;
        tryStr.assign("");
        bool check = true;
        char tempCharacter;
        while (check==true){

            ReadFile(SP-&gt;hSerial, &amp;tempCharacter, sizeof(char), &amp;bytesRead, NULL);
            tryStr += tempCharacter;

            if(tempCharacter == '$'){
                check = false;
            }
        }

        /* Print the string */
        std::cout &lt;&lt; tryStr  &lt;&lt; std::endl &lt;&lt; std::endl ;
    }
    return 0;
}


My Arduino Code:

int LedPin= 10; 
int value;
String  incomingData = "hello$";

void setup (){
  Serial.begin(9600); 
  pinMode(LedPin, OUTPUT); 
}

void loop (){

  while(!(Serial.available() &gt; 0)){
    /*Wait until take data */
  }

  value=Serial.read(); 

  if (value == 20)
  {
      digitalWrite (LedPin, HIGH); 
      incomingData.setCharAt(0,'o');
      incomingData.setCharAt(1,'p');
      incomingData.setCharAt(2,'e');
      incomingData.setCharAt(3,'n');
      incomingData.setCharAt(4,'n');

      Serial.print(incomingData);
  }
  else if (value == 15)
  {
      digitalWrite (LedPin, LOW);
      incomingData.setCharAt(0,'c');
      incomingData.setCharAt(1,'l');
      incomingData.setCharAt(2,'o');
      incomingData.setCharAt(3,'s');
      incomingData.setCharAt(4,'e');

      Serial.print(incomingData);
  }


}


This code works perfectly. Output:


  We're connected  Value: 20  openn$
  
  Value: 15  close$
  
  Value: 20  openn$
  
  Value: 20  openn$
  
  Value: 15  close$
  
  Value: 15  close$  
  
  Value: 20  openn$


But now I want to add another condition in my Arduino code:

int LedPin= 10; 
int value;
String  incomingData = "hello$";

void setup (){
  Serial.begin(9600); 
  pinMode(LedPin, OUTPUT); 
}

void loop (){

  while(!(Serial.available() &gt; 0)){
    /*Wait until take data */
  }

  value=Serial.read(); 

  if (value == 20)
  {
      digitalWrite (LedPin, HIGH); 
      incomingData.setCharAt(0,'o');
      incomingData.setCharAt(1,'p');
      incomingData.setCharAt(2,'e');
      incomingData.setCharAt(3,'n');
      incomingData.setCharAt(4,'n');

      Serial.print(incomingData);
  }
  else if (value == 15)
  {
      digitalWrite (LedPin, LOW);
      incomingData.setCharAt(0,'c');
      incomingData.setCharAt(1,'l');
      incomingData.setCharAt(2,'o');
      incomingData.setCharAt(3,'s');
      incomingData.setCharAt(4,'e');

      Serial.print(incomingData);
  }
  else{                                      // new line added
                                             // new line added
      incomingData.setCharAt(0,'g');         // new line added
      incomingData.setCharAt(1,'g');         // new line added
      incomingData.setCharAt(2,'g');         // new line added
      incomingData.setCharAt(3,'g');         // new line added
      incomingData.setCharAt(4,'g');         // new line added
                                             // new line added
      Serial.print(incomingData);            // new line added
  }                                          // new line added

}


But it didn't work. ScreenShot:


What went wrong? The led blinked correctly but the output string is wrong.
BOOL retVal = WriteFile(SP-&gt;hSerial, &amp;value, sizeof(value), &amp;bytesSend, NULL);


You're writing sizeof(value) bytes to the Arduino. Since value is an int, which is typically 4 bytes in size for PC compilers/processors, when you send 20, what actually gets to the Arduino is 0x00000014. Your processor is apparently little-endian so the order of bytes sent is 0x14, 0x00, 0x00, 0x00. 

You're reading a single byte in every loop() iteration. Your first sketch worked because you didn't provide any else clause for unrecognized bytes. The Arduino merely ignored those leading zeros. However, your second sketch handled unknown bytes (such as 0x00) by printing gggg. So what you get for every value you send to the Arduino is exactly what you'd expect and what you see in your shell -- the result you want (openn == 0x14), followed by 3 gibberish results (because of the leading zero bytes). 

You can fix it by either declaring value as a uint8_t in your CPP code (after including stdint.h) or only reading chunks of 4 bytes on the Arduino and then parsing what you get. The former solution seems more reasonable/reliable.