2014-12-02 44 views
0

选项(2)崩溃/重载我的编码软件,它具有与选项(1)相同的代码,是否有人知道为什么要这样做以及如何解决它?我的代码有什么问题?向后循环不起作用(选项2)

#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:%5d\n", 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:%5d\n", 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); 
} 

回答

0

您的for循环不完全相同。

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

第二种选择经过{8,7,...,1}

还要注意,控制[8 ]未定义(0..7)。所以当它试图引用这个位置时,应用程序会出错。

更改第二个for循环

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

非常感谢!帮助我摆脱了车辙!总是忘记0作为一个数字。 – 2014-12-02 16:56:08