Weird output in my code :(

Hi everyone,

Could someone very nice please check through my code when they get a chance and let me know why i am experiencing weird output and results when running the program. I'm new to C++ and cannot see for the life of me what could be going wrong.

It all works well except that for some reason the first note duration melody1_NOTE_VALUE[i - 1] is not as the user entered. Also, for the 5th (final note), melody1_NOTE_VALUE[i - 1] is either 0 or some really high number and nothing like the user entered.

There's something screwy going on with the melody1_NOTE_MIDI_VALUE[i - 1] value too. Its nothing like the value the ENTER_NOTE_VALUE_CHECK() function should set. Only some of elements in the NOTE_MIDI_VALUE[] array are affected, the other's seem ok. Everything else checks out.

Paul..


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
144
145
146

#include <iostream>
#include <string>
#include <cmath>
#include <windows.h>
#include <mmsystem.h>
#include <dos.h>

using namespace std;

// *******************
// DECLARE VARIABLES..
// *******************
string melody1_NOTE_VALUE[4];     // String input for MIDI key (A, C, G# etc)..
int OCTAVE[4] = {};     // Integer value for chosen Octave (0-9)..
int NOTE_LENGTH[4] = {};     // String value for note length..
double melody1_NOTEBEEP_FREQ[4] = {};     // Note Beep Frequency for sequence 1..
int melody1_NOTE_MIDI_VALUE[4] = {};     // MIDI Note Value for Sequence 1..
int i;
// ***********************
// END DECLARE VARIABLES..
//************************

// ***********************
// FUNCTIONS DECLARATION..
// ***********************

// FUNCTION: CAPTURE INPUT FOR OCTAVE VALUE..
void ENTER_OCTAVE()
{
Octave:
	cout << "Octave for this note (1-9): ";
	cin >> OCTAVE[i - 1];
	if (OCTAVE[i - 1] < 1 || OCTAVE[i - 1] > 10)
	{
		goto Octave;
	}
}
// END FUNCTION..

// FUNCTION: CAPTURE INPUT FOR NOTE LENGTH..
void ENTER_NOTELENGTH()
{
	cout << "Note Length (in milliseconds): ";
	cin >> NOTE_LENGTH[i - 1];
}
// END FUNCTION..

// FUNCTION: SWITCH CASE FOR INPUT CHECKING NOTE VALUES..
void ENTER_NOTE_VALUE_CHECK()
{
	// If C or c is pressed..
	if (melody1_NOTE_VALUE[i - 1] == "C" || melody1_NOTE_VALUE[i - 1] == "c")
	{
		ENTER_OCTAVE();
		// Check what OCTAVE has been entered and set MIDI and Frequency values.. 
		switch (OCTAVE[i - 1])
		{
		case 9:
			melody1_NOTEBEEP_FREQ[i - 1] = 8372.0;     // C9 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 108;     // MIDI code..
			break;
		case 8:
			melody1_NOTEBEEP_FREQ[i - 1] = 4186.0;     // C8 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 96;     // MIDI code..
			break;
		case 7:
			melody1_NOTEBEEP_FREQ[i - 1] = 2093.0;     // C7 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 84;     // MIDI code..
			break;
		case 6:
			melody1_NOTEBEEP_FREQ[i - 1] = 1047.0;     // C6 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 72;     // MIDI code..
			break;
		case 5:
			melody1_NOTEBEEP_FREQ[i - 1] = 523.3;     // C5 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 60;     // MIDI code..
			break;
		case 4:
			melody1_NOTEBEEP_FREQ[i - 1] = 261.6;     // C4 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 48;     // MIDI code..
			break;
		case 3:
			melody1_NOTEBEEP_FREQ[i - 1] = 130.8;     // C3 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 36;     // MIDI code..
			break;
		case 2:
			melody1_NOTEBEEP_FREQ[i - 1] = 65.41;     // C2 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 24;     // MIDI code..
			break;
		case 1:
			melody1_NOTEBEEP_FREQ[i - 1] = 32.70;     // C1 Frequency..
			melody1_NOTE_MIDI_VALUE[i - 1] = 12;     // MIDI code..
			break;
		default:
			cout << "No Octave value selected" << endl;
			break;
		} // END SWITCH..
	} // END IF..

	else     // IF BAD INPUT..
	{
		// IF BAD INPUT..
		cout << "Bad input!!!" << endl;
		//ENTER_NOTE();
	} // END IF..

} // END FUNCTION..

  // FUNCTION: CAPTURE INPUT FOR MIDI NOTE VALUE..
void ENTER_NOTE()
{
	cout << "\nNote value (C, D#, A etc): ";
	cin >> melody1_NOTE_VALUE[i - 1];
	ENTER_NOTE_VALUE_CHECK();
} // END FUNCTION..

 // ***************************
 // END FUNCTIONS DECLARATION..
 // ***************************

 // ***********************
 // ****** MAIN CODE ******
 // ***********************
int main(void)
{
	// Loop through note information entry until all 5 notes have been completed..
	for (i = 1; i < 6; ++i)
	{
		ENTER_NOTE();
		ENTER_NOTELENGTH();
	}

	// Loop through note information display until all 5 notes have been shown..
	for (i = 1; i < 6; ++i)
	{
		cout << "\nNote " << i << ": " << melody1_NOTE_VALUE[i - 1] << OCTAVE[i - 1] << " | Length: " << NOTE_LENGTH[i - 1] << "ms.." << " (" << melody1_NOTE_MIDI_VALUE[i - 1] << " (MIDI No) / " << melody1_NOTEBEEP_FREQ[i - 1] << "Hz)";
	}

	// Loop through playing beeps until all 5 notes have been played..
	for (i = 1; i < 6; ++i)
	{
		Beep(melody1_NOTEBEEP_FREQ[i - 1], NOTE_LENGTH[i - 1]);
	}
	return 0;     // End code..
}
Forgot to also say, if you are going to run this code, type C or c for all the note values. This is a stripped down version of the full code which checks that the user enters a valid music note value for all instances.
Topic archived. No new replies allowed.