【问题标题】:end loop based on input from user基于用户输入的结束循环
【发布时间】:2015-05-30 01:13:20
【问题描述】:

我的程序从用户那里接受一个数字来确定正在记录的序列的长度。我将如何获取该数字并让它确定执行此循环的次数。 While(true) 显然根本不允许循环结束。

提前致谢

这是从 MIDI 输入合成声音的功能

void midisound (int note)
    {
        int velocity;
        int playingNote = -1;
        float frequency;

        while(true)
        {
            note = aserveGetNote();
            velocity = aserveGetVelocity();

            if(velocity > 0)
            {
                frequency = 440 * pow(2, (note-69) / 12.0);
                aserveOscillator(0, frequency, 1.0, 0);
                playingNote = note;
            }
            else if(note == playingNote)
            {
                aserveOscillator(0, 0, 0, 0);
            }

        }


    }

---here is where function ^ is called in the program----

   if (reclayer == 1)
                    {
                        //open first text file for layer 1 to be written to
                        textFilePointer = fopen("recording1.txt", "w+");
                        if(textFilePointer == NULL)
                        {
                            printf("Error Opening File!");
                        }
                        else
                        {

                            //function call to write notes and vel data
                            notetofile(input, seqlen, reclayer);
                            printf("Would you like to record a second layer or re-record? (y or n)\n");
                            scanf(" %c", &choice2);
                        }

                    }

【问题讨论】:

  • 使用一个变量并将其初始化为零。然后为每个循环增加它。使用它,您可以获得执行循环的次数。

标签: c function loops midi


【解决方案1】:

使用 for 循环。

for ( i = 0; i < note; i++ ) {
    // Your code here
}

这将执行 'note' 次数。

【讨论】:

    【解决方案2】:

    使用可以使用scanf()函数从控制台输入用户号:

    void midisound (int note) {
        int input;
        int velocity;
        int playingNote = -1;
        float frequency;
    
        printf("Enter integer number of times to loop: ");
        scanf("%d", &input);
    
        while(input > 0) {
            input = input - 1;
            note = aserveGetNote();
            velocity = aserveGetVelocity();
    
            if(velocity > 0) {
                frequency = 440 * pow(2, (note-69) / 12.0);
                aserveOscillator(0, frequency, 1.0, 0);
                playingNote = note;
    
            } else if(note == playingNote) {
                aserveOscillator(0, 0, 0, 0);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      在第一次从用户输入的数字中获取数字的时间循环将运行使用以下

      对于 (i=0 ; i

      【讨论】:

        【解决方案4】:

        首先分配一个变量来保存你想要重复的次数。例如,让我们以int n = 5; 为例。 (您也可以通过调用scanf( "%d " , &amp;n ); 让用户输入n 的值(这就是您所要求的),然后执行其余操作)

        int n ;
        scanf( "%d " , &n );
        

        您可以将其用作我以下所有情况的通用方法

        然后,只需添加一个简单的for循环,例如

        int i;
        for ( i = 0 ; i < n ; i++ )
          {
             // The code that you want to repeat
          }
        

        这应该可以解决问题。

        如果您想使用 while 循环,那么像以前一样,让 n 成为循环必须执行的次数。那么

        int i=0;
        while ( i < n )
          {
             // Your code
             i++;
          }
        

        您也可以使用while ( true ) 循环,但您只需给出一个条件并使用break;。让我们以上面的例子为例。

        int i=0;
        while ( true )
          {
             // your code
             i++;
             if ( i == n )
                break;
          }
        

        这些只是许多不同的可能性。如果您尝试,您甚至可以提出自己的条件。

        编码愉快.... 8-)

        【讨论】:

          猜你喜欢
          • 2015-06-07
          • 1970-01-01
          • 2013-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多