0

I have set up a couple of printf statements to find the problem and I am still clueless.

Basically I am creating the array plane to contain 12 struct seats.

Then I assign each struct inside of plane data. Everything looks good at this point.

Then I pass that array to numberEmptySeats and all of the sudden plane[0].seatID is lost in the sauce instead of being the 1 it was originally assigned.

Please help me understand why this is happening.

-------------Current Output-------------

1
Entering numberEmptySeats
1123456789101112
Seats Available: 12

------------Desired Output-------------

1
Entering numberEmptySeats
1
Seats Available: 12

Code:

#include<stdio.h>
#include<string.h>
#define SEATS 12

struct seat {
    int seatID;
    int reserved;
    char firstName[20];
    char lastName[20];
};

void resetPlane(struct seat ar[],int seats);
void numberEmptySeats(struct seat ar[],int seats);

int main()
{
    struct seat plane[SEATS];
    resetPlane(plane,SEATS);
    printf("%d\n",plane[0].seatID);
    numberEmptySeats(plane,SEATS);
}

void resetPlane(struct seat ar[],int seats)
{
    int i;
    for(i=0;i<seats;i++)
    {
        ar[i].seatID = i+1;
        ar[i].reserved = 0; 
        strcpy(ar[i].firstName,"Unassigned");
        strcpy(ar[i].lastName,"Unassigned");
    }
}

void numberEmptySeats(struct seat ar[],int seats)
{
    int i,j=0;
    printf("Entering numberEmptySeats\n");
    printf("%d",ar[0].seatID);
    for(i=0;i<seats;i++)
    {
        if (ar[i].reserved == 0)
        {
            printf("%d",ar[i].seatID);
            j++;
        }
    }
    printf("\nSeats Available: %d\n",j);
}

1 Answer 1

3

You're printing the id of every available seat, with no newline afterwards, after printing the first ID once (also without a newline).

void numberEmptySeats(struct seat ar[],int seats)
{
  int i,j=0;
  printf("Entering numberEmptySeats\n");
  printf("%d\n",ar[0].seatID);   // added newline
  for(i=0;i<seats;i++)
  {
    if (ar[i].reserved == 0)
    {
        // printf("%d",ar[i].seatID);     // drop the extra output
        j++;
    }
  }
  printf("\nSeats Available: %d\n",j);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Or include a blank or a newline in the printf() to make it clearer.
Wow, thank you for that, unfortunately the problem I am having is still happening in my main code. I ported this over and I guess it stopped it somehow. This sucks, but thank you for the answer!
I finally figured out why I was having a problem. char selection[1] = "z"; scanf("%s",selection); I changed the 1 to a 2 and no more problems. Could you please help me understand why?
A char[] array of length 1 won't have room for the terminating \0 at the end of the string. scanf() doesn't know that, and writes (at least) 2 characters anyway, probably overwriting a nearby variable.
Makes sense, I really appreciate all the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.