Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inputting a 6 digit number into an array

How do I ask for an input (ex:123456) into an integer array so that it is 1,2,3,4,5,6 in the array without using any punctuations or spaces?

int main(void)
{
    int input[7] = {0};

    printf("");
    scanf("%d%d%d%d%d%d", &input);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

scanf() can’t read directly into an array (except for strings), it requires a separate argument for each value you’re reading.

And to read each digit separately without any delimiters, you have to specify the field size in the format string.

scanf("%1d%1d%1d%1d%1d%1d", &input[0], &input[1], &input[2], &input[3], &input[4], &input[5]);

You can also use a loop:

for (int i = 0; i < 6; i++) {
    scanf("%1d", &input[i]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading