# Thread: entering into 2d array

1. ## entering into 2d array

This is week number 5 of my programming education. I have read the rules of the forum. I do not want anyone to do my homework for me. I have spent two days on this and I am getting no where. I am writing a function that will take in a license plate number and desired lot position (eg 123vhs 5). I am to search the (already nulled array) for cars in that position, if no car is there, print 'your cars is parked in lot 5'. If a car is there, put the car in the next empty lot. If lot is full, print 'lot is full'

it is probably obvious that this won't work, I need a few pointers in the right direction.

{ int number=0;
number++;

if (number > MAX)
{
puts("Lot is full!");
return;
}

{
int i,j,k;

for(i=0; i<=PLATE; i++)
{ for (j=0;j<=MAX;j++)

scanf("%s%s%s",&i,&j,&k);
sprintf(cars,"%s,%s",i,j);

printf("Car parked in space %s!\n",cars);
}

}

}

2. > void Addcar (char cars[PLATE][MAX])
This should be

[MAX] is the number of car parking spaces, and in each space, there is a licence PLATE

The next thing you need to do is break the code into two steps
1. input

At the moment, your search and input are mixed together

3. I am trying now to compare the last character input by user to all rows in the second column of the array but I am getting conversion errors.

{
int a,b,c;
scanf("%s%s%s",&a,&b,&c);//input user entry, variable b will consume the whitespace

Lotfull(cars);//function call that determines if the lot is full

{ int n;
for(n=0;n<=MAX-1;++n)//searching the array for a match to the location requested
if(cars[n]==c)//getting an error with the '==',
{
int i,j;
for(i=0; i<=MAX; i++)
{ for (j=0;j<=PLATE;j++)

printf("Car parked in space %s!\n",cars);
}

}
}

}

void Lotfull (char cars[MAX][PLATE])
{

int number=0;
number++;

if (number > MAX) //checks to see if more than 20 cars entered
{
puts("Lot is full!");
Options();
}

}

4. cars[n] is a string and c is an integer, this is an illegal comparison. Instead of using an integer to compare with, try using an array and compare the two strings with strcmp. Or if you are only trying to compare one character, specify which one with something like:

if ( cars[n][last] == c )

Keep in mind that cars is a two dimensional array and should be treated as such.

-Prelude

5. I wish I did know how to treat 2 dimensional arrays.

{
char a,b,c;
scanf("%s%s%s",&a,&b,&c);//input user entry, variable b will consume the whitespace

if (strchr(cars[],c)!=NULL)

I thought I could treat the lot number as a character rather than an int, I want to bring in the user input and compare the last entry represented by c to all rows in the array.

6. > char a,b,c;
> scanf("%s%s%s",&a,&b,&c);//input user entry, variable b will consume the whitespace
Well to read a string and an int, do this

char new_car[PLATE];
int lot;
scanf( "%s %d", new_car, lot );

> if (strchr(cars[],c)!=NULL)
An empty lot would be something like
if ( cars[i][0] == '\0' ) {
// lot i is free
}

7. when I try this, no errors in compiling but it crashes- is there anything that is obviously wrong?

{

Lotfull(cars);//function call that determines if the lot is full

char new_car[PLATE];
int lot;
char b;
scanf( "%s%s%s", new_car,b, lot );

char i;
if ( cars[i][0] == '\0' )
{
cars[i][0]==(new_car[0]);
//strncpy(cars[i][0],lot,new_car);
printf("Car parked in space %s!\n",cars[i]);

}
}

8. >scanf( "%s%s%s", new_car,b, lot );
b is not a string, you also need to pass the address of a non-pointer variable to scanf. lot is not a string either, it should be like this:

scanf ( "%s %d %c", new_car, &b, &lot );

This kind of error will usually compile with little more than a warning and will almost always seg fault.

-Prelude