Thread: Quick question

  1. #1
    River21
    Guest

    Quick question

    Sorry, I'm having a huge brainfart and need some help. I have the following code and I'm trying to read a string from the user (maximum of three strings) then execute those commands. Everytime I compile, I get a segmentation fault. Can someone out there help me find my mistake?

    #include <stdio.h>
    #include <string.h>

    int main() {

    int i, j, times = 1;
    char choice;
    char *command[3];
    char *temp;

    for(i=0;times < 3;i++) {

    printf("Please input a valid UNIX command: ");
    /* I get seg. Fault on the following line */
    scanf("%s", command[i]);
    /* gets(*command[i]); */

    printf("You have successfully entered %d command(s) out of a possible /
    %d commands.\n");
    printf("Would you like to enter another command? (y/n): ");

    scanf("%c" ,&choice);

    if(choice == 'Y' || choice =='y') {
    times++;
    continue;
    }
    if(choice == 'N' || choice == 'n')
    break;
    else
    return 0;

    } /* End loop */

    for(i=0;times <= 3;i--) {

    strcpy(command[i], temp);

    for(j=0;!isspace(*command[j]);j++)
    temp[j] = *command[j];

    printf("%% %s\n", temp);
    system(command[i]);

    } /*End for loop*/

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    This is the easy way to do it!
    Code:
    #include <stdio.h>
    #include  <string.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
         int i;
         for (i = 0; i < argc, ++i)
                system(argv[i]);
     
         return 0;
    }
    Your code has a number of bugs. It uses gets and using scanf with %s and no
    field specifier is just as bad. You should use fgets.

    command[3] should be command[3][MAX_CMD_SIZE]

    If I'm correct the best way to do this is to avoid the 2D array with something
    like this

    Code:
    char command[MAX_CMD_SIZE];
    
    while(fgets(comand, sizeof command, stdin) != NULL) {
          printf("Enter command EOF to quit: ");
           fflush(stdout);
          system(command);
    }

  3. #3
    Registered User Gwargh's Avatar
    Join Date
    Aug 2001
    Posts
    7
    Brainfart... my seventh grade history teacher used that word...

  4. #4
    River21
    Guest
    Thanks for all the help guys, I ended up doing it with 2D char arrays, but it's done and it works and that's all that matters!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM