title: How to read white spaces in a do while loop in c++
body: <p>I'm trying to scan my input <code>msg</code> that reads white spaces. I tried <code>scanf("%[^\n]s,&amp;msg);</code> it works when I don't use a loop. But when I use it inside a do while loop, the <code>scanf("%[^\n]s,&amp;msg);</code> will not read If I press '1' to execute infinite loop. Is there any alternate process to read the string with spaces in do while loop?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main()
   {
  /* Variable declaration */
  char msg[100];
   char countch;
   int key = 0;
   int i = 0;
   int error = 0;
  
   printf("*** Caesar Cipher ***\n\n");
  
  do{
  
  /* Input Plain Text */
  printf("\nEnter Plain Text:");
  scanf("%s",&amp;msg);
  
  /* Input key */
  printf("Enter Key:");
  scanf("%d",&amp;key);
  
  /* Encryption */
  /* Traverse Text */
  for(i = 0;msg[i] != '\0'; i++)
  {
  countch = msg[i];
  
  /* apply Encryption lowercase letters */
  if(countch &gt;= 'a' &amp;&amp; countch &lt;= 'z')
  {
      countch = countch + key;
  
      if(countch &gt; 'z')
      {
  
      countch = countch - 'z' + 'a' - 1;
      }
      msg[i] = countch;
  }
  /* apply Encryption Uppercase letters */
  else if(countch &gt;= 'A' &amp;&amp; countch &lt;= 'Z')
  {
      countch = countch + key;
  
      if(countch &gt; 'Z')
      {
      countch = countch - 'Z' + 'A' - 1;
      }
      msg[i] = countch;
  }
  }
  
  //printf("\n===================================");
  /* print encryption result */
  printf("Encrypted Message:%s",msg);
  
  
  /* Decryption */
  for(i = 0;msg[i] != '\0'; i++)
  {
  countch = msg[i];
  
  /* apply decryption lowercase letters */
  if(countch &gt;= 'a' &amp;&amp; countch &lt;= 'z')
  {
      countch = countch - key;
  
      if(countch &gt; 'z')
      {
  
      countch = countch + 'z' - 'a' + 1;
      }
      msg[i] = countch;
  }
  /* apply decryption Uppercase letters */
  else if(countch &gt;= 'A' &amp;&amp; countch &lt;= 'Z')
  {
      countch = countch - key;
  
      if(countch &gt; 'Z')
      {
          countch = countch + 'Z' - 'A' + 1;
      }
      msg[i] = countch;
  }
  }
  
  /* print decryption result */
  printf("\nDecrypted Message:%s",msg);
  //printf("\n");
  
  printf("\nDo you want to continue '1' or '0':");
  scanf("%d",&amp;error);
  
    }while(error != 0);
   exit(0);
  
  
   getch();
   }
  </code></pre>