首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C语言编程中,做一个并发循环,询问用户是否希望该程序再次运行。

在C语言编程中,做一个并发循环,询问用户是否希望该程序再次运行。
EN

Stack Overflow用户
提问于 2017-03-29 11:04:20
回答 2查看 631关注 0票数 0

这个程序到目前为止扫过的频率范围0-20000赫兹和打印每50赫兹在屏幕上,然后停止,当它达到20000赫兹。如果我在屏幕上的midi键盘上移动一个控件,它也会停止。

现在我要做的是修改它,当它达到2000赫兹,而不是停止,程序给用户一个选项,比如“你想再次启动这个程序吗?”,如果用户键入'Y‘,那么它将重新启动,但是如果用户输入'N’,程序将停止。我相信do while是我要找的,但我试过了,但似乎无法让它发挥作用。

我的守则:

代码语言:javascript
复制
#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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-29 11:45:40

您的概念是正确的,但是您刚刚错过了do while循环的语法。

它应该是

代码语言:javascript
复制
do {

} while(condition);

因此,修改后的代码将是

代码语言:javascript
复制
#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);
}
票数 4
EN

Stack Overflow用户

发布于 2017-03-29 11:46:09

首先,您有一个语法错误:必须将while子句放在循环主体之外:

代码语言:javascript
复制
do
{
}
while(someCondition); // here!

在循环体中定义的变量在其外部是不可访问的,因此您需要在此之前声明它们:

代码语言:javascript
复制
char userInput;
do
{
}
while(userInput == 'y');

除此之外,您的代码应该运行良好。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43091739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档