Valhalla Legends Forums Archive | C/C++ Programming | help with scanf

AuthorMessageTime
treyreese
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
February 19, 2004, 9:58 PM
iago
you should use getchar().

or scanf("%c", &thischar); in a loop might work, I'm not entirely sure how %c works
February 19, 2004, 10:50 PM
Eli_1
I believe you could just do

[code]
#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;
}

[/code]

[Edit] added \n
February 19, 2004, 10:52 PM
iago
[quote author=Eli_1 link=board=30;threadid=5369;start=0#msg45090 date=1077231147]
I believe you could just do

[code]
#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;
}

[/code]

[Edit] added \n
[/quote]

That can be overflowed and crash the program. I would use fgets() instead.
February 19, 2004, 11:05 PM
Eli_1
of course it could, but I was trying to stick to his question on how to use scanf() to do it... ;D
February 19, 2004, 11:08 PM
Skywing
Try %7s?
February 19, 2004, 11:09 PM
Eli_1
I didn't know you could do that... thanks, sky
February 19, 2004, 11:11 PM

Search