Thread: Inputted values into an array

  1. #1
    Panther
    Guest

    Inputted values into an array

    I'm having a problem trying to convert a set of inputted values (integers) into an array. Ideally, the user is prompted to input a number (imagine inputting a dollar amount or a number using binary notation) and the number is stored in an array with the first number inputted as array[0]. An example would be:
    Please input binary notation: 10011001
    Then, processing will put the first '1' in array[0], '0' in array[1], '0' in array [2], ..., '1' in array[7]. The array size is known and will be size of 8 elements for binary notation or it could be set of a specific dollar amount.
    Sticking with the binary example, a user can input each value and then have it stored in an array using printf()/scanf() statements contained in a for loop. However, ideally, the program should take the value inputted for a question and then created the values in an array instead of asking the same question numerous times and increasing the likelyhood of wrong inputted values by the user. Any suggestions will be beneficial, thank you.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Why inputting integers? It would be much easier if you let the user input a number as a string and then access the individual characters, convert them to integer and finally store them in the array.

    If you still want to use integers, remember that you can use the modulo-operator, %, to get individual digits. Perform modulo N, where N is a multiple of 10 and you can get them.

  3. #3
    Panther
    Guest
    Why integers? For odd reasons in my code, the conversion from string to int changes the original value. If it is possible to perform the tasks as described above using strings, then the original question should be edited to try to find out how to convert an initial string value of integers to be stored in an array defined as int type while maintaining the original inputted values. Please help by either pointing/linking me to the right thread or any other resources to solve my problem. Thank you.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    How does the conversion change the original value?

    Conversion of a single character in a string to an integer value can be done as follows:

    int_value = character - '0';

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Use getche() in a for loop, ex: array[i] = getche(); while incrementing i in the for loop. The FAQ has something on this with its "getting a number from the user" try reading all those FAQ's they are very helpful. Here is a link
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Panther
    Guest
    Originally posted by Shiro
    If you still want to use integers, remember that you can use the modulo-operator, %, to get individual digits. Perform modulo N, where N is a multiple of 10 and you can get them.
    Please explain this concept in more detail. I'll work on the string conversion but I'm just curious about what is quoted. I know you can use % for output but did not know how to implement it for input [scanf()].

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I hope the following example will make it a little more clear of how to use the modulo-operator in this situation.

    Using scanf() you have retrieved an integer N, which you want to store in an array A. Then you could use the following algorithm:

    Code:
    // Determine the first digit
    M = N % 10
    
    // Store the first digit
    A [0] = M
    
    // Prepare N for the next iteration
    N = (N - M) / 10
    
    // Note: if N is now 0, this means that N is equal to M. And since
    // M is a single digit, this means N is also a single digit, so then we
    // have finished the number.
    
    // Now repeat this process until you have finished the number
    Example with N = 123.

    Calculate -> 123 % 10 = 3
    Store -> A [0] = 3
    Calculate -> (123 - 3) / 10 = 12

    Calculate -> 12 % 10 = 2
    Store -> A [1] = 2
    Calculate -> (12 - 2) / 10 = 1

    Calculate -> 1 % 10 = 1
    Store -> A [2] = 1
    Calculate -> (1 - 1) / 10 = 0

    Now we are finished.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Single line user inputted string to Array?
    By Sparrowhawk in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 08:10 PM
  2. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  3. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  4. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM