Thread: problem: reading user input into an array

  1. #1
    alpha561
    Guest

    problem: reading user input into an array

    This program sorts an array of data, and is supposed to print the first number of the array and the last number of the array after the sort.

    The problem is that it will let the user enter more than the size of the array. If you run it, you'll see what I mean. Would I have to implement some form of sentinel that checks if they have entered more than the array size?

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    void bubble(int *, const int, int (*)(int, int));	/* bubble sort */
    void swap(int *, int *);	
    int sortAscending(const int, const int);/* sorting items in ascending order*/
    
    
    int main(void)
    {
    	int months[SIZE];
    	int counter;
    
    	printf("Sorting Data in array months\n");
    	scanf("%d", &months);
    
    	
    	bubble(months, SIZE, sortAscending);
    	
    	/* prints out the first value */
    	for (counter = 0; counter <= 1 - 1; counter++)
    	{
    			printf("%4d\n", months[counter]);
    	}
    
    	/* prints out the last value */
    	for (counter = 0; counter <= SIZE; counter++)
    
    		if (counter == SIZE -1)
    		{
    			printf("%4d", counter[months]);
    		}
    
    	printf("\n");
    
    	return 0;
    
    
    }
    
    /* sorts the array months */
    void bubble(int *work, const int size, int (*compare)(int, int))
    {
    	int pass, count;
    
    	void swapt(int *, int *);
    
    	for (pass = 1; pass <= size - 1; pass++)
    
    		for (count = 0; count <= size - 2; count++)
    
    			if ((*compare) (work[count], work[count + 1]))
    				swap(&work[count], & work[count + 1]);
    }
    
    
    void swap(int *element1Ptr, int *element2Ptr)
    {
    	int temp;
    
    	temp = *element1Ptr;
    	*element1Ptr = *element2Ptr;
    	*element2Ptr = temp;
    
    }
    
    /* sorts the array months into ascending order (smallest to largest) */
    int sortAscending(const int a, const int b)
    {
    
    	return b < a;
    }

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Use fgets, as opposed to scanf, for exactly that reason.
    Seach this board for a myriad of examples.

    Here's one I grabbed earlier, (poster unknown)
    Code:
    char car [10]; 
    printf ("\nEnter type of car"); 
    fgets(car, sizeof(car), stdin);
    Last edited by Azuth; 05-23-2002 at 10:17 PM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    alpha561
    Guest
    fgets won't work because I'm dealing with an integer array, not a character array.

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Ah, I see. Can't you validate the input later? Convert the string with atof or similar? Sorry if this isn't much help, I'm very new.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    alpha561
    Guest
    That's okay, I appreciate the help anyway .

  6. #6
    Registered User alpha561's Avatar
    Join Date
    May 2002
    Posts
    18
    I added some code to print the values in the array month into a random access file. Again when you enter in any values, it returns rubbish to the screen, and to the file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 10
    
    void bubble(int *, const int, int (*)(int, int));	/* bubble sort */
    void swap(int *, int *);	
    int sortAscending(const int, const int);/* sorting items in ascending order*/
    
    
    int main(void)
    {
    	int months[SIZE];
    		//= {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
    	int counter;
    
    	FILE *cfPtr;
    
    	if((cfPtr = fopen("phonebill.dat", "w")) == NULL)
    		printf("The file could not be opened!\n");
    
    	else {
    
    	printf("Enter your monthly phone bills for each month of the year\n");
    	scanf("%d", &months);
    
    	
    	bubble(months, SIZE, sortAscending);
    
    
    		for (counter = 0; counter <= SIZE - 1; counter++)
    		{
    		
    		fprintf(cfPtr, "%4d\n", months[counter]);
    		
    		}
    			
    		fclose(cfPtr);
    	
    	/* prints out the first value */
    	for (counter = 0; counter <= 1 - 1; counter++)
    	{
    			printf("%4d\n", months[counter]);
    	}
    
    	/* prints out the last value */
    	for (counter = 0; counter <= SIZE; counter++)
    
    		if (counter == SIZE -1)
    		{
    			printf("%4d", counter[months]);
    		}
    
    	printf("\n");
    
    	}
    
    	return 0;
    
    
    }
    
    /* sorts the array months */
    void bubble(int *work, const int size, int (*compare)(int, int))
    {
    	int pass, count;
    
    	void swapt(int *, int *);
    
    	for (pass = 1; pass <= size - 1; pass++)
    
    		for (count = 0; count <= size - 2; count++)
    
    			if ((*compare) (work[count], work[count + 1]))
    				swap(&work[count], & work[count + 1]);
    }
    
    
    void swap(int *element1Ptr, int *element2Ptr)
    {
    	int temp;
    
    	temp = *element1Ptr;
    	*element1Ptr = *element2Ptr;
    	*element2Ptr = temp;
    
    }
    
    /* sorts the array months into ascending order (smallest to largest) */
    int sortAscending(const int a, const int b)
    {
    
    	return b < a;
    }
    alpha561

    "You don't want to sell me death sticks"

  7. #7
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I know I'm still very green, but will this work?

    scanf("%d", &months);

    Would something like this work?

    Code:
    for (x=0;x<SIZE;x++)
    {
    scanf ("%d",months[SIZE]);
    }
    Demonographic rhinology is not the only possible outcome, but why take the chance

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'd like to help you, but I am unsure what the flow of your program is supposed to be. Can you explain what it is supposed to do, then I'll help you.

    In the meantime, here's some observations

    - Why is the months array only 10 elements?

    - You only ask for one input from the user. How is the rest of the array going to get filled?

    >scanf("%d", &months);
    If you're going to do this, you need to check scanf()'s return code.

    >for (counter = 0; counter <= 1 - 1; counter++)
    You don't need to loop to print only one element. Just do:
    >printf("%4d\n", months[0]);

    >for (counter = 0; counter <= SIZE; counter++)
    You don't need to loop to print the last element either. Just do
    >printf("%4d\n", months[SIZE-1]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    3
    alpha561

    in "void bubble( () () ) {"

    you refer to "void swapt(int *, int *);" with a "t"

    whereas, in the last line, the reference is to: "swap(&work[count], etc"

    The actual function is shown as:

    "void swap(int *element1Ptr, int *element2Ptr) {"

    Forgive my ignorance if there is a need for the "t" that I am unaware of, but it just looks (to a novice) like a typographic error.

    Bill

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by BillD

    in "void bubble( () () ) {"

    you refer to "void swapt(int *, int *);" with a "t"

    whereas, in the last line, the reference is to: "swap(&work[count], etc"

    Forgive my ignorance if there is a need for the "t" that I am unaware of, but it just looks (to a novice) like a typographic error.
    The line
    >void swapt(int *, int *);
    within the bubble() function is a function prototype. This function (swapt) doesn't get called so I expect you are correct, it's a typo.

    But, even if it was supposed to be swap, the prototype isn't needed here, as it is done at the top of the file.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User alpha561's Avatar
    Join Date
    May 2002
    Posts
    18
    Boy, the small mistakes that slip through the cracks! Thanks for the help, I'll make those changes.
    alpha561

    "You don't want to sell me death sticks"

  12. #12
    Registered User alpha561's Avatar
    Join Date
    May 2002
    Posts
    18
    Finally got the blasted thing working. To read in the input, I had to do something similar to how I printed out the array into the file

    Code:
    for (counter = 0; counter <= SIZE -1; counter++)
    	{
    		scanf("%d", &months[counter]);
    	}
    I'm pretty rusty when it comes to dealing with arrays. Too much time spent (wasted?) on Swing in Java.
    alpha561

    "You don't want to sell me death sticks"

  13. #13
    Unregistered
    Guest
    This works.
    Code:
    #include <stdio.h>
    #define SIZE 10
    
    void bubble(int *, const int, int (*)(int, int));	/* bubble sort */
    void swap(int *, int *);	
    int sortAscending(const int, const int);/* sorting items in ascending order*/
    
    
    int main(void)
    {
    	int months[SIZE];
    	int counter;
    
    	printf("Enter 10 numbers\n");
    	for( counter = 0; counter < SIZE; ++counter )
                    if( scanf("%d", &months[counter]) != 1 )
                            perror( "scanf filling months" );
    
    	printf("Sorting Data in array months\n");
    	bubble(months, SIZE, sortAscending);
    	
    	/* prints out the first value */
    	printf("%4d\n", months[0]);
    
    	/* prints out the last value */
    	printf("%4d\n", months[SIZE-1]);
    
    	return 0;
    }
    
    /* sorts the array months */
    void bubble(int *work, const int size, int (*compare)(int, int))
    {
    	int pass, count;
    
    	for (pass = 1; pass <= size - 1; pass++)
    
    		for (count = 0; count <= size - 2; count++)
    
    			if ((*compare) (work[count], work[count + 1]) != 0)
    				swap(&work[count], & work[count + 1]);
    }
    
    
    void swap(int *element1Ptr, int *element2Ptr)
    {
    	int temp;
    
    	temp = *element1Ptr;
    	*element1Ptr = *element2Ptr;
    	*element2Ptr = temp;
    
    }
    
    /* sorts the array months into ascending order (smallest to largest) */
    int sortAscending(const int a, const int b)
    {
    	return (int)(b < a);
    }

  14. #14
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    So my last post was almost right, (I missed the &). w00t.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM
  2. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 12:35 PM
  3. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  4. How to let the user input values for an array...
    By TerranAce007 in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2002, 03:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM