problem with looping

This is a discussion on problem with looping within the C Programming forums, part of the General Programming Boards category; I'm preparing to make a very simple linux shell in c (one that will just run one program with any ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    Question problem with looping

    I'm preparing to make a very simple linux shell in c (one that will just run one program with any required arguments and perhaps later might search for the program in a path variable if its not found in the working directory).

    Anyway I thought since its currently well above my skill level in C I would learn to play with loops since thats pretty much what the shell will be doing wont it?


    #include <stdio.h>
    #define SENTINEL -1 // to be implemented later.

    int main()
    {
    char v[256];
    char command;
    do
    {
    fgets(v, 256, stdin);
    system(v);
    } while (fgets(v, 256, stdin) != NULL);
    }

    my intention is that if the user enters just '\n' without anything else or 'sentinel' is typed (-1) then the code will no longer loop otherwise it will indefinently run commands for the user I've tried heaps but cant seem to make anything work..

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: problem with looping

    Originally posted by razza
    my intention is that if the user enters just '\n' without anything else
    So you need to check for a \n at the beginning of your buffer.
    Code:
    #include <stdio.h> 
    #define SENTINEL -1 // to be implemented later. 
    
    int main() 
    { 
    	char v[256]; 
    	while(fgets(v, 256, stdin) != NULL && v[0] != '\n')
    	{ 
    		system(v); 
    	}
    }
    Cheers,
    Monster

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    thanks buddie!

    I really appreciate the help.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    You may need to remove the \n from the buffer before passing it to system()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21