Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

error C2146- plz help

Hey, im making a prototype for a MUD using C++, mostly to get better at the launguge and just for fun, I recently encountered an error however
"error C2146: syntax error : missing ';' before identifier 'main'"
I tried debugging it and finding resources on the internet to help me, does anyone know what the problem is?

#include <iostream>
#include <fstream>
using namespace std;

void look()
int main()

{float name;
int HP, XP, room;
HP = 100;
XP = 0;
room = 1;
ifstream a_file (info.txt");

look()
{if (room == 1) {cout<<"This is a grassy plain\n There is 3 deer here.\n";}
else
{cout<<"Fatal error\n";} return 0; }}


cout<<"Whats your name?";
cin>> name;
cout<<"Welcome "<< name <<\n";
a_file<<" "<< name<<" ";

cout<<"You are in a grassy field\n";

return 1 } }
[990 byte] By [theillbehaviore] at [2007-11-11 10:23:53]
# 1 Re: error C2146- plz help
you need to add a ; after the declaration of look()
(the function that you declare just before main() )
Danny at 2007-11-11 20:59:01 >
# 2 Re: error C2146- plz help
I tried that, when I do that I just get abunch of other errors

C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(14) : error C2065: 'info' : undeclared identifier
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(14) : error C2001: newline in constant
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(14) : error C2228: left of '.txt' must have class/struct/union type
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(14) : error C2143: syntax error : missing ')' before 'string'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(19) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(28) : error C2143: syntax error : missing ';' before '<<'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(28) : error C2501: 'cout' : missing storage-class or type specifiers
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(28) : error C2143: syntax error : missing ';' before '<<'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(29) : error C2143: syntax error : missing ';' before '>>'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(29) : error C2501: 'cin' : missing storage-class or type specifiers
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(29) : error C2143: syntax error : missing ';' before '>>'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(30) : error C2143: syntax error : missing ';' before '<<'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(30) : error C2501: 'cout' : missing storage-class or type specifiers
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(30) : error C2086: 'cout' : redefinition
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(30) : error C2143: syntax error : missing ';' before '<<'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(30) : error C2017: illegal escape sequence
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(30) : error C2001: newline in constant
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(34) : error C2143: syntax error : missing ';' before '<<'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(34) : error C2501: 'cout' : missing storage-class or type specifiers
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(34) : error C2086: 'cout' : redefinition
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(34) : error C2143: syntax error : missing ';' before '<<'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(36) : error C2143: syntax error : missing ';' before 'return'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(36) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\Camper #1.IDKPPL\Desktop\MUD\Cpp1.cpp(36) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

Cpp1.exe - 24 error(s), 0 warning(s)
theillbehaviore at 2007-11-11 20:59:56 >
# 3 Re: error C2146- plz help
no " before info string -- it thinks its a variable and the closing " is taken as a starter which makes a big mess.

room = 1;
ifstream a_file (info.txt"); <---------- this line
jonnin at 2007-11-11 21:00:56 >
# 4 Re: error C2146- plz help
There are many other syntax errors in your code. You have unblanaced "" as jonnin said, and you also call look() inside main without putting a ; after the call:
look();
Danny at 2007-11-11 21:02:07 >
# 5 Re: error C2146- plz help
OMG! WHat a messy code! :eek:
do that:
#include <iostream>
#include <fstream>
using namespace std;
float name;
int HP, XP;
ifstream a_file ("info.txt");
int room; //needs to be global

int look();
int main()
{

HP = 100;
XP = 0;
room = 1;
look();


cout<<"Whats your name?";
cin>> name;
cout<<"Welcome "<< name <<\n";
a_file<<" "<< name<<" ";

cout<<"You are in a grassy field\n";

return 1 }
int look(){
if (room == 1) {cout<<"This is a grassy plain\n There is 3 deer here.\n";}
else
{
cout<<"Fatal error\n";
return 0; //if ya returning then it has to be int look();
}
return 1;}
you should've done this using classes anyway, but have it yor way...
Radius at 2007-11-11 21:03:06 >
# 6 Re: error C2146- plz help
Thanks for the help guys; I think im going in the right direction
theillbehaviore at 2007-11-11 21:04:00 >