ThreadBoard ArchivesSite FeaturesActiveworlds SupportHistoric Archives |
Mult. lists (Sdk)
Mult. lists // SdkvelenoDec 17, 1998, 6:03pm
I'm trying to get my DJ bot to get the user to chose between 3 different
midi lists during the login procedure. Anything I keep trying causes the program to coredump once it reaches that point in the code. Here's a few pieces from the source... see if you can figure out what I'm doing wrong... Would be greatly appreciated. #include <stdio.h> ...... (rest of the needed headers, of course) int list; //declared as global variable int current_midi; char *midis[] = {...} //continue for 3 lists... .... main (int argc; char *argv[]) { .... //login coding... printf("Option for Midi List (1,2 or 3) :\n1: List one\n2: List 2\n3: List 3\n> "); gets(list); //create speaker //query for speaker object //Then to the main event loop... .... } //bot function creates speaker on entry void change_midi (void) { static int rc; //static because of an rc function used later char *sptr; char action[200], descrip[200]; aw_int_set(AW_OBJECT_OLD_NUMBER, speaker_number); aw_int_set(AW_OBJECT_OLD_X, speaker_x); aw_int_set(AW_OBJECT_OLD_Z, speaker_z); aw_int_set(AW_OBJECT_X, speaker_x); aw_int_set(AW_OBJECT_Y, speaker_y); aw_int_set(AW_OBJECT_Z, speaker_z); aw_int_set(AW_OBJECT_YAW, speaker_yaw); aw_int_set(AW_OBJECT_OWNER, atoi(lowner)); //char -> int defined in login procedure as lowner. global aw_string_set(AW_OBJECT_MODEL, SPEAKER); //global deffinition sprintf(descrip, "Music Speaker playing..."); aw_string_set(AW_OBJECT_DESCRIPTION, descrip); if (list == 1) { sprintf(action, "create sound %s", 1midis[1current_midi++]); if(!1midis[1current_midi]) 1current_midi = 0; aw_string_set(AW_OBJECT_ACTION, action); if (rc = aw_object_change()) { printf("Unable to change speaker (reason %d)\n", rc); rc_errors(rc); } else { sptr = strrchr (1midis[1current_midi],'/'); sptr++; printf("Speaker Music Changed - %s\n", sptr); speaker_number = aw_int(AW_OBJECT_NUMBER); } } else if (list == 2) { sprintf(action, "create sound %s", 2midis[2current_midi++]); if(!2midis[2current_midi]) 2current_midi = 0; aw_string_set(AW_OBJECT_ACTION, action); if (rc = aw_object_change()) { printf("Unable to change speaker (reason %d)\n", rc); rc_errors(rc); } else { sptr = strrchr(2midis[2current_midi], '/'); sptr++; printf("Speaker Music Changed - %s\n", sptr); speaker_number = aw_int(AW_OBJECT_NUMBER); } } else if (list == 3) { sprintf(action, "create sound %s", 3midis[3current_midi++]); if(!3midis[3current_midi]) current_midi = 0; aw_string_set(AW_OBJECT_ACTION, action); if (rc = aw_object_change()) { printf("Unable to change speaker (reason %d)\n", rc); rc_errors(rc); } else { //strrchr set left off here due to change in char * on the 3rd global midi list above printf("Speaker Music Changed - %s\n", 3midis[3current_midi]); speaker_number = aw_int(AW_OBJECT_NUMBER); } } else { aw_int_set(AW_OBJECT_NUMBER, speaker_number); aw_int_set(AW_OBJECT_X, speaker_x); aw_int_set(AW_OBJECT_Z, speaker_z); if (rc = aw_object_delete()) { printf ("Unable to delete object (reason %d)\n", rc); rc_errors(rc); } else puts ("Speaker deleted"); } } //then of course the rest of the querying process and remaining event functions walter knupeDec 17, 1998, 7:57pm
See my comments below
Veleno schrieb in Nachricht <3679637A.8484160 at hiwaay.net>... >int list; //declared as global variable >int current_midi; >char *midis[] = {...} >//continue for 3 lists... the exact declaration would help here... >... >main (int argc; char *argv[]) >{ >... >//login coding... > printf("Option for Midi List (1,2 or 3) :\n1: List one\n2: List >2\n3: List 3\n> "); > gets(list); gets returns the line as input. it expects a character pointer, not an integer.. try the following code block { // read a number from stdin char line[256]; // or whatever your max input line lenght might be gets(line); // get input line list = atoi(line); // convert to integer } // now you have the number in the integer variable called <list> > [....] some code skipped.. > if (list == 1) { > sprintf(action, "create sound %s", 1midis[1current_midi++]); > if(!1midis[1current_midi]) what are 1midis and 1current_midi ? as far as i know you can't start a variable with a number. I wouldn't expect the code to compile here same for 2midis and 3midis, etc.. [....] some code skipped.. > //strrchr set left off here due to change in char * on the 3rd >global midi list above > printf("Speaker Music Changed - %s\n", 3midis[3current_midi]); whats the difference here so that you change your procedure ? you can safely pass char * to strrchr by casting it to (const char *) (the compiler should do that implicit since its safe to have a non const variable beeing read by a function expecting it to be const). Walter andras sarkozyDec 19, 1998, 7:43am
char *gets( char *); is the gets function prototype.
You used an integer and that creates the coredump. I'm surprized that your compiler let you do it tho. [View Quote] > I'm trying to get my DJ bot to get the user to chose between 3 different > midi lists during the login procedure. Anything I keep trying causes the > program to coredump once it reaches that point in the code. Here's a few > pieces from the source... see if you can figure out what I'm doing > wrong... Would be greatly appreciated. > > #include ?stdio.h? > ..... (rest of the needed headers, of course) > int list; //declared as global variable > int current_midi; > char *midis[] = {...} > //continue for 3 lists... > ... > main (int argc; char *argv[]) > { > ... > //login coding... > printf("Option for Midi List (1,2 or 3) :\n1: List one\n2: List > 2\n3: List 3\n? "); > gets(list); > //create speaker > //query for speaker object > //Then to the main event loop... > ... > } > //bot function creates speaker on entry > void change_midi (void) > { > static int rc; //static because of an rc function used later > char *sptr; > char action[200], descrip[200]; > aw_int_set(AW_OBJECT_OLD_NUMBER, speaker_number); > aw_int_set(AW_OBJECT_OLD_X, speaker_x); > aw_int_set(AW_OBJECT_OLD_Z, speaker_z); > aw_int_set(AW_OBJECT_X, speaker_x); > aw_int_set(AW_OBJECT_Y, speaker_y); > aw_int_set(AW_OBJECT_Z, speaker_z); > aw_int_set(AW_OBJECT_YAW, speaker_yaw); > aw_int_set(AW_OBJECT_OWNER, atoi(lowner)); //char -? int defined in > login procedure as lowner. global > aw_string_set(AW_OBJECT_MODEL, SPEAKER); //global deffinition > sprintf(descrip, "Music Speaker playing..."); > aw_string_set(AW_OBJECT_DESCRIPTION, descrip); > if (list == 1) { > sprintf(action, "create sound %s", 1midis[1current_midi++]); > if(!1midis[1current_midi]) > 1current_midi = 0; > aw_string_set(AW_OBJECT_ACTION, action); > if (rc = aw_object_change()) { > printf("Unable to change speaker (reason %d)\n", rc); > rc_errors(rc); > } else { > sptr = strrchr (1midis[1current_midi],'/'); > sptr++; > printf("Speaker Music Changed - %s\n", sptr); > speaker_number = aw_int(AW_OBJECT_NUMBER); > } > } else if (list == 2) { > sprintf(action, "create sound %s", 2midis[2current_midi++]); > if(!2midis[2current_midi]) > 2current_midi = 0; > aw_string_set(AW_OBJECT_ACTION, action); > if (rc = aw_object_change()) { > printf("Unable to change speaker (reason %d)\n", rc); > rc_errors(rc); > } else { > sptr = strrchr(2midis[2current_midi], '/'); > sptr++; > printf("Speaker Music Changed - %s\n", sptr); > speaker_number = aw_int(AW_OBJECT_NUMBER); > } > } else if (list == 3) { > sprintf(action, "create sound %s", 3midis[3current_midi++]); > if(!3midis[3current_midi]) > current_midi = 0; > aw_string_set(AW_OBJECT_ACTION, action); > if (rc = aw_object_change()) { > printf("Unable to change speaker (reason %d)\n", rc); > rc_errors(rc); > } else { > //strrchr set left off here due to change in char * on the 3rd > global midi list above > printf("Speaker Music Changed - %s\n", 3midis[3current_midi]); > speaker_number = aw_int(AW_OBJECT_NUMBER); > } > } else { > aw_int_set(AW_OBJECT_NUMBER, speaker_number); > aw_int_set(AW_OBJECT_X, speaker_x); > aw_int_set(AW_OBJECT_Z, speaker_z); > if (rc = aw_object_delete()) { > printf ("Unable to delete object (reason %d)\n", rc); > rc_errors(rc); > } else > puts ("Speaker deleted"); > } > } > //then of course the rest of the querying process and remaining event > functions |