array help

This is a discussion on array help within the C Programming forums, part of the General Programming Boards category; ok i need to write a programme that asks the user to input student numbers as an array and check ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    array help

    ok i need to write a programme that asks the user to input student numbers as an array and check the initial number and allocate them into different classrooms.. my code looks lik ths:


    #include <stdio.h>

    int number;
    int i;
    int student_number[10];
    int total;
    int first[0];

    int main (void)
    {


    printf("Enter the total number of students ");
    scanf("%d", &total);


    for (i=1; i<=total; i++)
    {
    printf("Enter the student number\n: ");
    scanf("%d", &student_number);
    printf("first digit is %d\n", student_number[0]);

    if (student_number[0] < 5)
    {
    printf(" Classroom A\n");
    }
    else if (student_number[0] >5)
    {
    printf(" Classroom B\n");
    }



    }

    system ("pause");
    return 0;
    }

    the code only works if there is a space between the digits, ie. if input is 23 3, i get the first digit as 23. what should i do so that it extracts the first digit from the number with no spaces in between?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    int a, b;
    scanf("%1d%1d",&a,&b);
    %1d puts a single digit in.

    You may want to read this, btw:

    STDIN pitfalls
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    thanks but is there a way i can pass it into an array and extract the first digit only?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ancientdragon View Post
    thanks but is there a way i can pass it into an array and extract the first digit only?
    Code:
    int array[10], i;
    for (i=0; i<10; i++) scanf("%1d",&array[i]);
    So if you type:
    9413823144
    each digit will be a separate element in the array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ancientdragon View Post
    thanks but is there a way i can pass it into an array and extract the first digit only?
    Read it as a string, compare the spot you want with isdigit.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 10:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21