title: how do i get this c++ program to generate a working wav file
body: <p>im trying to make a wav file writer and came up with this</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  char riff[5] = {'R', 'I', 'F', 'F', '\0'};
  unsigned int chunksize;
  char format[5] = {'W', 'A', 'V', 'E', '\0'};
  char subchunk1ID[5] = {'f', 'm', 't', ' ', '\0'};
  unsigned int subchunk1size;
  unsigned short int audioformat = 1;
  unsigned short int numchannels = 1;
  unsigned int samplerate;
  unsigned int byterate;
  unsigned short int blockalign;
  unsigned short int bitspersample;
  char subchunk2ID[5] {'d', 'a', 't', 'a', '\0'};
  unsigned int subchunk2size;
  
  int x = 0;
  char a = 0;
  char b = 4;
  
  int main()
  {
      subchunk1size = 16;
      subchunk2size = 1000;
      chunksize = 1036;
      samplerate = 500;
      bitspersample = 8;
      byterate = 500;
      blockalign = 1;
  
      ofstream myfile;
      myfile.open("test2.wav");
      myfile &lt;&lt; riff &lt;&lt; chunksize &lt;&lt; format
             &lt;&lt; subchunk1ID &lt;&lt; subchunk1size &lt;&lt; audioformat 
             &lt;&lt; numchannels &lt;&lt; samplerate &lt;&lt; byterate &lt;&lt; blockalign
             &lt;&lt; bitspersample &lt;&lt; subchunk2ID &lt;&lt; subchunk2size;
      while (x &lt; 1000) {
          if (x%2 == 1) {
              myfile &lt;&lt; a;
          }
          else {
              myfile &lt;&lt; b;
          }
          x=x+1;
      }
      myfile.close();
      return 0;
  }
  </code></pre>
  
  <p>however when i try to open the file i get an error "windows media player encountered a problem while playing the file" (because its header is messed up i assume)</p>
  
  <p>i tried writing this as a .txt instead of a .wav and the integers were written as numbers which makes me think that the problem is a non literal translation from the binary values of the variables to the .wav file
  if this is the problem is there a simple way around this</p>