Thread: entering into 2d array

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    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.

    void Addcar (char cars[PLATE][MAX])//function to add a car

    { 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void Addcar (char cars[PLATE][MAX])
    This should be
    void Addcar (char cars[MAX][PLATE])

    [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
    2. search and add

    At the moment, your search and input are mixed together

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    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.


    void Addcar (char cars[MAX][PLATE])//function to add a car

    {
    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. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

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

    void Addcar (char cars[MAX][PLATE])//function to add a car

    {
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    when I try this, no errors in compiling but it crashes- is there anything that is obviously wrong?

    void Addcar (char cars[MAX][PLATE])//function to add a car

    {

    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. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM