I'm sorry, but it won't work ,again: structures

Member
Posts: 185
Joined: 2005.02
Post: #1
ok i'm really confused as to why this isn't working. the program appears to be hung up on a simple scanf() call. I have included the relavant code bellow.

Code:
void Assign(struct seat (*)[SMAX], struct seat[SMAX]);
int getInteger(void);
int checkIfEmpty(int, struct seat[SMAX]);

function prototypes.

Code:
void Assign(struct seat (* Pointer)[SMAX], struct seat seats[SMAX])
{
    int seatToAssign;
    char lastTemp[NMAX];
    char firstTemp[NMAX];
    listEmpty(seats);
    puts("Which seat would you like to assign?(enter 0 to abort)");
    seatToAssign = getInteger();
    if (seatToAssign == 0)
        return;
    while(checkIfEmpty(seatToAssign, seats) == 0)
    {
        puts("I'm sorry, that seat is already taken.");
        seatToAssign = getInteger();
        if (seatToAssign == 0)
            return;
    }
    puts("Please enter the last name:");
    fgets(lastTemp, NMAX, stdin);
    puts("And the last name:");
    fgets(firstTemp, NMAX, stdin);
    printf("assign %s %s to seat %d?(y or n)", firstTemp, lastTemp, seatToAssign);
    if (getchar() == 'y')
    {
        for (int count = 0; count < SMAX; count++)
        {
            if (seats[count].seatNum == seatToAssign)
            {
                Pointer[count]->Taken == 1;
                Pointer[count]->last == lastTemp;
                Pointer[count]->first == firstTemp;
                return;
            }    
        }
    }
}
                                                            
int getInteger(void)
{
    puts("Y");
    int input = 0;
    input = scanf("%d");
    puts("y");
    trashRest();
    while (input < 0 || input > SMAX)
    {
        puts("Invalid input");
        input = scanf("%d");
        trashRest();
    }
    return input;
}    

int checkIfEmpty(int seatToCheck, struct seat seats[SMAX])
{
    int true = 0;
    for (int count = 0; count < SMAX; count++)
    {
        if (seats[count].seatNum == seatToCheck)
        {
            if (seats[count].Taken == 0)
            {
                true++;
                return true;
            }
        }
    }
    return true;
}

the Assign function gets to the point where it calls the getInteger function. I inserted the two puts() calls to find where the function was getting hung up. It prints the first Y but crashes before the second is printed. Which leads me to believe that the scanf call or assignment is what is causing the problem. When i enter a number it crashes and the debugger comes up and highlights this line: "0x900d3850 <+4856> stw r4,0(r2)"
which says nothing to me.

Anybody see the problem?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
scanf takes a variable argument list of pointers, the return value is how many items in your string %d's %f's etc that its managed to get, you should do somthing like scanf("%d", &my_integer).

also man scanf.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #3
darn it! I knew it was something simple.
now I feel really stupid.Blush
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  I built structures into TNTBasic. leRiCl 1 2,292 Apr 22, 2007 07:35 PM
Last Post: Shunter