Reading numbers from console

This is a discussion on Reading numbers from console within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm new to the forum. I'm new to C and have only ever programmed in Java, therefore having ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Reading numbers from console

    Hi guys, I'm new to the forum.

    I'm new to C and have only ever programmed in Java, therefore having a bit of problems at the moment.
    I'm trying to write a program that reads ten numbers from the keyboard, stores
    them in an array of integers and then prints out the numbers in the reverse order.

    I have done this so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
    	int arr[10];
    	int BUFFSIZE = 10;
    	char buffer[BUFFSIZE];
    
    	int i=0;
    	int *pa;
    
    	int z=9;
    	int a;
    
    	while (i<10){
    
    	printf("Enter an integer:\n");
    	fgets(buffer,BUFFSIZE,stdin);
    
    	if ( feof(stdin)){
    		printf("Finished\n");
    		break;
    	}
    	arr[0]= atoi(buffer);
    
    	i++;
    	}
    
    
    	while(z>=0){
    		pa=&arr[z];
    		a=*pa;
    		printf(a);
    		z--;
    	}
    
    }
    I do not really understand where i am going wrong (maybe because i might not fully understand pointers yet). Also I do not understand why the feof function is needed in this case.

    Any help would be really appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    simple code to demonstrate an array of integers

    Code:
    #include <stdio.h>
    
    int main() {
      int arr[10] = {0};
      int index = 0;
    
      for (; index < 10; ++index) {
        printf("Please enter %d number\n", index + 1);
        scanf("%d", &arr[index]);
      }
    
      // Display numbers                                                                                                                                                                                              
      index = 0;
      for (; index < 10; ++index)
        printf("%d  ", arr[index]);
    
      return 0;
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gerrard View Post
    Also I do not understand why the feof function is needed in this case.
    Neither do I. That condition will never be met, so this is meaningless:
    Code:
    	if ( feof(stdin)){
    		printf("Finished\n");
    		break;
    	}
    If you want to check the user's input (to see if they just pressed enter with no number) you need to examine what's in "buffer".

    Your problem is a malformed printf. Look at RockM's example.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Thanks everyone, I have managed to sort it out now. Really much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 03:21 AM
  4. character strings and reading in a file...
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 07-30-2002, 09:51 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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