Changing Array Values

Hi again all, i'm looking to change the index of my array with a for loop, but for some reason i'm getting these build errors:

error C2143: syntax error : missing ',' before '<='
error C2086: 'int i' : redefinition
see declaration of 'i'
syntax error : missing ';' before '{'

Here is the loop:
for(int i = MajorIntervalAscend[i], i<= 7, i++)
{
sendMidiMessage(0x90, MajorIntervalAscend[0], 0x7f);
delay(userDelay);
sendMidiMessage(0x80, MajorIntervalAscend[0], 0x7f);
}


i'm an absolute beginner so its probably some glaring error that i cant see =[

thanks in advance
Use semi colons ';' in your for loop, not comma's ','. that should help

also, do you have 'i' declared elsewhere in your code?

:)
Last edited on
thanks, that worked, BUT in the for loop, it wont let me use i in the MajorIntervalAscend[]

it works if i have 0 in there, but then the loop doesn't move forward. any thoughts?

int play = 0;
while(play <= TimesPlayed)
{
for(int i = MajorIntervalAscend[0]; i<= 7; i++)
{
sendMidiMessage(0x90, MajorIntervalAscend[0], 0x7f);
delay(userDelay);
sendMidiMessage(0x80, MajorIntervalAscend[0], 0x7f);
}
play++;

}
take a look at the the loop examples below, you may need to initialize your array (looking at the way the loop is written), hope it helps :)

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

#include <iostream>
using namespace std;

int main ()

{

	int test[10];
	int i = 0;
		//initializing array test[], otherwise it won't work the way your for loop is written
	for (i=0; i<=9; i++){ test[i] = i; cout << test[i];}

	cout << "\n";
	
	i = 0; // this initialization will reset test[i] to '0' instead of '9' from previous array initialization
		// without this initialization the loop won't run
		//next is the format your loop is written('i' takes on the value of test[i])
	for(i = test[i]; test[i]<=7; i++) cout << test[i];		


	cout << "\n";
	
	i = 0;// reinitializing to '0' so next loop will run
		//if you want test[i] to take on the value of 'i', the loop should be written as below
		//and the array initialization loop (1st loop) above is unnecessary

	for(test[i] = i ; test[i]<=7; i++) cout << test[i];


	return 0;

}



EDIT: sorry, I forgot you were using the array in the upper boundary part of the loop, I changed the examples to reflect that

Double EDIT: I also posted this mistakenly to your other thread,,, jeez louise! :)
Last edited on
just removing my code unless anybody from uni tries to nick it
Last edited on
the MajDirection variable is of type char
If you press 1 on the keyboard you will get the ascii code for 1 which is 49 - not decimal 1 as you are expecting. That is why the switch doesn't work
thanks, didn't spot that, but its more the for loop thats annoying me, it seems to be skipping straight past it instead of playing the scale =[
I'm not sure if this will solve the problem, but it did get the for loop going for me. I initialized 'i' before the for loop then replaced the :
for(int i = MajorIntervalAscend[0]; i<= 7; i++)
with the loop as you originally intended to display:
for(int i = MajorIntervalAscend[i]; i<= 7; i++)]

I also changed MajDirection variable type to int

here is the code snippet(line 3 & 11) .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
,,,,,,,
	int play = 0;
	int i = 0;
      //ignore the Case 2 part
	switch(MajDirection)
	{
		case 1 :
			
			while(play <= MajTimesPlayed)
				{
					for(i = MajorIntervalAscend[i]; i<= 7; i++)
					{
												
						sendMidiMessage(0x90, MajorIntervalAscend[0], 0x7f);
						delay(MajUserDelay);
						sendMidiMessage(0x80, MajorIntervalAscend[0], 0x7f);
					}
					play++;

				} 
			break;

,,,,,,,

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
    while(play <= MajTimesPlayed)
    {
      for(int i = MajorIntervalAscend[0]; i<= 7; i++) //This can't be right - see explanation below
      {
        sendMidiMessage(0x90, MajorIntervalAscend[0], 0x7f);
        delay(MajUserDelay);
        sendMidiMessage(0x80, MajorIntervalAscend[0], 0x7f);
      }
      play++;

    } 
    break;


At the start of the program you have this code:
1
2
3
4
5
6
7
8
	//user input stage
	cout << "Which note would you like the scale to start on? (60 = Middle C)" << endl;
	cin >> MajStartingNote;
	 
	//Assign Array Values
	MajorIntervalAscend[0] = MajStartingNote ;
	MajorIntervalAscend[1] = (MajorIntervalAscend[0] + (halfstep*2));
	MajorIntervalAscend[2] = (MajorIntervalAscend[1] + (halfstep*2));


This means that if for example the user enters 60 for middle C - then MajorIntervalAscend[0] = MajStartingNote ; = 60.

So in your for loop for(int i = MajorIntervalAscend[0] means i will start at 60 - so i<= 7 will always test false which means loop will NOT run
thankyou all =] i get where the problem is now, but i still cant get it to work. the major scale has set intervals defined by the array, but then i need the loop to index each stage of the array to play the relevant midi note.
i'm really sorry to keep arsing around and probably wasting your time but i've really hit a wall here.

thanks
the for loop DOES work as I layed out above. I don't have audio.h in my library so I had to modify the program to display to the console the values as shown here, with the output I get(input '60' as MajStartingNote) .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

	int play = 0;
	int i = 0;
      //ignore the Case 2 part
	switch(MajDirection)
	{
		case 1 :
			
			while(play <= MajTimesPlayed)
				{
					for(i = MajorIntervalAscend[i]; i<= 7; i++)
					{
						{cout << "musical note  " << MajorIntervalAscend[i] << "\n" ;}
						
						//sendMidiMessage(0x90, MajorIntervalAscend[0], 0x7f);
						//delay(MajUserDelay);
						//sendMidiMessage(0x80, MajorIntervalAscend[0], 0x7f);
					}
					play++;

				} 
			break;





musical note  60
musical note  62
musical note  64
musical note  65
musical note  67
musical note  69
musical note  71
musical note  72







EDIT:

looking at the rest of your arguements in your for loop(I am completely unfamiliar with audio applications) I am wondering if your arguements for MajorIntervalAscend should be changed to:

1
2
3
sendMidiMessage(0x90, MajorIntervalAscend[i], 0x7f);
delay(MajUserDelay);
sendMidiMessage(0x80, MajorIntervalAscend[i], 0x7f);


?
Last edited on
And if you look at my explanation you will see I'm explaining the OP's original code.

Topic archived. No new replies allowed.