这个程序到目前为止扫过的频率范围0-20000赫兹和打印每50赫兹在屏幕上,然后停止,当它达到20000赫兹。如果我在屏幕上的midi键盘上移动一个控件,它也会停止。
现在我要做的是修改它,当它达到2000赫兹,而不是停止,程序给用户一个选项,比如“你想再次启动这个程序吗?”,如果用户键入'Y‘,那么它将重新启动,但是如果用户输入'N’,程序将停止。我相信do while是我要找的,但我试过了,但似乎无法让它发挥作用。
我的守则:
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
int main()
{
int frequency = 0.0;
int modulation = aserveGetControl(1);
printf("aserve get note control is %d\n", modulation);
do
{
while( frequency < 20000.0 && modulation == 0)
{
modulation = aserveGetControl(1);
aserveOscillator(0,frequency,1.0,0);
printf("The frequency is: %dHz\n", frequency);
frequency = frequency + 50.0;
aserveSleep(20);
}
char userInput;
printf("enter 'y' if you would like to repeat: ");
scanf(" %c", &userInput);
while(userInput == 'y');
}
return(0);
}发布于 2017-03-29 11:45:40
您的概念是正确的,但是您刚刚错过了do while循环的语法。
它应该是
do {
} while(condition);因此,修改后的代码将是
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
int main()
{
int frequency = 0.0;
int modulation = aserveGetControl(1);
printf("aserve get note control is %d\n", modulation);
char userInput;
do
{
while( frequency < 20000.0 && modulation == 0)
{
modulation = aserveGetControl(1);
aserveOscillator(0,frequency,1.0,0);
printf("The frequency is: %dHz\n", frequency);
frequency = frequency + 50.0;
aserveSleep(20);
}
printf("enter 'y' if you would like to repeat: ");
scanf(" %c", &userInput);
} while(userInput == 'y');
return(0);
}发布于 2017-03-29 11:46:09
首先,您有一个语法错误:必须将while子句放在循环主体之外:
do
{
}
while(someCondition); // here!在循环体中定义的变量在其外部是不可访问的,因此您需要在此之前声明它们:
char userInput;
do
{
}
while(userInput == 'y');除此之外,您的代码应该运行良好。
https://stackoverflow.com/questions/43091739
复制相似问题