Suppose I wanted to input a seven digit binary number... how would i do it using scanf without having to put a space inbetween each digit
you should use getchar().
or scanf("%c", &thischar); in a loop might work, I'm not entirely sure how %c works
I believe you could just do
#include <stdio.h>
#include <string.h>
int main() {
char oooga[16];
scanf("%s", oooga);
if (strlen(oooga) != 7) {
printf("I wanted 7 digits bastard!\n");
return 1;
}
return 0;
}
[Edit] added \n
Quote from: Eli_1 on February 19, 2004, 04:52 PM
I believe you could just do
#include <stdio.h>
#include <string.h>
int main() {
char oooga[16];
scanf("%s", oooga);
if (strlen(oooga) != 7) {
printf("I wanted 7 digits bastard!\n");
return 1;
}
return 0;
}
[Edit] added \n
That can be overflowed and crash the program. I would use fgets() instead.
of course it could, but I was trying to stick to his question on how to use scanf() to do it... ;D
Try %7s?
I didn't know you could do that... thanks, sky