Thread: Can you make an array of commands?

  1. #31
    Registered User
    Join Date
    Jul 2011
    Quote Originally Posted by AndrewHunter View Post
    1. How does this program get the text to read to the user?
    2. Where is this text stored during program execution?
    3. How did you implement "reading the text to the user" (I don't need code for txt -> audio, just where and how called)
    4. How do you keep track of where the program currently is in the text?

    I am asking these questions because it is starting to appear to me that something is amiss with your program design.
    Sorry, actually sound files are stored onto the microcontroller and then, once the user says something, like "access file one", the microcontroller will play that audio. Instead of just linearly playing the audio, I wanted to give the user the ability to rewind and fast forward. The only way I could think about how to do this was create a variable which could be decremented (for rewinding) and incremented (for fast forward) a user specified number of times. This would be much more preferable than having to wait until the entire program is done before repeating. That would be bad.

    So, once the program has read a section of text, the user is prompted to say "continue", "stand by", "go backwards" and "go forwards". Then once the user says "go backwards" or "go forwards" he will again be prompted for the number of sections of audio he wants to go back or forward to. So I thought I would create a variable (DeltaIndex) which is initially set to "0" and then accesses an array of functions that reads the text, the zeroth one being the first section of audio. Then, once the user says "continue", the variable is incremented and the subroutine that tests for the words mentioned before, goes back to the main program, where the array is again accessed by the program with an offset of "DeltaIndex+1"(assuming the user said "continue"). Then, that same variable could be decremented and incremented for going backwards or forwards.

    Before, I did not keep track of where the user was in the text - it just played sound files.
    Last edited by ☼Aulos; 07-23-2011 at 12:56 PM.

  2. #32
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    So is it really seperate commands to access the microcontroller to play the audio files? It seems to me that it would be something like PlayAudioClip(offset) where offset would represent which section of audio to play.

    I have included a mockup sample which I think does generally what you are trying to do. Note I do not need function pointers to implement this at all. Also, this is a quick example; if you were to implement these things pay attention to the comments and also avoid those magic numbers (aka numerical literals for array bound checking). I hope this helps you implement a better solution to your current problem.
    #include <stdio.h>
    #include <string.h>
    //this is just an example to represent the microcontroller that would hold
    //the audio files
    char *AudioFiles[50]={"This is my first line of text",
    					  "This is my second line of text",
    					  "Why doesn't Barbie get pregnant?",
    					  "Because Ken comes in a different box"};
    //This would simulate my command to the microcontroller to play a "file"
    int playAudio(int);
    //This is our function to get the user input and convert to int for use 
    //with our switch statement. Note: There are cleaner solutions for this
    //however let's keep things simple for now
    int getUserInput(void);
    int main(void){
    	//This would be our original offset
    	int delta = 0;
    	//Stores return from getUserInput()
    	int choice = 0;
    	//This is our main program loop where we sample for user input and then make
    	//make our decisions based on that input
    	while((choice=getUserInput()) != 0){
    			case 1: //continue-plays the next line of text
    			case 2: //fast forward-plays 2 lines forward
    				else if(delta<3)
    			case 3: //rewind
    			default://no valid input
    				printf("Command not recognized\n");
    				//this jumps us to the next iteration of our loop
    				//bypassing our playAudio function
    		//This will allow us to keep track of what the current text is
    		//see the definition of the function below for the reason
    		delta = playAudio(delta);
    	return (0);
    int playAudio(int offset){
    	//As stated before this simulates what you need to do for your microcontroller
    	//You don't necessarily need to understand the process behind the array because
    	//that is not what you are dealing with
    	//return our offset to our main loop. I put this in because I am unsure of how your
    	//microcontroller is set up. For instance, on an MP3 player this would most likely 
    	//be the next track, aka offset + 1
    	return (offset);
    int getUserInput(void){
    	//This stores our user input
    	char userInput[10];
    	//The printf and fgets statements can be ignored since they are not how you get 
    	//your input.
    	//used to make our input a null terminated string. May or may not be required for 	//you
                char *nullSearch=NULL;
    	printf("What do you want to do?");
    	fgets(userInput, 10, stdin);
    	//this just makes this a null terminated string.
    		*nullSearch = '\0';
    	//Now this is where we format our input so our main loop can process easier. Note:
    	//I broke this up into seperate functions assuming you had a similar input function
    	//for the microcontroller
    	if(strcmp(userInput, "exit")==0)
    		return 0;
    	else if(strcmp(userInput,"continue")==0)
    		return 1;
    	else if(strcmp(userInput, "ff")==0)
    		return 2;
    	else if(strcmp(userInput, "rw")==0)
    		return 3;
    	//return our command not found error number. Pick one.
    	return (42);
    Let me know if you have any questions.
    Last edited by AndrewHunter; 07-23-2011 at 07:40 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make an array of classes in c#
    By alireza in forum C# Programming
    Replies: 8
    Last Post: 11-16-2010, 05:16 PM
  2. Make an array of arrays
    By JOCAAN in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 12:18 PM
  3. How do I make a *HUGE* array in C++?
    By rooster in forum C++ Programming
    Replies: 10
    Last Post: 02-27-2006, 08:05 PM
  4. how to make an array of strings
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-24-2003, 01:18 PM
  5. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:12 PM

Tags for this Thread