What's wrong with this code?

L

LL

#include <stdio.h>

#define ISDIGIT(x) \
switch(x) { \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9': \
return true; \
default: \
return false; \
}

bool isdigit(char x) {
ISDIGIT(x)
}

int main() {
char a;
for (;;) {
scanf("%c", &a);
if (a=='q') break;
if (isdigit(a))
printf("Is digit\n");
else
printf("Is not digit\n");
}
}

/*
Output:
a
Is not digit.
Is not digit.
1
Is digit
Is not digit

What's causing every second line to print Is not digit?
*/
 
I

Ian Collins

LL said:
#include <stdio.h>

#define ISDIGIT(x) \
switch(x) { \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9': \
return true; \
default: \
return false; \
}

bool isdigit(char x) {
ISDIGIT(x)
}

Why have you used an awful macro inside a function?
int main() {
char a;
for (;;) {
scanf("%c", &a);

You should check the return value from scanf.
if (a=='q') break;
if (isdigit(a))
printf("Is digit\n");
else printf("Is not digit\n");
}
}

/*
Output:
a
Is not digit.
Is not digit.
1
Is digit
Is not digit

What's causing every second line to print Is not digit?

Probably the linefeed character you type.
 
R

Roberto Divia

LL wrote:
[...]
int main() {
char a;
for (;;) {
scanf("%c", &a);
if (a=='q') break;
if (isdigit(a))
printf("Is digit\n");
else printf("Is not digit\n");
}
}

/*
Output:
a
Is not digit.
Is not digit.
1
Is digit
Is not digit

What's causing every second line to print Is not digit?

After the scanf, you must skip the rest of the line until you reach the end.
Print the value of "a" as an int and you will see the problem.

Few hints:
http://c-faq.com/stdio/scanfprobs.html
http://c-faq.com/stdio/stdinflush2.html

Ciao,
--
Roberto Divia` Love at first sight is one of the greatest
Dep:pH Bat:53 Mailbox:C02110 labour-saving devices the world has ever seen
Route de Meyrin 385 ---------------------------------------------
Case Postale Phone: +41-22-767-4994
CH-1211 Geneve 23 CERN Fax: +41-22-767-9585
Switzerland E-Mail: (e-mail address removed)
 
U

ugepp

LL said:
#include <stdio.h>

#define ISDIGIT(x) \
switch(x) { \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9': \
return true; \
default: \
return false; \
}

bool isdigit(char x) {
ISDIGIT(x)
}

int main() {
char a;
for (;;) {
scanf("%c", &a);
if (a=='q') break;
if (isdigit(a))
printf("Is digit\n");
else printf("Is not digit\n");
}
}

/*
Output:
a
Is not digit.
Is not digit.
1
Is digit
Is not digit

What's causing every second line to print Is not digit?
*/

consider this :

#include <stdio.h>

enum ft { false=0, true };
typedef unsigned bool;

#define ISDIGIT(x) \
switch(x) { \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9': \
return true; \
default: \
return false; \
}

bool my_isdigit(char x) {
ISDIGIT(x)
}

int main( void ) {
char a;
for (;;) {
scanf("%c", &a); /* this leaves a '\n' at buffer */
getchar(); /* this throws '\n' away */
if (a=='q') break;
if (my_isdigit(a))
printf("Is digit\n");
else printf("Is not digit\n");
}
return 0;
}

PS :
do you know that there is an "int isdigit(int c)" function in <ctype.h> ?
 
R

Richard Tobin

LL said:
scanf("%c", &a);
Output:
a
Is not digit.
Is not digit.
What's causing every second line to print Is not digit?

The linefeed you get by pressing return after each character.

Also, you need <stdbool.h> if you're using C99's booleans. You
shouldn't call your function isdigit, because C already uses that
name. And you should handle end-of-file properly instead of relying
on the user typing "q".

-- Richard
 
M

mohi

LL wrote:

[...]


int main() {
 char a;
 for (;;) {
  scanf("%c", &a);
  if (a=='q') break;
  if (isdigit(a))
   printf("Is digit\n");
  else
   printf("Is not digit\n");
 }
}
/*
Output:
a
Is not digit.
Is not digit.
1
Is digit
Is not digit
What's causing every second line to print Is not digit?
*/

This would have been a good opportunity for improving basic debugging
skills. You could have first reasoned that the loop was executing twice,
rather than just once as you expected. Then you could have inserted a
printf to find out what a was on the second iteration.

well just a query regarding the same :
won't scanf() skip '\n' if we use %d i mean like in the given code we
used
scanf("%d",&a) instead
would that be fine

Mohan
 
R

Richard Bos

pete said:
Hopefully, LL will read that and now know too.

I hope it won't bother to use that knowledge, but just use the Standard
isdigit() instead.

Richard
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top