storing integers in an array

This is a discussion on storing integers in an array within the C Programming forums, part of the General Programming Boards category; i ma currently writing a program that converts numbers to words. but this part stores the characters in an array ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    194

    Talking storing integers in an array

    i ma currently writing a program that converts numbers to words. but this part stores the characters in an array of nine elements. this code is giving me heat..... help me out with this

    Code:
    //read_int: This function reads the integers from input checks and stores them in an array.
    int read_int(int num[], int n)
    {
        int ch;
    
        i = 0;
        printf("\nEnter number Here: ");
    
        while( (ch = getchar())!= '\n')
        {
    
            if ( ch == 'Q' || ch == 'q')
            {
                printf("\nExiting....");
                printf("\nThanks for using our software; we would like to receive your comments and suggestions to better our services.");
                printf("\nINK Corp. 2012.");
                exit(EXIT_SUCCESS);
            }
    
            if ( i < n)
                num[i++] = ch;
        }
        printf("\n");
        putchar(i);
    
        return i;
    }\

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Can you describe what you expect to happen and how what actually happens differ from that.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    194
    i expect each integer entered to be stored in the array and then print the total number of integers stored to check whether it collected all the characters. i was thinking of including a printf statement to check whether each character was stored as an element in the array.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    194
    but this function collects some characters and prints some weird characters i did not enter.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Nyah Check View Post
    but this function collects some characters and prints some weird characters i did not enter.
    I assume you have declared 'i' in your real function or it's global.

    But try to replace:

    Code:
    putchar(i);
    
    //with
    
    printf("%d\n", i);
    "i" expressed as a character will not be a printable character for the values you are targeting (below 9) and either way it won't print the amount of characters you have added to the array.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    194
    i is a global variable any it works now there was the problem with puchar() so i used printf.... so its working its the next part that is the headache.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Nyah Check View Post
    i is a global variable any it works now there was the problem with puchar() so i used printf.... so its working its the next part that is the headache.
    What next part? Your program does what you have described so far. It's not clear if you want the array to keep integers or characters, at the moment you store characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing and printing large integers
    By newbie123 in forum C Programming
    Replies: 8
    Last Post: 09-16-2010, 02:17 PM
  2. storing integers from a text file into a 1d array
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 09-30-2008, 10:04 PM
  3. Storing array of integers as keys
    By vsla in forum C Programming
    Replies: 2
    Last Post: 05-08-2008, 01:53 PM
  4. Storing integers from file into arrays???? please help
    By adzza69 in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2004, 01:28 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 02:17 AM

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