Hi all,
I'm really new to this so please excuse my ignorance.
I'm making a program that works out the frequency in hz of a musical note.
The user enters the note name, then the octave its in, then it reads the frequency from an array and prints that to the console.
Initially, the variable pitch was a char, but then I realised that I would need to change this into a string type so that the user could enter in # or b (sharp or flat)
I'm now converting the string varialble into an Int so that I can use it in a switch, which will then read off the correct array.
So I'm using a series of else-ifs to get the relevant int value.
The program builds but when I run it two problems occur (I added in cout pitchInt to check if this was where it was going wrong)
a) pitchInt always ends up as either 1 (if pitch ==a) or 2, not the others.
b) the programs skips the switch and terminates after displaying pitchInt.
Would really appreciate any help.
Also I've not entered in all the array data yet, I wanted to check I could get this all running before I go through that labourious process!
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#include <string>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//enter the note name eg C
string pitch;
cout << "enter the pitch of your note" << endl;
cin >> pitch;
cout << "pitch is:" << pitch << endl;
//convert pitches into integer values because switch conditional statements can't use string variables.
//remember you now have to change all single quote marks to double speech marks because its a string not a char anymore.
int pitchInt;
if (pitch == "a")
{
pitchInt = 1;
}
else if (pitch == "bb" or "a#")
{
pitchInt = 2;
}
else if (pitch == "b")
{
pitchInt = 3;
}
else if (pitch == "c")
{
pitchInt = 4;
}
else if (pitch == "c#" or "db")
{
pitchInt = 5;
}
else if (pitch == "d")
{
pitchInt = 6;
}
else if (pitch == "d" or "eb")
{
pitchInt = 7;
}
else if (pitch == "e")
{
pitchInt = 8;
}
else if (pitch == "f")
{
pitchInt = 9;
}
else if (pitch == "f#" or "gb")
{
pitchInt = 10;
}
else if (pitch == "g")
{
pitchInt = 11;
}
else if (pitch == "g#" or "ab")
{
pitchInt = 12;
}
else
{
cout << "No such note. Go back to music school." << endl;
}
cout << "pitchInt is:" << pitchInt<< endl;
//initialise arrays
float frequencyArrayA[5];
frequencyArrayA[0] = 27.5;
frequencyArrayA[1] = 55.0;
frequencyArrayA[2] = 110.0;
frequencyArrayA[3] = 220.0;
frequencyArrayA[4] = 440.0;
float frequencyArrayBb[5];
frequencyArrayBb[0] = 29.14;
frequencyArrayBb[1] = 58.27;
frequencyArrayBb[2] = 116.54;
frequencyArrayBb[3] = 233.08;
frequencyArrayBb[4] = 466.16;
float frequencyArrayB [5];
frequencyArrayB[0] = 30.87;
frequencyArrayB[1] = 61.74;
frequencyArrayB[2] = 123.47;
frequencyArrayB[3] = 246.94;
frequencyArrayB[4] = 493.88;
float frequencyArrayC [5];
frequencyArrayC[0] = 16.35;
frequencyArrayC[1] = 32.70;
frequencyArrayC[2] = 65.41;
frequencyArrayC[3] = 130.81;
frequencyArrayC[4] = 261.63;
switch(pitchInt) {
case '1':
cout << "enter the octave 0-4" <<endl;
int nA;
cin >> nA;
cout << "the frequency of the note is:";
cout << frequencyArrayA[nA] << "hz" << endl;
break;
case '2':
int nBb;
cin >> nBb;
cout << "the frequency of the note is:";
cout << frequencyArrayBb[nBb] << "hz" << endl;
break;
case '3':
cout << "enter the octave 0-4" <<endl;
int nB;
cin >> nB;
cout << "the frequency of the note is:";
cout << frequencyArrayB[nB] << "hz" << endl;
break;
case '4':
cout << "enter the octave 0-4" <<endl;
int nC;
cin >> nC;
cout << "the frequency of the note is:";
cout << frequencyArrayC[nC] << "hz" << endl;
break;
}
return 0;
}
|