user3681862
user3681862

Reputation: 21

Creating an Integer Array and terminating with a blank

I'm writing a program that involves requesting input from the user one at a time for a random amount of integers, which terminates after the user returns a blank. These integers are then put into an array, which I use for sorting the integers later. The program essentially runs as follows:

Enter a list of integers:
> 5
> 0
> 3
>4

etc... until the user enters a blank.The program then sorts the integers and prints them out.

My snippet of code is:

static int GetIntegerArray(int array[], int max)
{
   int n, value;

   n = 0;

   while(1) {
       printf("> ");
       value = GetInteger();
       if (value == "") break;
       if (n == max) Error("Too many input items for array");
       array[n] = value;
       n++;
   }
   return(n);
}

and I get the error "Warning: comparison between pointer and integer" where value == ""

The number of elements is random, and I set the max number of possible elements to 1000 to account for this.

EDIT: Got it!! Totally overlooked that blanks had to be read as strings. Just changed it to initially read the input as strings and covert the inputs to integers if no blanks were entered. Thanks!

Upvotes: 0

Views: 38

Answers (1)

alpartis
alpartis

Reputation: 1122

In your code, value is defined as an int. You can't compare an int to a string (empty, or not). So the strict answer to your question about why the compiler is complaining is due to a type mismatch between a character pointer and an int.

The help you need is in your documentation of the GetInteger() function. What does that function return when no user input is provided? Compare 'value' to that return value and you'll be fine.

Upvotes: 2

Related Questions