Stuck on homework question about playing octave

the question says:
Write a program that will repetitively play an entire octave: C, D, …

I missed the day in class were it was explained how to do stuff like this and im . a little stuck any help would be great

thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <map>
#include <windows.h>
using namespace std;

const double factorOctave = 2.0;
const double factorSemitone = 1.0594630943593;
const int majorScaleIntervals[] = { 2, 2, 1, 2, 2, 2, 1 };
map<string,double> noteMap;

//======================================================================

void play( double freq, int ms )
{
   int ifreq = freq + 0.5;
   Beep( ifreq, ms );
}

//======================================================================

void setMap()      // Maps notes to frequency (in Hz) for octave starting at A above middle C
{
   noteMap["A" ]                 = 440.00;       // tuning-fork A
   noteMap["A#"] = noteMap["Bb"] = 466.16;
   noteMap["B" ] = noteMap["Cb"] = 493.88;
   noteMap["B#"] = noteMap["C" ] = 523.25;
   noteMap["C#"] = noteMap["Db"] = 554.37;
   noteMap["D" ]                 = 587.33;
   noteMap["D#"] = noteMap["Eb"] = 622.25;
   noteMap["E" ]                 = 659.26;
   noteMap["E#"] = noteMap["F" ] = 698.46;
   noteMap["F#"] = noteMap["Gb"] = 739.99;
   noteMap["G" ]                 = 783.99;
   noteMap["G#"] = noteMap["Ab"] = 830.61;
}

//======================================================================

int main()
{
   setMap();
   string scale = "C";       // Starting note
   int ms = 500;             // Duration in milliseconds

   while ( true )            // Hmm ... repeats indefinitely
   {
      double freq = noteMap[scale];
      play( freq, ms );
      for ( int semitones : majorScaleIntervals )
      {
         for ( int k = 0; k < semitones; k++ ) freq *= factorSemitone;
         play( freq, ms );
      }
   }
}
lastchance> This code is beautiful :oD~

Tell me, why do you use "while(true)" instead of "for(;;)" ?
punksheep wrote:
Tell me, why do you use "while(true)" instead of "for(;;)" ?


Personal preference, I guess, @punksheep.

I'm a mathematician-turned-engineer, not a computer scientist. (And, apropos this thread, a very-amateur violinist. If you want "Finlandia" played with the Windows "Beep" function then just let me know!).

If there's a good computing reason for preferring a for(;;) loop then I'll hear it ... but I doubt if I can be persuaded to do anything different.
Last edited on
Topic archived. No new replies allowed.