Reputation: 13
This program so far sweeps through the frequency range 0-20000 Hz and prints every 50 Hz on screen, it then stops when it reaches 20000 Hz. It also stops if I move a control on an on screen midi keyboard.
Now what I'm trying to do is to modify it so that when it has reached 2000 Hz, instead of stopping, the program gives the user an option, something like "Do you want to start this program again?", and if the user types 'Y' then it will start again, but if the user types 'N' the program will stop. I believe a do while
is what I'm looking for, but I've tried and just can't seem to get it to work.
My Code:
#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);
}
Upvotes: 0
Views: 921
Reputation: 78
Your concept is correct, but you have just missed the syntax of do while
loop.
It should be
do {
} while(condition);
So, modified code will be
#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);
}
Upvotes: 4
Reputation: 25516
At very first, you have a syntax error: The while
clause has to be placed outside of the loop body:
do
{
}
while(someCondition); // here!
Variables defined inside the loop body are not accessible outside of it - so you need to declare them before:
char userInput;
do
{
}
while(userInput == 'y');
Apart from that, your code should work fine.
Upvotes: 3