getchar and character arrays

E

ehabaziz2001

CBFalconer said:
... snip ...

can I read '\n' using getchar() as I do in scanf ("\n") ?????

please suggested code :

void input (char *n)
////////////////////
{
int i=0;
printf ("\nInput :");
getchar()="\n";
do {
n=getchar();
i++;
} while (n!='\n');
}

void output (char *n)
////////////////////
{
int i=0;
printf ("\noutput :");
do {
printf ("%c" ,n);
i++;
} while (n!='\n');
}


Don't use // comments in usenet messages, they can easily be
wrapped and destroy the code. Also try to post complete compilable
programs, with proper indentation. Don't use tabs, because they
are often lost entirely.

'\n' is just another character. If you know that the input
contains an unread one you can flush it with a routine such as the
following:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f)) && ('\n' != ch)) continue;
return ch;
}

Calling this guarantees that any '\n' in the input has been
absorbed. It doesn't ensure that there was such to be absorbed.
Note that the input is stored in an int, so that EOF can be
detected. Also note that all input lines (on a buffered system,
which yours almost certainly is) terminate in a '\n'.

Now let us assume that you want to input lines not exceeding some
sort of maximum length, and are willing to discard any input longer
than that maximum. First you must define the maximum length
somewhere:

#define MAXLGH 80 /* just to pick a number out of the hat */

and somewhere you will have a buffer to hold the lines:

char buffer[MAXLGH];

now you want to call a routine to fill that buffer, and discard any
excess length:

void fillbuf(char *buf, int maxlgh); /* A prototype */

(which I haven't used - I combined it with something that prompts)

Putting things together you might end up with a program that looks
like:

#include <stdio.h>
#define MAXLGH 10

/* -------------- */

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

/* -------------- */

int getinput(FILE *f, char *buf, int lgh, char *prompt) {
int i;
int ch;

fputs(prompt, stdout);
fflush(stdout);
i = 0;
while ((i < lgh) && (EOF != (ch = getc(f))) && ('\n' != ch)) {
/* study the above condition carefully,
especially the order of tests.
Note that with lgh > 0 getc is always called */
buf[i++] = ch;
}
/* Now decide why the loop ended. */
if ('\n' != ch) ch = flushln(f);
/* This assumes that EOF is sticky. Don't worry about it */

/* Now we know that any final '\n' has been absorbed */

/* important - terminate the string */
buf = '\0';
return ch;
}

/* -------------- */

int main(void) {

char buffer[MAXLGH + 1]; /* +1 allows for terminating '/0' */
char prompt[] = "Input: ";

while (EOF != getinput(stdin, buffer, MAXLGH, prompt)) {
fputs("Output: ", stdout); /* no appended '\n' */
puts(buffer); /* which appends a '\n' and forces output */
}
return 0; /* main always returns a value */
}

Note that the program terminates on receiving an EOF signal. How
this is done depends on your system. Under linux that will
probably be a CTL-d key. Under MSDOS or Windows that will probably
be a CTL-z key. Entered immediately after the "Input:" prompt.

Lines terminate on receiving a '\n', generated by the Enter key.
Storage terminates either on line termination or receiving MAXLGH
characters.

Aside: It would be well to add a 'static' in the headers of the
functions (other than main), but this won't affect anything until
you get into more complicated multi-file programs.

getchar() is just shorthand for getc(stdin). Routines are more
flexible when you can aim them at arbitrary files. Keep them as
simple as possible.

Don't use scanf for interactive input. It will always leave
confusion. When and if you do use it always check its return
value.

Carefully read the descriptions of each standard function I have
called. This includes getc, getchar, puts, fputs, fflush. Note
that they all do simple things.

I appreciate your trail to give me a better code but still How can I
fix above code to make the loop like this :
{
...
na=getchar();
+ii;
...
}

and not like this
{
...
+ii;
na=getchar();
...
}

Thanks
 
E

ehabaziz2001

CBFalconer said:
... snip ...

can I read '\n' using getchar() as I do in scanf ("\n") ?????

please suggested code :

void input (char *n)
////////////////////
{
int i=0;
printf ("\nInput :");
getchar()="\n";
do {
n=getchar();
i++;
} while (n!='\n');
}

void output (char *n)
////////////////////
{
int i=0;
printf ("\noutput :");
do {
printf ("%c" ,n);
i++;
} while (n!='\n');
}


Don't use // comments in usenet messages, they can easily be
wrapped and destroy the code. Also try to post complete compilable
programs, with proper indentation. Don't use tabs, because they
are often lost entirely.

'\n' is just another character. If you know that the input
contains an unread one you can flush it with a routine such as the
following:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f)) && ('\n' != ch)) continue;
return ch;
}

Calling this guarantees that any '\n' in the input has been
absorbed. It doesn't ensure that there was such to be absorbed.
Note that the input is stored in an int, so that EOF can be
detected. Also note that all input lines (on a buffered system,
which yours almost certainly is) terminate in a '\n'.

Now let us assume that you want to input lines not exceeding some
sort of maximum length, and are willing to discard any input longer
than that maximum. First you must define the maximum length
somewhere:

#define MAXLGH 80 /* just to pick a number out of the hat */

and somewhere you will have a buffer to hold the lines:

char buffer[MAXLGH];

now you want to call a routine to fill that buffer, and discard any
excess length:

void fillbuf(char *buf, int maxlgh); /* A prototype */

(which I haven't used - I combined it with something that prompts)

Putting things together you might end up with a program that looks
like:

#include <stdio.h>
#define MAXLGH 10

/* -------------- */

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

/* -------------- */

int getinput(FILE *f, char *buf, int lgh, char *prompt) {
int i;
int ch;

fputs(prompt, stdout);
fflush(stdout);
i = 0;
while ((i < lgh) && (EOF != (ch = getc(f))) && ('\n' != ch)) {
/* study the above condition carefully,
especially the order of tests.
Note that with lgh > 0 getc is always called */
buf[i++] = ch;
}
/* Now decide why the loop ended. */
if ('\n' != ch) ch = flushln(f);
/* This assumes that EOF is sticky. Don't worry about it */

/* Now we know that any final '\n' has been absorbed */

/* important - terminate the string */
buf = '\0';
return ch;
}

/* -------------- */

int main(void) {

char buffer[MAXLGH + 1]; /* +1 allows for terminating '/0' */
char prompt[] = "Input: ";

while (EOF != getinput(stdin, buffer, MAXLGH, prompt)) {
fputs("Output: ", stdout); /* no appended '\n' */
puts(buffer); /* which appends a '\n' and forces output */
}
return 0; /* main always returns a value */
}

Note that the program terminates on receiving an EOF signal. How
this is done depends on your system. Under linux that will
probably be a CTL-d key. Under MSDOS or Windows that will probably
be a CTL-z key. Entered immediately after the "Input:" prompt.

Lines terminate on receiving a '\n', generated by the Enter key.
Storage terminates either on line termination or receiving MAXLGH
characters.

Aside: It would be well to add a 'static' in the headers of the
functions (other than main), but this won't affect anything until
you get into more complicated multi-file programs.

getchar() is just shorthand for getc(stdin). Routines are more
flexible when you can aim them at arbitrary files. Keep them as
simple as possible.

Don't use scanf for interactive input. It will always leave
confusion. When and if you do use it always check its return
value.

Carefully read the descriptions of each standard function I have
called. This includes getc, getchar, puts, fputs, fflush. Note
that they all do simple things.

I appreciate your trail to give me a better code but still How can I
fix above code to make the loop like this :
{
..
na=getchar();
+ii;
..
}

and not like this
{
..
+ii;
na=getchar();
..
}

Thanks


How can I begin my index with 0 and not 1 ?
How can I turn my module like this :

{
..
na=getchar();
+ii;
..
}
 
E

ehabaziz2001

CBFalconer said:
... snip ...

can I read '\n' using getchar() as I do in scanf ("\n") ?????

please suggested code :

void input (char *n)
////////////////////
{
int i=0;
printf ("\nInput :");
getchar()="\n";
do {
n=getchar();
i++;
} while (n!='\n');
}

void output (char *n)
////////////////////
{
int i=0;
printf ("\noutput :");
do {
printf ("%c" ,n);
i++;
} while (n!='\n');
}


Don't use // comments in usenet messages, they can easily be
wrapped and destroy the code. Also try to post complete compilable
programs, with proper indentation. Don't use tabs, because they
are often lost entirely.

'\n' is just another character. If you know that the input
contains an unread one you can flush it with a routine such as the
following:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f)) && ('\n' != ch)) continue;
return ch;
}

Calling this guarantees that any '\n' in the input has been
absorbed. It doesn't ensure that there was such to be absorbed.
Note that the input is stored in an int, so that EOF can be
detected. Also note that all input lines (on a buffered system,
which yours almost certainly is) terminate in a '\n'.

Now let us assume that you want to input lines not exceeding some
sort of maximum length, and are willing to discard any input longer
than that maximum. First you must define the maximum length
somewhere:

#define MAXLGH 80 /* just to pick a number out of the hat */

and somewhere you will have a buffer to hold the lines:

char buffer[MAXLGH];

now you want to call a routine to fill that buffer, and discard any
excess length:

void fillbuf(char *buf, int maxlgh); /* A prototype */

(which I haven't used - I combined it with something that prompts)

Putting things together you might end up with a program that looks
like:

#include <stdio.h>
#define MAXLGH 10

/* -------------- */

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

/* -------------- */

int getinput(FILE *f, char *buf, int lgh, char *prompt) {
int i;
int ch;

fputs(prompt, stdout);
fflush(stdout);
i = 0;
while ((i < lgh) && (EOF != (ch = getc(f))) && ('\n' != ch)) {
/* study the above condition carefully,
especially the order of tests.
Note that with lgh > 0 getc is always called */
buf[i++] = ch;
}
/* Now decide why the loop ended. */
if ('\n' != ch) ch = flushln(f);
/* This assumes that EOF is sticky. Don't worry about it */

/* Now we know that any final '\n' has been absorbed */

/* important - terminate the string */
buf = '\0';
return ch;
}

/* -------------- */

int main(void) {

char buffer[MAXLGH + 1]; /* +1 allows for terminating '/0' */
char prompt[] = "Input: ";

while (EOF != getinput(stdin, buffer, MAXLGH, prompt)) {
fputs("Output: ", stdout); /* no appended '\n' */
puts(buffer); /* which appends a '\n' and forces output */
}
return 0; /* main always returns a value */
}

Note that the program terminates on receiving an EOF signal. How
this is done depends on your system. Under linux that will
probably be a CTL-d key. Under MSDOS or Windows that will probably
be a CTL-z key. Entered immediately after the "Input:" prompt.

Lines terminate on receiving a '\n', generated by the Enter key.
Storage terminates either on line termination or receiving MAXLGH
characters.

Aside: It would be well to add a 'static' in the headers of the
functions (other than main), but this won't affect anything until
you get into more complicated multi-file programs.

getchar() is just shorthand for getc(stdin). Routines are more
flexible when you can aim them at arbitrary files. Keep them as
simple as possible.

Don't use scanf for interactive input. It will always leave
confusion. When and if you do use it always check its return
value.

Carefully read the descriptions of each standard function I have
called. This includes getc, getchar, puts, fputs, fflush. Note
that they all do simple things.

I appreciate your trail to give me a better code but still How can I
fix above code to make the loop like this :
{
..
na=getchar();
+ii;
..
}

and not like this
{
..
+ii;
na=getchar();
..
}

Thanks


What is the way of making the module of input like :
{
...
na=getchar();
+ii;
...


}
 
E

ehabaziz2001

CBFalconer said:
... snip ...

can I read '\n' using getchar() as I do in scanf ("\n") ?????

please suggested code :

void input (char *n)
////////////////////
{
int i=0;
printf ("\nInput :");
getchar()="\n";
do {
n=getchar();
i++;
} while (n!='\n');
}

void output (char *n)
////////////////////
{
int i=0;
printf ("\noutput :");
do {
printf ("%c" ,n);
i++;
} while (n!='\n');
}


Don't use // comments in usenet messages, they can easily be
wrapped and destroy the code. Also try to post complete compilable
programs, with proper indentation. Don't use tabs, because they
are often lost entirely.

'\n' is just another character. If you know that the input
contains an unread one you can flush it with a routine such as the
following:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f)) && ('\n' != ch)) continue;
return ch;
}

Calling this guarantees that any '\n' in the input has been
absorbed. It doesn't ensure that there was such to be absorbed.
Note that the input is stored in an int, so that EOF can be
detected. Also note that all input lines (on a buffered system,
which yours almost certainly is) terminate in a '\n'.

Now let us assume that you want to input lines not exceeding some
sort of maximum length, and are willing to discard any input longer
than that maximum. First you must define the maximum length
somewhere:

#define MAXLGH 80 /* just to pick a number out of the hat */

and somewhere you will have a buffer to hold the lines:

char buffer[MAXLGH];

now you want to call a routine to fill that buffer, and discard any
excess length:

void fillbuf(char *buf, int maxlgh); /* A prototype */

(which I haven't used - I combined it with something that prompts)

Putting things together you might end up with a program that looks
like:

#include <stdio.h>
#define MAXLGH 10

/* -------------- */

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

/* -------------- */

int getinput(FILE *f, char *buf, int lgh, char *prompt) {
int i;
int ch;

fputs(prompt, stdout);
fflush(stdout);
i = 0;
while ((i < lgh) && (EOF != (ch = getc(f))) && ('\n' != ch)) {
/* study the above condition carefully,
especially the order of tests.
Note that with lgh > 0 getc is always called */
buf[i++] = ch;
}
/* Now decide why the loop ended. */
if ('\n' != ch) ch = flushln(f);
/* This assumes that EOF is sticky. Don't worry about it */

/* Now we know that any final '\n' has been absorbed */

/* important - terminate the string */
buf = '\0';
return ch;
}

/* -------------- */

int main(void) {

char buffer[MAXLGH + 1]; /* +1 allows for terminating '/0' */
char prompt[] = "Input: ";

while (EOF != getinput(stdin, buffer, MAXLGH, prompt)) {
fputs("Output: ", stdout); /* no appended '\n' */
puts(buffer); /* which appends a '\n' and forces output */
}
return 0; /* main always returns a value */
}

Note that the program terminates on receiving an EOF signal. How
this is done depends on your system. Under linux that will
probably be a CTL-d key. Under MSDOS or Windows that will probably
be a CTL-z key. Entered immediately after the "Input:" prompt.

Lines terminate on receiving a '\n', generated by the Enter key.
Storage terminates either on line termination or receiving MAXLGH
characters.

Aside: It would be well to add a 'static' in the headers of the
functions (other than main), but this won't affect anything until
you get into more complicated multi-file programs.

getchar() is just shorthand for getc(stdin). Routines are more
flexible when you can aim them at arbitrary files. Keep them as
simple as possible.

Don't use scanf for interactive input. It will always leave
confusion. When and if you do use it always check its return
value.

Carefully read the descriptions of each standard function I have
called. This includes getc, getchar, puts, fputs, fflush. Note
that they all do simple things.

I appreciate your trail to give me a better code but still How can I
fix above code to make the loop like this :
{
..
na=getchar();
+ii;
..
}

and not like this
{
..
+ii;
na=getchar();
..
}

Thanks


How can I begin my index with 0 and not 1 ?
How can I turn my module like this :

{
..
na=getchar();
+ii;
..
}
 
E

ehabaziz2001

CBFalconer said:
... snip ...

can I read '\n' using getchar() as I do in scanf ("\n") ?????

please suggested code :

void input (char *n)
////////////////////
{
int i=0;
printf ("\nInput :");
getchar()="\n";
do {
n=getchar();
i++;
} while (n!='\n');
}

void output (char *n)
////////////////////
{
int i=0;
printf ("\noutput :");
do {
printf ("%c" ,n);
i++;
} while (n!='\n');
}


Don't use // comments in usenet messages, they can easily be
wrapped and destroy the code. Also try to post complete compilable
programs, with proper indentation. Don't use tabs, because they
are often lost entirely.

'\n' is just another character. If you know that the input
contains an unread one you can flush it with a routine such as the
following:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f)) && ('\n' != ch)) continue;
return ch;
}

Calling this guarantees that any '\n' in the input has been
absorbed. It doesn't ensure that there was such to be absorbed.
Note that the input is stored in an int, so that EOF can be
detected. Also note that all input lines (on a buffered system,
which yours almost certainly is) terminate in a '\n'.

Now let us assume that you want to input lines not exceeding some
sort of maximum length, and are willing to discard any input longer
than that maximum. First you must define the maximum length
somewhere:

#define MAXLGH 80 /* just to pick a number out of the hat */

and somewhere you will have a buffer to hold the lines:

char buffer[MAXLGH];

now you want to call a routine to fill that buffer, and discard any
excess length:

void fillbuf(char *buf, int maxlgh); /* A prototype */

(which I haven't used - I combined it with something that prompts)

Putting things together you might end up with a program that looks
like:

#include <stdio.h>
#define MAXLGH 10

/* -------------- */

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

/* -------------- */

int getinput(FILE *f, char *buf, int lgh, char *prompt) {
int i;
int ch;

fputs(prompt, stdout);
fflush(stdout);
i = 0;
while ((i < lgh) && (EOF != (ch = getc(f))) && ('\n' != ch)) {
/* study the above condition carefully,
especially the order of tests.
Note that with lgh > 0 getc is always called */
buf[i++] = ch;
}
/* Now decide why the loop ended. */
if ('\n' != ch) ch = flushln(f);
/* This assumes that EOF is sticky. Don't worry about it */

/* Now we know that any final '\n' has been absorbed */

/* important - terminate the string */
buf = '\0';
return ch;
}

/* -------------- */

int main(void) {

char buffer[MAXLGH + 1]; /* +1 allows for terminating '/0' */
char prompt[] = "Input: ";

while (EOF != getinput(stdin, buffer, MAXLGH, prompt)) {
fputs("Output: ", stdout); /* no appended '\n' */
puts(buffer); /* which appends a '\n' and forces output */
}
return 0; /* main always returns a value */
}

Note that the program terminates on receiving an EOF signal. How
this is done depends on your system. Under linux that will
probably be a CTL-d key. Under MSDOS or Windows that will probably
be a CTL-z key. Entered immediately after the "Input:" prompt.

Lines terminate on receiving a '\n', generated by the Enter key.
Storage terminates either on line termination or receiving MAXLGH
characters.

Aside: It would be well to add a 'static' in the headers of the
functions (other than main), but this won't affect anything until
you get into more complicated multi-file programs.

getchar() is just shorthand for getc(stdin). Routines are more
flexible when you can aim them at arbitrary files. Keep them as
simple as possible.

Don't use scanf for interactive input. It will always leave
confusion. When and if you do use it always check its return
value.

Carefully read the descriptions of each standard function I have
called. This includes getc, getchar, puts, fputs, fflush. Note
that they all do simple things.

I appreciate your trail to give me a better code but still How can I
fix above code to make the loop like this :
{
..
na=getchar();
+ii;
..
}

and not like this
{
..
+ii;
na=getchar();
..
}

Thanks


How can I begin my index with 0 and not 1 ?
How can I turn my module like this :

{
..
na=getchar();
+ii;
..
}
 
C

CBFalconer

.... snip 187 lines ...

How can I begin my index with 0 and not 1 ?
How can I turn my module like this :

Ignoring the advice you have received, and posting the same message
4 times in 70 minutes, will not gain you any friends here. I took
some trouble to give you a fairly comprehensive example, but will
not bother in future.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top