c program - command line prompt - need help

Discussion in 'C Programming' started by raeiko, Mar 1, 2009.

  1. raeiko

    raeiko Guest

    Hello guys,

    i urgently need your help

    I have to write a C program that reads a series of strings and prints
    only those that end with the letters “ed.” To enter the string i
    should use the command line prompt and when i press the return key,
    the program should print the string if it ends in "ed" and prompt for
    another string if it doesn't.

    My problem is that, even after reading from Dietel book and searching
    in internet, I didn't get how to use the command line prompt...

    Could you be so kind to give me some hints to start from?

    Thanks in advance for your help,
     
    raeiko, Mar 1, 2009
    #1
    1. Advertising

  2. On 1 Mar, 23:07, raeiko <> wrote:
    > Hello guys,
    >
    > i urgently need your help


    Left homework for the last moment , have we ?

    > I have to write a C program that reads a series of strings and prints
    > only those that end with the letters “ed.” To enter the string i
    > should use the command line prompt and when i press the return key,
    > the program should print the string if it ends in "ed" and prompt for
    > another string if it doesn't.
    >
    > My problem is that, even after reading from Dietel book and searching
    > in internet, I didn't get how to use the command line prompt...
    >
    > Could you be so kind to give me some hints to start from?


    The right place to start is to describe the problem exactly. The
    problem here is that it is unclear what you mean by "command
    line prompt". The C standard doesn't say anything about a
    "command line prompt" nor have I seen anywhere a description
    of C which mentions a "command line prompt". It's not clear
    whether you simply mean reading from stdin or whether you
    are using some sort of specialised environment where C comes
    with a "command line prompt".

    Do you know how to do everything else and simply have trouble
    with this prompt thingy ?
     
    Spiros Bousbouras, Mar 1, 2009
    #2
    1. Advertising

  3. raeiko

    Curtis Dyer Guest

    raeiko <> wrote:
    > Hello guys,
    >
    > i urgently need your help
    >
    > I have to write a C program that reads a series of strings and prints
    > only those that end with the letters “ed.†To enter the string i
    > should use the command line prompt and when i press the return key,
    > the program should print the string if it ends in "ed" and prompt for
    > another string if it doesn't.
    >
    > My problem is that, even after reading from Dietel book and searching
    > in internet, I didn't get how to use the command line prompt...
    >
    > Could you be so kind to give me some hints to start from?


    What have you tried so far?

    > Thanks in advance for your help,


    One option might be using the `fgets()' function in a loop to read
    from `stdin', one line at a time. You could then examine the data
    for the current line in your loop, and make a decision as to what you
    want to do.

    Just a bit of a disclaimer, I'm still in the process of learning C as
    well.

    --
    Curtis
    s/sig\.invalid/gmail.com/;
     
    Curtis Dyer, Mar 2, 2009
    #3
  4. raeiko

    Curtis Dyer Guest

    Richard Heathfield <> wrote:
    > raeiko said:
    >
    > > Hello guys,
    > >
    > > i urgently need your help
    > >
    > > I have to write a C program that reads a series of strings and
    > > prints only those that end with the letters ?ed.? To enter the
    > > string i should use the command line prompt and when i press the
    > > return key, the program should print the string if it ends in "ed"
    > > and prompt for another string if it doesn't.
    > >
    > > My problem is that, even after reading from Dietel book and
    > > searching in internet, I didn't get how to use the command line
    > > prompt...

    >
    > You seem to be talking about what are usually known as command line
    > arguments.
    >
    > > Could you be so kind to give me some hints to start from?

    >
    > I think I see what you mean about Deitel and Deitel's explanation.
    >
    > Okay, here's the basic idea:
    >
    > We start off by using the other form of main(). You are probably
    > accustomed to int main(void), but to gain access to command line
    > arguments we need to use this instead:


    Perhaps I'm misreading the OP's post still, but it seems like they
    want something like getting input from STDIN interactively via a
    shell terminal. Despite the OP mentioning not understanding how to
    use "the command line prompt", their spec seems doesn't seem to
    indicate the need for command line arguments.

    This is an excerpt from the OP's spec:

    Message-ID: <c45530e9-d711-4101-9458-
    >
    ... when i press the return key, the program should print the
    string if it ends in "ed" and prompt for another string if it
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    > doesn't.


    Sorry if I'm missing something painfully obvious. :-/

    <snip>

    --
    Curtis
    s/sig\.invalid/gmail.com/;
     
    Curtis Dyer, Mar 2, 2009
    #4
  5. raeiko

    Phil Carmody Guest

    Richard Heathfield <> writes:
    > raeiko said:
    >
    >> Hello guys,
    >>
    >> i urgently need your help
    >>
    >> I have to write a C program that reads a series of strings and
    >> prints only those that end with the letters ?ed.? To enter the
    >> string i should use the command line prompt and when i press the
    >> return key, the program should print the string if it ends in "ed"
    >> and prompt for another string if it doesn't.
    >>
    >> My problem is that, even after reading from Dietel book and
    >> searching in internet, I didn't get how to use the command line
    >> prompt...

    >
    > You seem to be talking about what are usually known as command line
    > arguments.


    "...and prompt for another string..." sounds more like stdin to me.
    An array of char, and an fgets() call seems to be the order of the day.

    Phil
    --
    I tried the Vista speech recognition by running the tutorial. I was
    amazed, it was awesome, recognised every word I said. Then I said the
    wrong word ... and it typed the right one. It was actually just
    detecting a sound and printing the expected word! -- pbhj on /.
     
    Phil Carmody, Mar 2, 2009
    #5
  6. raeiko

    Richard Guest

    Richard Heathfield <> writes:

    > Curtis Dyer said:
    >
    > <snip>
    >
    >> Perhaps I'm misreading the OP's post still, but it seems like they
    >> want something like getting input from STDIN interactively via a
    >> shell terminal. Despite the OP mentioning not understanding how to
    >> use "the command line prompt", their spec seems doesn't seem to
    >> indicate the need for command line arguments.
    >>
    >> This is an excerpt from the OP's spec:
    >>
    >> Message-ID: <c45530e9-d711-4101-9458-
    >> >
    >> ... when i press the return key, the program should print the
    >> string if it ends in "ed" and prompt for another string if it
    >> ^^^^^^^^^^^^^^^^^^^^^^^^^
    >> > doesn't.

    >>
    >> Sorry if I'm missing something painfully obvious. :-/

    >
    > Perhaps we both did. You're right to point out my omission, but now
    > I'm wondering whether he needs /both/ - command-line processing
    > /and/ standard input processing. (Alternatively, you're right, I'm
    > wrong, and I just chased him up a blind alley.)


    Why would you wonder that. Requirements analysis is simple enough in this
    case because he *specifically* states:

    ,----
    | >> ... when i press the return key, the program should print the
    | >> string if it ends in "ed" and prompt for another string if it
    `----

    It could not be clearer.
     
    Richard, Mar 2, 2009
    #6
  7. raeiko

    Guest

    On 1 Mar, 23:07, raeiko <> wrote:

    > i urgently need your help


    :-(

    > I have to write a C program that reads a series of strings and prints
    > only those that end with the letters “ed.” To enter the string i
    > should use the command line prompt and when i press the return key,
    > the program should print the string if it ends in "ed" and prompt for
    > another string if it doesn't.


    I think other posters have seriously misunderstood you.
    This is plainly an interactive program (no command line arguments
    needed)

    > My problem is that, even after reading from Dietel book and searching
    > in internet, I didn't get how to use the command line prompt...
    >
    > Could you be so kind to give me some hints to start from?
    >
    > Thanks in advance for your help,


    does this help?

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

    int main (void)
    {
    for (;;)
    {
    char line [1024];
    char *nl;

    printf ("enter a string: ");
    fflush (stdout); /* ensure prompt appears */

    /* read a string, halt if get end-of-file */
    if (fgets (line, sizeof line, stdin) == NULL)
    return 0;

    /* halt when get an empty string */
    if (line[0] == '\n')
    return 0;

    /* remove end-of-line */
    if ((nl = strchr (line, '\n')) != NULL)
    *nl = '\0';

    printf ("the string you entered was \"%s\"\n", line);
    }

    return 0;
    }
     
    , Mar 2, 2009
    #7
  8. raeiko

    Guest

    On 2 Mar, 00:48, Curtis Dyer <> wrote:
    > raeiko <> wrote:
    > > Hello guys,

    >
    > > i urgently need your help

    >
    > > I have to write a C program that reads a series of strings and prints
    > > only those that end with the letters “ed.” To enter the string i
    > > should use the command line prompt and when i press the return key,
    > > the program should print the string if it ends in "ed" and prompt for
    > > another string if it doesn't.

    >
    > > My problem is that, even after reading from Dietel book and searching
    > > in internet, I didn't get how to use the command line prompt...

    >
    > > Could you be so kind to give me some hints to start from?

    >
    > What have you tried so far?
    >
    > > Thanks in advance for your help,

    >
    > One option might be using the `fgets()' function in a loop to read
    > from `stdin', one line at a time.  You could then examine the data
    > for the current line in your loop, and make a decision as to what you
    > want to do.
    >
    > Just a bit of a disclaimer, I'm still in the process of learning C as
    > well.


    nevertheless you were correct
     
    , Mar 2, 2009
    #8
  9. raeiko

    Richard Guest

    writes:

    > On 1 Mar, 23:07, raeiko <> wrote:
    >
    >> i urgently need your help

    >
    > :-(
    >
    >> I have to write a C program that reads a series of strings and prints
    >> only those that end with the letters “ed.†To enter the string i
    >> should use the command line prompt and when i press the return key,
    >> the program should print the string if it ends in "ed" and prompt for
    >> another string if it doesn't.

    >
    > I think other posters have seriously misunderstood you.
    > This is plainly an interactive program (no command line arguments
    > needed)


    One other poster.
     
    Richard, Mar 2, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chrisdude911
    Replies:
    10
    Views:
    135,624
    srk289
    Mar 10, 2009
  2. Endless Story
    Replies:
    9
    Views:
    636
    Endless Story
    Feb 16, 2007
  3. jacob navia

    Re: c program - command line prompt - need help

    jacob navia, Mar 2, 2009, in forum: C Programming
    Replies:
    29
    Views:
    826
    CBFalconer
    Mar 5, 2009
  4. Ulf Meinhardt
    Replies:
    1
    Views:
    1,004
    Andrew Thompson
    Aug 10, 2009
  5. Ulf Meinhardt
    Replies:
    6
    Views:
    935
    Martin Gregorie
    Aug 25, 2009
Loading...

Share This Page