Thread: Loading numbers into array problem

  1. #1
    Registered User
    Join Date
    Dec 2022

    Unhappy Loading numbers into array problem

    Hello i am trying to make code which will request user for his phone number (on one line), that number will be stored in array number, everything works ok till i dont type less then 10 numbers and press enter, cycle is waiting for next number/s on new line but i want program to give message like: number is too short or too long , type that number again: i have tried many options with while cycle and do while but none of them works properly
    #define ARRAYSIZE(a)   (sizeof(a) / sizeof(a[0]))
    int main()
      int number[10];
       printf("type your number: \n");
       for (int i = 0; i < ARRAYSIZE(number); i++)
        scanf("%1d",&number[i]); //fe input: 033232313 is correct 
                //123 is incorrect user will be able to type numbers again 
    Thanks in advance for any help...
    Last edited by grepawk; 12-07-2022 at 11:35 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    You probably need to read the input line into a string first and then extract the digits from the string.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You can't really store phone numbers in integers.

    To begin with, integers do not store leading zeros.

    This is a big problem when you have international numbers like 004212345678

    Then some (actually a lot) of numbers have more digits than can be stored in a 32-bit integer.
    Phone Number Length by Country 2022 - World Populace

    To reliably deal with phone numbers, read them in as a string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a problem with random numbers in array
    By Man_Onaizah in forum C Programming
    Replies: 2
    Last Post: 05-05-2016, 08:08 AM
  2. problem store random numbers in array
    By Xavi Camps in forum C Programming
    Replies: 7
    Last Post: 01-13-2015, 12:03 AM
  3. Replies: 2
    Last Post: 01-29-2014, 05:17 PM
  4. Array problem averaging numbers
    By Thedon in forum C Programming
    Replies: 6
    Last Post: 10-27-2011, 11:27 AM
  5. problem with loading array of class objects
    By McElmurry in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2010, 03:34 AM

Tags for this Thread