Thread: Breaking an integer stored in an array into two integers

  1. #1
    Registered User
    Join Date
    Feb 2012

    Breaking an integer stored in an array into two integers

    Ok, so I understand the basics on how to split a number into two that's not my problem. A user can enter an integer and it is stored in an array. I need to take the integer from the array and split it into two halves (opCode the 1st half, memLocation the 2nd half). I put a printf statement in to track that the integer is actually being split and stored properly, and my output every time no matter what number I enter is "0 1".. I was thinking that this was maybe a pointer problem, but even when I print the pointers for these two variables they still have the same address for each number entered. I need these to be split so I can call a switch statement using the 1st half of the numbers and the 2nd half is the value that I will be passing.

    I hope this makes sense, I wasn't trying to be super wordy.. Thank You!

    Here's the part of my code that is relevant to this problem I am having:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        int memory[100];
        int opCode = 0; /* variable to store first 2 digits*/
        int memLocation = 0; /* variable to store end 2 digits*/
        int selection = 0; /* what user will be entering into the program (ex. 1234)*/
        int iCount = 0; /* Counter for the number of instructions entered */
        /* -99999 is the escape*/
        while (selection !=-99999){
            printf("%.2d ? ", iCount);
            memory[iCount] = scanf("%d", &selection);
            opCode = memory[iCount] / 100;
            memLocation = memory[iCount] % 100;
            printf("%d\t%d\n", opCode, memLocation); /* Using this to track if the number is actually being split properly or not*/
        return 0;

  2. #2
    Registered User
    Join Date
    Feb 2012
    Wow nevermind figured it out... I guess that's why they invented the idea of using a temp variable.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    I've never seen "%.2d" used as a format specifier (I'm surprised it works). Usually people use "%02d" to pad with zeroes.

    However, your problem is that you're putting the return value of scanf into memory. scanf's return value is the number of values scanned, which is always 1 in your case. You want:
    scanf("%d", &memory[iCount]);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Feb 2012
    Yeah that may be the java programming in me but I've always had it work no problem. But thank you for the suggestion that makes so much more sense now that way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concating integers to form an integer
    By Imanuel in forum C Programming
    Replies: 6
    Last Post: 12-07-2010, 07:12 PM
  2. how integer stored in binary format in 'c' language
    By pankaj_kumar in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 08:26 AM
  3. breaking up the array
    By teves16 in forum C Programming
    Replies: 3
    Last Post: 08-23-2006, 08:33 PM
  4. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM
  5. Can yoou return a range of values stored into a integer?
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 04:24 PM