Thread: array of ints

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    array of ints

    I have these codes:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    int main()
    {
      int arrayofints2[5];
      int i;
      
      
      printf("Enter five numbers\t");
      
      for (i=0; i<5; i++){
           scanf("%d", &arrayofints2[i]);
           }
      for (i=0; i<5; i++){
          printf("arrayofints2[%d]has a value of %d\n",i,arrayofints2[i]);
          }
      getch();	
      return 0;
    }
    I don't understand why i have to push enter every time i enter number? When I try to enter numbers horizontally with spaces i've got error message that program performed illegal operation.

    Thanks.

  2. #2
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    Others may yell at you, so take out conio.h and replace getch() with getchar(). With scanf you have to press enter after input. That's just the way it is. If you wanted to enter a number with more than one digit that enter would mean the difference between workage and not-workage.

    If you are entering single digit numbers you can use getchar() instead of scanf().
    Last edited by VOX; 12-31-2004 at 07:52 PM.
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real reason is because you're telling scanf to read one at a time.

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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I don't understand why i have to push enter every time i enter number?
    You don't need to - code works fine for me (after removing the DOS-isms)

    Example
    Code:
    $ ./a.out
    Enter five numbers      1 2 3 4 5
    arrayofints2[0]has a value of 1
    arrayofints2[1]has a value of 2
    arrayofints2[2]has a value of 3
    arrayofints2[3]has a value of 4
    arrayofints2[4]has a value of 5
    $ ./a.out
    Enter five numbers      1 2 3
    4 5
    arrayofints2[0]has a value of 1
    arrayofints2[1]has a value of 2
    arrayofints2[2]has a value of 3
    arrayofints2[3]has a value of 4
    arrayofints2[4]has a value of 5
    $ ./a.out
    Enter five numbers      1
    2 3 4
    5
    arrayofints2[0]has a value of 1
    arrayofints2[1]has a value of 2
    arrayofints2[2]has a value of 3
    arrayofints2[3]has a value of 4
    arrayofints2[4]has a value of 5
    Whether they're all on one line or split across several lines should not matter.

    However, your code will break if there is anything other than spaces, newlines or digits.

  5. #5
    Quote Originally Posted by barim
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    int main()
    {
      int arrayofints2[5];
      int i;
      
      printf("Enter five numbers\t");
      
      for (i=0; i<5; i++){
           scanf("%d", &arrayofints2[i]);
           }
      for (i=0; i<5; i++){
          printf("arrayofints2[%d]has a value of %d\n",i,arrayofints2[i]);
          }
      getch();	
      return 0;
    }
    I don't understand why i have to push enter every time i enter number? When I try to enter numbers horizontally with spaces i've got error message that program performed illegal operation.
    Sounds good to me:
    Code:
    Enter five numbers      1 2 3 4 5
    arrayofints2[0]has a value of 1
    arrayofints2[1]has a value of 2
    arrayofints2[2]has a value of 3
    arrayofints2[3]has a value of 4
    arrayofints2[4]has a value of 5
    That said, I would not have mixed input functions from stdio and conio in the same program. It's an open door to hell...

    Also, the 'prompt' not being terminated by a '\n', I suggest to add a
    Code:
       fflush(stdout);
    just after it.

    Finally, scanf() is a tricky function that should not be used by newbies (actually only a bunch of gurus know exactly how to use it). Better to stick to fgets() to get the line and strtol() or the like to convert the string into a number.

    At minimum, you should test the value returned by scanf(). In your case, a value that is not 1 denotes an input error. (Details in your C-book)
    Last edited by Emmanuel Delaha; 01-01-2005 at 05:24 AM. Reason: details added
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading ints specified in a string into an array
    By Curtster in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 02:19 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Replies: 4
    Last Post: 11-18-2001, 07:29 PM