# Thread: problem: reading user input into an array

1. ## 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. 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);```

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

4. 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.

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

6. 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;
}```

7. 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]);
}```

8. 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]);

9. 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. 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.

11. Boy, the small mistakes that slip through the cracks! Thanks for the help, I'll make those changes.

12. 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.

13. 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. So my last post was almost right, (I missed the &). w00t.

Popular pages Recent additions