Thread: scanf in an array help

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    scanf in an array help

    Code:
     #include <stdio.h>
    
    int main(void)
    
    {
    
    	int  iNumbers[10];
    	int  iEntry=0;
    	int  x=0;
    
    	printf("Enter 10 numbers\n");
    	 
    	
    	for (x=0; x<=9; x++) {
    		scanf("%d\t", &iNumbers[x]);
    		printf("\n\t%d iNumbers\n", iNumbers[x]);//used for debugging
    		printf("\n\t%d X\n", x); //used for debugging
    	}
    	
    	printf("\n\nWhich order would you like to see your numbers?");
    	printf("\n1)\tAscending\n");
    	printf("\n2)\tDescending\n");
    	scanf("%d", &iEntry);
    	printf("\n\t%d iEntry\n\n", iEntry);//used for debugging
    	switch(iEntry)	{
    		case 1:
    		printf("\n\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",iNumbers[0],iNumbers[1],iNumbers[2],iNumbers[3],iNumbers[4],iNumbers[5],iNumbers[6],iNumbers[7],iNumbers[8],iNumbers[9]);
    		break;
    		
    		case 2:
    		printf("\n\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",iNumbers[9],iNumbers[8],iNumbers[7],iNumbers[6],iNumbers[5],iNumbers[4],iNumbers[3],iNumbers[2],iNumbers[1],iNumbers[0]);
    		break;
    		}
    }
    
    /*Build a program that uses a single-dimension array to store
    10 numbers input by a user. After inputting the numbers, the
    user should see a menu with two options to sort and print the
    10 numbers in ascending or descending order.*/
    It's hard for me to describe this so I will post what the output looks like if i enter 1-4 as an example,

    Enter 10 numbers
    1
    2
    1 iNumbers
    0 X
    3
    2 iNumbers
    1 X
    4
    3 iNumbers
    2 X

    I don't quite understand how this is happening because the printf is ignored for the first input and if I enter 10 numbers (1-10 as an example) the tenth number will also be applied to a different scanf variable (iEntry).

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Don't use \t in your scanf(). Any whitespace in scanf() means "read as much whitespace as you can". It doesn't matter what whitespace characters you specify, they all mean the same thing.

    So you hit 1, then hit enter. scanf() is still seeing valid characters (enter being whitespace), so it waits to see what's next. It's a 2, so finally scanf() returns. Only then will your printf() show up. Etc.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In scanf() you have entered a tab - that is not part of the scanf() format or control codes, so it's messing everything up.

    Overall, you have a real over-use problem with \t (tabs and what not), that should be left out of a program, until next to the end.

    Instead of a printf() line of code that runs into next week, just use a simple for loop:
    Code:
    for(i = 0; i < 10; i++)
      printf("%6d  ", number[i]);
    Note how the 6 gives a field width of 6 spaces, and the two spaces after the d will give two more spaces, later. So each number will take up an 8 char wide field - which just happens to fit perfectly onto an 80 char wide display console window.

    i < 10 is better than i <= 9. Using <= sizeOfArray-1 will come back to bite you.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf used in an array
    By BrandNew in forum C Programming
    Replies: 3
    Last Post: 02-28-2011, 11:06 PM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 10:58 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM