Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: treyreese on February 19, 2004, 03:58 PM

Title: help with scanf
Post by: treyreese on February 19, 2004, 03:58 PM
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
Title: Re:help with scanf
Post by: iago on February 19, 2004, 04:50 PM
you should use getchar().

or scanf("%c", &thischar); in a loop might work, I'm not entirely sure how %c works
Title: Re:help with scanf
Post by: 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
Title: Re:help with scanf
Post by: iago on February 19, 2004, 05:05 PM
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.
Title: Re:help with scanf
Post by: Eli_1 on February 19, 2004, 05:08 PM
of course it could, but I was trying to stick to his question on how to use scanf() to do it... ;D
Title: Re:help with scanf
Post by: Skywing on February 19, 2004, 05:09 PM
Try %7s?
Title: Re:help with scanf
Post by: Eli_1 on February 19, 2004, 05:11 PM
I didn't know you could do that... thanks, sky