(c)我遇到这个问题,每当我尝试执行它崩溃的程序时

时间:2016-07-25 20:40:19

标签: c windows

我正在使用的编译器是如果有任何帮助那么使用C,这对我的回答非常重要!它本来是一个房子的口头演练,程序没有发现任何错误,但我无法让它发挥作用。

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

#define North 0
#define East 1
#define South 2
#define West 3

void SetupRooms();
void DescribeRoom(unsigned int RN);
void DescribeExits(unsigned int RN);
signed int GetDirection(unsigned int RN);

typedef struct{
    char Description[500];
    unsigned int Exit[4];

}Location;

Location Room [8];

int main(void){
    unsigned int Room = 1;
    signed int Direction;
    SetupRooms();

    while(1){
        _clrscr();
        DescribeRoom(Room);
        DescribeExits(Room);
        Direction = GetDirection(Room);
        if(Direction == -1){
            printf("\n BYE!\n");
            exit(0);
        }
        if (Direction>0)Room=Direction;
            else printf("\nSorry you can't go that way...\n");
    }
}

void SetupRooms(){
    strcpy(Room[1].Description, "Welcome to the Simonsen house, come on in!\n");
    Room[1].Exit[North]=7;
    Room[1].Exit[East]=0;
    Room[1].Exit[South]=0;
    Room[1].Exit[West]=2;
    strcpy(Room[2].Description, "You've entered the Hall! Signs of an busy family surround you; a pile of skateboards and an overflowing coat stand. You take off your shoes before continuing.\n");
    Room[2].Exit[North]=3;
    Room[2].Exit[East]=1;
    Room[2].Exit[South]=0;
    Room[2].Exit[West]=5;
    strcpy(Room[3].Description, "Fancy a drink while you're in the kitchen? If you hear a scrabbling noise don’t worry, it’s just Homer the Bearded Dragon.\n");
    Room[3].Exit[North]=0;
    Room[3].Exit[East]=7;
    Room[3].Exit[South]=2;
    Room[3].Exit[West]=4;
    strcpy(Room[4].Description, "So this is the dining room, feel free to play any of the musical instruments\n... or just look at the embarassing childhood photos...\n");
    Room[4].Exit[North]=7;
    Room[4].Exit[East]=3;
    Room[4].Exit[South]=5;
    Room[4].Exit[West]=6;
    strcpy(Room[5].Description, "Welcome to the living room, the centre of the house with its cheerful yellow and blue walls, cream leather corner sofa and hundreds of DVDs. See anything of interest?\n");
    Room[5].Exit[North]=4;
    Room[5].Exit[East]=1;
    Room[5].Exit[South]=0;
    Room[5].Exit[West]=0;
    strcpy(Room[6].Description, "Make sure you don't disturb the person revising! The study is filled with French teaching materials, random cables and liquor...\n");
    Room[6].Exit[North]=0;
    Room[6].Exit[East]=4;
    Room[6].Exit[South]=8;
    Room[6].Exit[West]=0;
    strcpy(Room[7].Description, "You pop on the clogs by the door and explore the garden.\nHow about you have a bounce on the trampoline while you're here?\n");
    Room[7].Exit[North]=0;
    Room[7].Exit[East]=3;
    Room[7].Exit[South]=4;
    Room[7].Exit[West]=0;
    strcpy(Room[8].Description, "The garage light turns on as you walk in and you try not to trip over anything.\n");
    Room[8].Exit[North]=6;
    Room[8].Exit[East]=0;
    Room[8].Exit[South]=1;
    Room[8].Exit[West]=0;
}

signed int GetDirection(unsigned int RN){
int NextRoom = 0;
unsigned char d;
printf("Type the first letter of the direction you wish to travel.");
printf("\nOr type q to quit\n");
d = _getch();
if (d == 'n') NextRoom = Room[RN].Exit[North];
if (d == 'e') NextRoom = Room[RN].Exit[East];
if (d == 's') NextRoom = Room[RN].Exit[South];
if (d == 'w') NextRoom = Room[RN].Exit[West];
if (d == 'q') NextRoom = -1;

return(NextRoom);
}

void DescribeExits(unsigned int RN){
    printf("\nExits are available to the: ");
    if(Room[RN].Exit[North]>0) printf("\n North");
    if(Room[RN].Exit[East]>0) printf("\n East");
    if(Room[RN].Exit[South]>0) printf("\n South");
    if(Room[RN].Exit[West]>0) printf("\n West");
}

void DescribeRoom(unsigned int RN){     
     printf("\n %s", Room[RN].Description); 
}

1 个答案:

答案 0 :(得分:4)

C中的数组从0开始索引。您将数组定义为具有8个成员,因此有效索引为0-7。但你访问了房间[8]。不是没有这样的野兽。