![]() |
| | #1 |
| Registered User Join Date: Nov 2006
Posts: 1
| Simple C question: user input to repeat a loop ![]() I'm currently working on the following code for a musical test and, basically, I'd like to add an option for the user to restart the test from within the program ( rather than running the .exe again). I'll print something like "Do you want to run the test again? Y/N?" Do I use another if/else statement ( like, ..if user_input == 'Y' then ...re-run the for loop - or however you code it - else if user_input =='N' then...quit=TRUE? I know it's a simple question, but I can't seem to suss it. Thanks in advance for any help! insert Code: include "midi_lib.h"
#include <stdio.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
int main(void)
{
int pitch_array[5] = {60,62,64,65,67}; //Range of notes ( C D E F G ) stored in an array//
int count = 0; //Variable called count - counts through notes in the array//
int quit = FALSE; //Variable called quit - starts and ends loop//
int timbre = 1; //Variable called timbre - midi channel allocation//
int pause_val = 1000; //Variable called pause_val - determines note length//
char user_input; //Variable called char - keyboard input from user//
//Changes timbres from initial value 1//
program_change(1, 19); /* rock organ on channel 1 */
program_change(2, 1); /* piano on channel 2 */
program_change(3, 32); /* guitar harmonics on channel 3 */
program_change(4, 105);/* sitar on channel 4 */
printf (" =================PSYCHOACOUSTIC TEST=================");
printf ("\n");
printf ("\n");
printf ("\nThe aim of this test is to determine the point at which a sequence of ascending");
printf ("\n");
printf ("\ntones consisting of different timbres, will appear to:");
printf ("\n");
printf ("\n");
printf ("\n1.descend in pitch, and");
printf ("\n");
printf ("\n2.sound alike to the listener.");
printf ("\n");
printf ("\n");
printf ("\n");
printf ("\nWhen you begin the test, you will initially hear the sequence play slowly and");
printf ("\n");
printf ("\nbe aware of an incrementally ascending pitch scale consisting of easily");
printf ("\n");
printf ("\nidentifiable and contrasting timbres. It will then gradually begin to increase");
printf ("\n");
printf ("\nin speed and,at some point,you should be able to hear the sequence appear to");
printf ("\n");
printf ("\ndescend in pitch.(Initially, you may hear one prominent timbre descending -");
printf ("\n");
printf ("\ntry not to focus on this and wait until you begin to sense an overall effect).");
printf ("\n");
printf ("\nYou should also start to hear the different timbres begin to sound alike.");
printf ("\n");
printf ("\nIf you hear the descending pitches, but not the similarity of timbres, I would");
printf ("\n");
printf ("\nlike you to take the test again in order to establish when this particular");
printf ("\n");
printf ("\neffect occurs. As a general guide, it will probably happen later than the");
printf ("\n");
printf ("\ndescending effect (i.e, when the sequence is faster)");
printf ("\n");
printf ("\nDuring the test, if you need to increase the speed of the sequence, press the");
printf ("\n");
printf ("\nPLUS key until you hear a difference ( you will also see a numerical counter on");
printf ("\n");
printf ("\nyour screen as it plays) Should you need to decrease the speed of the sequence");
printf ("\n");
printf ("\npress the MINUS key. When you hear the sequence appear to descend");
printf ("\n");
printf ("\npress the Q key.This will stop the test and record the time at which");
printf ("\n");
printf ("\nyou perceived the change to occur.");
printf ("\n");
printf ("\n");
printf ("\nAre you are ready to begin the test?");
printf ("\n");
printf ("\n");
printf ("\nPress any key to start");
getch(); //gets any character from the keyboard//
while (quit == FALSE )
{
for(count=0; count < 5; count++) //loop counts from 0 to <5 in increments of one//
{
printf(" \n %d", pause_val); //continously prints the note length in milliseconds to the user//
midi_note(pitch_array[count], timbre, 64); //turns MIDI notes on and sets velocity//
pause(pause_val); // duration specified by variable pause_val//
midi_note(pitch_array[count], timbre, 0); //turns notes off//
timbre++; //midi channel selections increase in increments of one//
pause_val=pause_val-(pause_val/50);} //sets the speed at which sequence increases//
if (timbre == 5) timbre = 1; //when midi channel selection reaches 5, revert to 1//
if ( qwerty_input() ) { //enables user input from keyboard//
user_input=input_char();
if (user_input == '+') {pause_val=pause_val-(pause_val/10); printf("+");} //allows user to increase speed of sequence//
if (user_input == '-') {pause_val=pause_val+(pause_val/10); printf("-");} //allows user to decrease speed of sequence//
if (user_input == 'q') { //allows user to stop test//
printf("You perceived the sequence appear to change at this point = %d \n", pause_val);
getch(); //gets character from the keyboard//
midi_note(pitch_array[count], timbre, 0); // turns MIDI notes off//
quit = TRUE;//ends loop//
break;
}
}
}
return 0;
}
|
| evernaut is offline | |
| | #2 |
| Fear the Reaper... Join Date: Aug 2005 Location: Toronto, Ontario, Canada
Posts: 625
| You have the right idea, but it is nowhere implemented in your code, implement first. Ask for help second. Also, I don't know if you're going for platform-independance, but getch() is an OS-dependant function.
__________________ Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction |
| Happy_Reaper is offline | |
| | #3 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| The first step is to adopt a rigorous approach to code formatting. Poor formatting in simple programs is hard enough to follow, but in a big program it's fatal. Code: include "midi_lib.h"
#include <stdio.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
int main(void)
{
int pitch_array[5] = { 60, 62, 64, 65, 67 }; //Range of notes ( C D E F G ) stored in an array//
int count = 0; //Variable called count - counts through notes in the array//
int quit = FALSE; //Variable called quit - starts and ends loop//
int timbre = 1; //Variable called timbre - midi channel allocation//
int pause_val = 1000; //Variable called pause_val - determines note length//
char user_input; //Variable called char - keyboard input from user//
//Changes timbres from initial value 1//
program_change(1, 19); /* rock organ on channel 1 */
program_change(2, 1); /* piano on channel 2 */
program_change(3, 32); /* guitar harmonics on channel 3 */
program_change(4, 105); /* sitar on channel 4 */
printf(" =================PSYCHOACOUSTIC TEST=================");
printf("\n");
printf("\n");
printf("\nThe aim of this test is to determine the point at which a sequence of ascending");
printf("\n");
printf("\ntones consisting of different timbres, will appear to:");
printf("\n");
printf("\n");
printf("\n1.descend in pitch, and");
printf("\n");
printf("\n2.sound alike to the listener.");
printf("\n");
printf("\n");
printf("\n");
printf("\nWhen you begin the test, you will initially hear the sequence play slowly and");
printf("\n");
printf("\nbe aware of an incrementally ascending pitch scale consisting of easily");
printf("\n");
printf("\nidentifiable and contrasting timbres. It will then gradually begin to increase");
printf("\n");
printf("\nin speed and,at some point,you should be able to hear the sequence appear to");
printf("\n");
printf("\ndescend in pitch.(Initially, you may hear one prominent timbre descending -");
printf("\n");
printf("\ntry not to focus on this and wait until you begin to sense an overall effect).");
printf("\n");
printf("\nYou should also start to hear the different timbres begin to sound alike.");
printf("\n");
printf("\nIf you hear the descending pitches, but not the similarity of timbres, I would");
printf("\n");
printf("\nlike you to take the test again in order to establish when this particular");
printf("\n");
printf("\neffect occurs. As a general guide, it will probably happen later than the");
printf("\n");
printf("\ndescending effect (i.e, when the sequence is faster)");
printf("\n");
printf("\nDuring the test, if you need to increase the speed of the sequence, press the");
printf("\n");
printf("\nPLUS key until you hear a difference ( you will also see a numerical counter on");
printf("\n");
printf("\nyour screen as it plays) Should you need to decrease the speed of the sequence");
printf("\n");
printf("\npress the MINUS key. When you hear the sequence appear to descend");
printf("\n");
printf("\npress the Q key.This will stop the test and record the time at which");
printf("\n");
printf("\nyou perceived the change to occur.");
printf("\n");
printf("\n");
printf("\nAre you are ready to begin the test?");
printf("\n");
printf("\n");
printf("\nPress any key to start");
getch(); //gets any character from the keyboard//
while (quit == FALSE) {
for (count = 0; count < 5; count++) //loop counts from 0 to <5 in increments of one//
{
printf(" \n %d", pause_val); //continously prints the note length in milliseconds to the user//
midi_note(pitch_array[count], timbre, 64); //turns MIDI notes on and sets velocity//
pause(pause_val); // duration specified by variable pause_val//
midi_note(pitch_array[count], timbre, 0); //turns notes off//
timbre++; //midi channel selections increase in increments of one//
pause_val = pause_val - (pause_val / 50);
} //sets the speed at which sequence increases//
if (timbre == 5)
timbre = 1; //when midi channel selection reaches 5, revert to 1//
if (qwerty_input()) { //enables user input from keyboard//
user_input = input_char();
if (user_input == '+') {
pause_val = pause_val - (pause_val / 10);
printf("+");
} //allows user to increase speed of sequence//
if (user_input == '-') {
pause_val = pause_val + (pause_val / 10);
printf("-");
} //allows user to decrease speed of sequence//
if (user_input == 'q') { //allows user to stop test//
printf("You perceived the sequence appear to change at this point = %d \n", pause_val);
getch(); //gets character from the keyboard//
midi_note(pitch_array[count], timbre, 0); // turns MIDI notes off//
quit = TRUE; //ends loop//
break;
}
}
}
return 0;
}
I might be thinking about making the body of your while loop another function, like Code: while ( quit == FALSE ) {
quit = doTest( );
}
Code: while ( quit == FALSE ) {
quit = doTest( );
if ( quit == FALSE ) {
// do "go again" here
}
}
|
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I make it so the program carries on if there is no user input? | Swarvy | C++ Programming | 4 | 04-03-2008 09:07 PM |
| Need some help with C program writing | The_PC_Gamer | C Programming | 9 | 02-12-2008 09:12 PM |
| simple for loop function question | felicityxiv | C Programming | 7 | 05-06-2006 11:43 PM |
| Please don't laugh...SIMPLE loop question! | the_lumin8or | C++ Programming | 5 | 03-31-2006 01:08 PM |
| newbie question regarding user input | cantore | C Programming | 4 | 03-05-2006 08:57 PM |