作为学校项目的一部分,我必须构建一个程序,让用户穿过房子的房间。我是 C 新手,所以我有点卡在一个我无法解决的错误上。
该程序背后的概念是用户将通过带有数字的控制台输入方向命令,数字表示北,南,东,西的方向。
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define North 0
#define East 1
#define South 2
#define West 3
typedef struct{
char Description[200];
unsigned int Exit[4];
}
location;
void SetupRooms();
void DescribeRoom(unsigned int RoomNumber);
void DescribeExits(unsigned int RoomNumber);
int GetDirection(unsigned int RoomNumber);
location Room[8];
void main(){
unsigned int RoomNumber=1;
signed int Direction;
SetupRooms();
while(1){
//system("cls");
DescribeRoom(RoomNumber);
DescribeExits(RoomNumber);
Direction = GetDirection(RoomNumber);
if(Direction ==-1)
{
printf("\n BYE BYE!\n");
break;//exit(0);
}
if(Direction > 0)
RoomNumber = Direction;
else
printf("\n You just walked into a wall! \n");
}
}
void SetupRooms()
{
strcpy(Room[1].Description,"Hallway. ");
Room[1].Exit[North] =5;
Room[1].Exit[East] =7;
Room[1].Exit[South] =-1;
Room[1].Exit[West] =2;
strcpy(Room[2].Description,"Dining Room. ");
Room[2].Exit[North]=4;
Room[2].Exit[East]=1;
Room[2].Exit[South]=0;
Room[2].Exit[West]=3;
strcpy(Room[3].Description, "Kitchen. ");
Room[3].Exit[North]=-1;
Room[3].Exit[East]=2;
Room[3].Exit[South]=0;
Room[3].Exit[West]=0;
strcpy(Room[4].Description, "Main Bedroom. ");
Room[4].Exit[North] =0;
Room[4].Exit[East]=5;
Room[4].Exit[South]=2;
Room[4].Exit[West]=-1;
strcpy(Room[5].Description, "Bathroom. ");
Room[5].Exit[North]=0;
Room[5].Exit[East]=6;
Room[5].Exit[South]=1;
Room[5].Exit[West]=4;
strcpy(Room[6].Description, "Kids Bedroom. ");
Room[6].Exit[North]=0;
Room[6].Exit[East]=0;
Room[6].Exit[South]=7;
Room[6].Exit[West]=5;
strcpy(Room[7].Description, "Lounge. ");
Room[7].Exit[North]=6;
Room[7].Exit[East]=-1;
Room[7].Exit[South]=0;
Room[7].Exit[West]=1 ;
}
void DescribeRoom(unsigned int RoomNumber)
{
printf("The room you are in is the: %s",Room[RoomNumber].Description);
}
void DescribeExits(unsigned int RoomNumber)
{
if (Room[RoomNumber].Exit[North] != -1)
printf("To the North is the: %d",Room[RoomNumber].Exit[North]);
else
printf("No Exit to the North");
if (Room[RoomNumber].Exit[East] != -1)
printf("To the East is the: %d",Room[RoomNumber].Exit[East]);
else
printf("No Exit to the East");
if (Room[RoomNumber].Exit[South] != -1)
printf("To the South is the: %d",Room[RoomNumber].Exit[South]);
else
printf("No Exit to the South");
if (Room[RoomNumber].Exit[West] != -1)
printf("To the West is the: %d",Room[RoomNumber].Exit[West]);
else
printf("No Exit to the West");
}
int GetDirection(unsigned int RoomNumber)
{
int RetVal = -1;
char Input = _getch();
switch(Input)
{
case 'n':
RetVal = 0;
break;
case 's':
RetVal = 1;
break;
case 'e':
RetVal = 2;
break;
case 'w':
RetVal = 3;
break;
}
return RetVal;
}
这是我运行后得到的错误
;============ Building Project hose2 ============
;============ Linking ============
Linker Error (Severity 4)
No matching files for file specification:
"|:\\acfs5\dt11\dt11ddf\my documents\cc386 projects\assign.prac.2.obj"
Error: Unresolved External "_main" in Module "C:\tools\cc\clibs\platform\WIN32\pe\c0.c"
Linker Error (Severity 255)
Trouble opening "|:\\acfs5\dt11\dt11ddf\my documents\cc386 projects\hose2.lss" for output.
Compile done. Errors: 3, Warnings: 0