• Welcome to Valhalla Legends Archive.
 

help with scanf

Started by treyreese, February 19, 2004, 03:58 PM

Previous topic - Next topic

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

iago

you should use getchar().

or scanf("%c", &thischar); in a loop might work, I'm not entirely sure how %c works
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Eli_1

#2
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

iago

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Eli_1

of course it could, but I was trying to stick to his question on how to use scanf() to do it... ;D

Skywing


Eli_1

I didn't know you could do that... thanks, sky