Thread: Simple C question: user input to repeat a loop

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    Simple C question: user input to repeat a loop

    Hi guys - new to the board ( and C in general)...so go easy

    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;
    }

  2. #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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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;
    }
    One tip is to make sure your editor always uses spaces for indenting, even when you press tab. It's the only way to ensure everyone sees what you see in your editor.

    I might be thinking about making the body of your while loop another function, like
    Code:
    while ( quit == FALSE ) {
      quit = doTest( );
    }
    Which would become
    Code:
    while ( quit == FALSE ) {
      quit = doTest( );
      if ( quit == FALSE ) {
        // do "go again" here
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM