作为学校项目的一部分,我必须建立一个程序,让用户通过房间的房间。我是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
答案 0 :(得分:1)
Error: Unresolved External "_main" in ...
表示链接器找不到它正在寻找的名为“_main”的符号。 main
函数应该如下所示:
int main() { ... }
或者,如果您需要命令行参数,请执行以下操作:
int main(int argc, char *argv[]) { ... }
为了将来参考,如果您发现尝试缩小问题范围,则可以获得更好的响应 - 在这里您可以删除所有代码而不是void main() {}
并且仍然可以看到同样的错误。那将是一个相当强烈的线索。
答案 1 :(得分:0)
您的main函数需要返回一个整数(非零被解释为错误)。