我的代码有什么问题?向后循环不起作用(选项 2)



选项 (2) 使我的编码软件崩溃/过载,它与选项 (1) 具有相同的代码,有人知道它为什么要这样做以及如何修复它吗?

#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
int length();
float mtof(int note);
int main() {
    // do while the user hasnt pressed exit key (whatever)
    int control[8] = {74, 71, 91, 93, 73, 72, 5, 84};
    int index;
    int mod;
    float frequency;
    int notes[8];
    int response;
    mod = aserveGetControl(1);
    // ask backwards, forwards, exit
    // SCALING
    // (getControl(75) / ((127 - 0) / (1000 - 100))) + 100;
    while(true) {
        printf("Run Loop Forwards (1), Backwards (2), Exit (0)n");
        scanf("%d", &response);
        if(response == 1) {
            while(mod == 0) {
                for(index = 0; index < 8; index++) {
                    notes[index] = aserveGetControl(control[index]);
                    frequency = mtof(notes[index]);
                    aserveOscillator(0, frequency, 1.0, 0);
                    aserveSleep(length());
                    printf("Slider Value:%5dn", notes[index]);
                    mod = aserveGetControl(1);
                }
            }
        } else if(response == 2) {
            // here is the part where the code is exactly
            // the same apart from the for loop which is
            // meant to make the loop go backwards
            while(mod == 0) {
                for(index = 8; index > 0; index--) {
                    notes[index] = aserveGetControl(control[index]);
                    frequency = mtof(notes[index]);
                    aserveOscillator(0, frequency, 1.0, 0);
                    aserveSleep(length());
                    printf("Slider Value:%5dn", notes[index]);
                    mod = aserveGetControl(1);
                }
            }
        } else if(response == 0) {
            return 0;
        }
    }
}
int length() {
    return (aserveGetControl(75)/((127.0 - 0) / (1000 - 100))) + 100;
}
float mtof(int note) {
    return 440 * pow(2, (note-69) / 12.0);
}

你的 for 循环并不完全相同。

第一个选项通过 { 0, 1, ..., 7 }

第二个选项通过 { 8, 7, ..., 1 }

另请注意,控件[8]是未定义的(0..7)。因此,当它尝试引用此位置时,应用程序会遇到错误。

将第二个 for 循环更改为

for (index = 7; index >= 0; index--) {
    ...
}

最新更新