void user_input (int * array, int intSize)
{
	printf("Please enter any number of strings (80 characters per string max), enter an empty string to signal input completion\n\n");
	
	int i = 0;
	int j = 0;
	int arrayMax = MAXARRAY - 1;
	
	char input[81];
	
	while (input[0] != '\0')
	{
		printf("->");
		
		i = 0;
		
		while((input[i] = getchar())!= i < 80 && '\n')
		{
			i++;
		}
		
		if(input[i] != '\0')
			input[i] = '\0';
		
		
		if(j == arrayMax)
		{
			realloc_data_up(array, intSize);
				
			arrayMax = MAXARRAY + arrayMax;
		}

			array[j] = atoi(input);
			j++;
	}
	
	realloc_data_down(array, j, intSize);
}

input should terminate when the user enters a blank string, (a null '\0'). But it doesnt work, i'm probably not seeing some small detail.

Any help is greatly appreciated.

Recommended Answers

All 5 Replies

I'm not sure what drug you guys are taking that kills brain cells, but it seems like everyone and his brother is having trouble with passing pointers to functions and changing where they point to.

But here's an example that does what you want (without using dynamic memory, for simplicity). Compare with what you have and try to locate the differences:

#include <stdio.h>
#include <stdlib.h>

int user_input(int *array, int size)
{
    int n = 0;

    while (n < size)
    {
        char input[81];
        int i = 0;

        printf("->");

        /* Be sure to check for EOF as well as '\n' */
        while ((input[i] = getchar()) != '\n' && input[i] != EOF)
        {
            i++;
        }

        /* '\n' or EOF found immediately */
        if (i == 0)
            break;

        /* Always terminate your strings */
        input[i] = '\0';

        /* atoi is evil, but that's another lesson */
        array[n++] = atoi(input);
    }

    /* We need to know how many integers were saved */
    return n;
}

int main(void)
{
    int array[10];
    int n = user_input(array, 10);
    int i;

    for (i = 0; i < n; i++)
        printf("%d\n", array[i]);

    return 0;
}

Yeah i understand how to do that, and my code isn't faulty in that aspect. What im looking for is a way to terminate the while loop when a user enters an empty string. So what should happen is, getchar() will gather from character buffer and place into the char array, until 80 spots are filled or user hits enter, then a null is placed at the end (for end of string). Then that string is converted to an integer (EX: string "and123" atoi(string) would equal "123") and that is saved in the array of ints, then the array of ints is incremented. What i want it to do is to terminate this loop when a user enters nothing (hitting enter). And the way ive done it makes sense to me that it should work but for some reason (which i cannot find) it does not do what i want.

nevermind i got it to do what i wanted.

>Yeah i understand how to do that, and my code isn't faulty in that aspect.
Bullshit. Your code is as faulty as the "logic" of believing that your code isn't faulty when it clearly doesn't work.

>And the way ive done it makes sense to me that it should work but
>for some reason (which i cannot find) it does not do what i want.

You're the only one it makes sense to, of that I can assure you. I think my favorite line in terms of sheer lunacy would be this one:

while((input[i] = getchar())!= i < 80 && '\n')

Is this you talking about??

while( (ch=getchar())!="\n" && i < MAX_SIZE)
{
          input[i] = ch;
          i++;
}
input[i] = "\0";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.