user3661531
user3661531

Reputation: 175

How to scanf each number into an array slot?

Im trying to scan each single number into each into an array slot. So the first digit will go to slot 1 and the second would go to slot 2 etx. How do I do this? With the code below all of the numbers get stored in slot 1. I want to enter all the numbers togther, no 1 number at a time. Please can someone tell me how to do this?

#include <stdio.h>

int main()
{
    int lotoNumbers[7];

    printf("Please enter todays winning ticket number:");
    scanf("%d%d%d%d%d%d%d", &lotoNumbers[1],
                            &lotoNumbers[2],
                            &lotoNumbers[3],
                            &lotoNumbers[4],
                            &lotoNumbers[5],
                            &lotoNumbers[6],
                            &lotoNumbers[7]);

    printf("Your loto ticket number is: %d%d%d%d%d%d%d",
                lotoNumbers[1],
                lotoNumbers[2],
                lotoNumbers[3],
                lotoNumbers[4],
                lotoNumbers[5],
                lotoNumbers[6],
                lotoNumbers[7]);

    return(0);
}

Upvotes: 0

Views: 530

Answers (4)

vmp
vmp

Reputation: 2420

This code below will let you get all at a time separated by spaces... Also, your code start counting from 1 up to N, you must go from 0 up to N - 1

#include <stdio.h>



int main()
{

    int lotoNumbers[7];


    printf("Please enter todays winning ticket number:");
    scanf("%d %d %d %d %d %d %d",&lotoNumbers[0],&lotoNumbers[1],&lotoNumbers[2],&lotoNumbers[3],&lotoNumbers[4],&lotoNumbers[5],&lotoNumbers[6]);
// scanf("%d.%d.%d.%d.%d.%d.%d",&lotoNumbers[0],&lotoNumbers[1],&lotoNumbers[2],&lotoNumbers[3],&lotoNumbers[4],&lotoNumbers[5],&lotoNumbers[6]);

    printf("Your loto ticket number is: %d %d %d %d %d %d %d", lotoNumbers[0], lotoNumbers[1], lotoNumbers[2], lotoNumbers[3], lotoNumbers[4], lotoNumbers[5], lotoNumbers[6]);

return(0);
}

Look at the commented scanf... it will work if you enter the digits separated by a . :

    312.832.3278.3217.3123.7812.8

but if you glue all %d's together how will it know if the number 145 is just a single number or 1, 4, 5 or 14, 5 or 1, 45...

Upvotes: 2

chux
chux

Reputation: 153338

Use "%1d". This limits the width of the scanning to 1 char.

int lotoNumbers[7];
if (scanf("%1d%1d%1d%1d%1d%1d%1d", &lotoNumbers[0], &lotoNumbers[1],
    &lotoNumbers[2], &lotoNumbers[3], &lotoNumbers[4], &lotoNumbers[5],
    &lotoNumbers[6]) == 7) {
  GoodToGo();
}

Check scanf() results and start indexing at 0.

Upvotes: 1

Surinder ツ
Surinder ツ

Reputation: 1788

Try this:

#include <stdio.h>
int main()
{
    int lotoNumbers[7];
    printf("Please enter todays winning ticket number:");
    scanf("%d%d%d%d%d%d%d",&lotoNumbers[0],&lotoNumbers[1],&lotoNumbers[2],&lotoNumbers[3],&lotoNumbers[4],&lotoNumbers[5],&lotoNumbers[6]);

    printf("Your loto ticket number is:\n");
    for(int i=0;i<7;i++)
    {
        printf("%d\n",lotoNumbers[i]);
    }
    return(0);
}

Upvotes: 0

Ronn Wilder
Ronn Wilder

Reputation: 1368

instead of scanf, use getchar() function to get single char. getchar() or getch() function do not require to press ENTER after input. here's the code -

int loto[7];

int i;
char c;

for(i=0;i<7;i++){

    c=getchar();

    int temp=c-'0';

    loto[i]=temp;    
}

Upvotes: 0

Related Questions