c program - command line prompt - need help

R

raeiko

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,
 
S

Spiros Bousbouras

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 ?
 
C

Curtis Dyer

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...

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.
 
C

Curtis Dyer

Richard Heathfield said:
raeiko said:


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


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-
(e-mail address removed)>
... 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>
 
P

Phil Carmody

Richard Heathfield said:
raeiko said:


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
 
R

Richard

Richard Heathfield said:
Curtis Dyer said:



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.
 
G

Guest

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;
}
 
G

Guest

What have you tried so far?


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top