Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please Kindly Advice

8 views
Skip to first unread message

Diana Garison

unread,
Oct 21, 2001, 11:43:08 PM10/21/01
to
Have problem making the following C++ into a C equivalent. Bought an old C
reference book but still have no clue. Please kindly help.

int num;

while (!(cin >> num)) //Enter the loop if input
fails
{
cin.clear(); //Reset the stream's
status flags
cin.ignore(80, '\n'); //Skip to beginning of
next line, or 80 chars,
cout << "The number must be an integer. Try again\n";
}

Thank you very much in advance.
--
comp.lang.c.moderated - moderation address: cl...@plethora.net

Paul Braman

unread,
Oct 23, 2001, 2:59:42 PM10/23/01
to
"Diana Garison" <dgaris...@yahoo.com> wrote

> Have problem making the following C++ into a C equivalent. Bought an old C
> reference book but still have no clue. Please kindly help.
>
> int num;
>
> while (!(cin >> num)) //Enter the loop if input
> fails
> {
> cin.clear(); //Reset the stream's
> status flags
> cin.ignore(80, '\n'); //Skip to beginning of
> next line, or 80 chars,
> cout << "The number must be an integer. Try again\n";
> }
>
> Thank you very much in advance.

It's not exactly equivalent, but writing it makes me remember why I
hate (old) C programming. (Note, before C99.)


#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main () {
int value = 0;
int success = 0;
while (!success) {
char buffer[81];
char* ptr;
fputs ("enter a number: ", stdout);
if (!fgets (buffer, 80, stdin)) {
perror (strerror (errno));
exit (1);
}
for (ptr = buffer; *ptr; ++ptr) {
if (*ptr == '\n') {
if (sscanf (buffer, "%d", &value) == 1) {
success = 1;
break;
}
}
else if (isdigit (*ptr)) {
continue;
}
puts ("The number must be an integer. Try again");
}
}
printf ("value entered = %d\n", value);
return 0;
}


Paul Braman
Paul_Braman @ tvratings.com

CBFalconer

unread,
Oct 23, 2001, 2:59:46 PM10/23/01
to
Diana Garison wrote:
>
> Have problem making the following C++ into a C equivalent. Bought an old C
> reference book but still have no clue. Please kindly help.
>
> int num;
>
> while (!(cin >> num)) //Enter the loop if input
> fails
> {
> cin.clear(); //Reset the stream's
> status flags
> cin.ignore(80, '\n'); //Skip to beginning of
> next line, or 80 chars,
> cout << "The number must be an integer. Try again\n";
> }

unsigned int frduint(int *value, FILE *f)
{
int ch;

*value = 0;
while ((EOF != (ch = fgetc(f)) && (....) ) /* more */;
if (!isdigit(ch)) {
ungetc(ch, f);
return 0;
}
do {
ch = ch - '0';
*value = 10 * *value + ch;
ch = fgetc(f);
} while (isdigit(ch));
ungetc(ch, f);
return 1;
/* In all cases the final character that signalled termination of
the integer field is returned to the stream with ungetc(ch,f) for
use by other routines. */
} /* frduint */

void fflushln(FILE *f)
{
int ch;

WHILE ((EOF != (ch = fgetc(f)) && ('\n' != ch)) /* more */;
} /* fflushln */

....

while (!frduint(&num, stdin)) {
fflushln(stdin);
fputs("Bad input, try again\n", stdout);
}

C++ has effectively pre-written frduint and similar routines for
you. The above is untested code.

--
Chuck F (cbfal...@yahoo.com) (cbfal...@XXXXworldnet.att.net)
Available for consulting/temporary embedded and systems.
(Remove "XXXX" from reply address. yahoo works unmodified)
mailto:u...@ftc.gov (for spambots to harvest)

David Thompson

unread,
Nov 13, 2001, 1:25:44 AM11/13/01
to
Paul Braman <Paul_...@tvratings.com> wrote :
....

> It's not exactly equivalent, but writing it makes me remember why I
> hate (old) C programming. (Note, before C99.)
>
I don't see how any of the C99 changes would be relevant here.
Except maybe allowing you to declare char* ptr in the for header,
but that entire loop is unnecessary and not really right anyway.

>
> #include <ctype.h>
> #include <errno.h>
> #include <stdio.h>
> #include <string.h>
> int main () {
> int value = 0;
> int success = 0;
> while (!success) {
> char buffer[81];
> char* ptr;
> fputs ("enter a number: ", stdout);

The OP code did not print a prompt (except for error).
If you do want to do so, and don't end with a \n, need fflush.
C++ "ties" cin to cout but C does guarantee the equivalent.

> if (!fgets (buffer, 80, stdin)) {
> perror (strerror (errno));

This unnecessarily outputs strerror(errno) twice, because
perror() writes its argument if any followed by strerror(errno).
It's more common (and usually more useful) to put
some identification of the file in the perror() argument.

> exit (1);
> }
> for (ptr = buffer; *ptr; ++ptr) {
> if (*ptr == '\n') {
> if (sscanf (buffer, "%d", &value) == 1) {
> success = 1;
> break;
> }
> }
> else if (isdigit (*ptr)) {
> continue;
> }
> puts ("The number must be an integer. Try again");
> }
> }

This outputs the error message once for every nondigit character
in the input line, plus for the newline if (present and) not preceded
by a valid number. It thus gives error messages but proceeds to
accept the input if it has leading whitespace and/or sign, which
operator>>(,int&) would accept silently. If you want to accept
a number if present and leave anything following it, as OP code
does, you need scanf (or fscanf) (unless you want to reimplement
everything yourself, as CBFalconer does) and for error a getchar
(or equivalent) until newline loop. If you want to insist that
the input line (fit and) consist entirely of a number, why not:
while( !success ) {
if( ! fgets ... ) error;
if( sscanf(buffer, "%d %c",&value,&tempchar)==1
&& tempchar=='\n' ) success;
/* leave space out of format string to prohibit _trailing_ whitespace */
puts("try again");
}
(or any of the obvious variants like putting the fgets in the while).
(But this fails if a last (or only) incomplete line is input,
on implementations that support such.)

--
- David.Thompson 1 now at worldnet.att.net

Paul Braman

unread,
Nov 14, 2001, 4:16:04 PM11/14/01
to
"David Thompson" <david.t...@worldnet.att.net> wrote in message

> [clipped a bunch of corrections to my hastily written C code]

Yeah, what he said.


Paul Braman
Paul_Braman @ tvratings . com

0 new messages