Overclock.net banner

1 - 7 of 7 Posts

·
Banned
Joined
·
2,226 Posts
Discussion Starter #1
another C++ question<br>
when i compile this program<br><div class="bbcode_code">
<div class="bbcode_code_head">Code:</div>
<pre>
<code>/////////////////////////////


///////////////////////////////

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;
int newratingsame(int ro, int s, int rop);
int newratingdiffr(int ro, int s, int rop);
int newratingdiffp(int ro, int s, int rop);
void initdisplay();
void secdisplay();
int main()
{
string b,c,d,e,f,g,h;
int a, ro, s, rop;
initdisplay();
cin >>a;
if (a == 1)
{
cout <<"Name of new player (no spaces):";
getline (cin,b);
c=b+"ratings.txt";
ofstream myfile3("namelist.txt");
myfile3 <<b <<"\
";
ofstream myfile2(c.c_str());
d="1200";
myfile2 <<d;
cout <<"Player added successfully, press enter to continue:";
system("PAUSE");
initdisplay();
}
if (a == 2)
{
int ab,pa,pb;
secdisplay();
cin >>ab;
if(ab==1)
{
string i,ia,is,ias;
int ans,ansa;
ifstream myfile4("namelist.txt");
while (! myfile4.eof() )
{
getline (myfile4,e);
cout << e << endl;
}
cout<<"Player 1's NAME:";
getline (cin, i);
i=i+"ratings.txt";
ifstream myfile7(i.c_str());
cout<<"Player 2's NAME:";
getline (cin, ia);
ia=ia+"ratings.txt";
ifstream myfile6(ia.c_str());
while (! myfile7.eof() )
{
getline (myfile7,is);
cout << is << endl;
}
while (! myfile6.eof() )
{
getline (myfile6,ias);
cout << ias << endl;
}
cout <<"\
enter P1 original rating (see above):";
cin >>pa;
cout <<"\
enter P2 rating (see above):";
cin >>pb;

//player 1
cout <<"did P1 win (1) lose(-1) or draw(0); enter correct number:";
cin >>s;
ans=newratingsame(pa,s,pb);
ofstream myfile8(i.c_str());
myfile8 <<ans;
//player2
cout <<"did P2 win (1) lose(-1) or draw(0); enter correct number:";
cin >>s;
ansa=newratingsame(pb,s,pa);
ofstream myfile9(ia.c_str());
myfile9 <<ansa;
cout <<"done";
system("PAUSE");
initdisplay();


}
if (a == 3)
{
ifstream myfile4("namelist.txt");
while (! myfile4.eof() )
{
getline (myfile4,e);
cout << e << endl;
}
cout << "Enter name of player whose rating you want to see:";
getline (cin,f);
g=f+"ratings.txt";
ifstream myfile5(g.c_str());
while (! myfile5.eof() )
{
getline (myfile5,h);
cout << h << endl;
}
initdisplay();
}
if (a ==4)
{system("PAUSE");}
system("PAUSE");
return 0;
}
void initdisplay()
{
cout << "////////////////////////////////////\
";
cout << "Chess Rating Manager v1.0\
";
cout << "Sreenath Pillai\
";
cout << "////////////////////////////////////\
\
\
\
";
cout << "[1] Add Player\
";
cout << "[2] Score Game\
";
cout << "[3] Check Ratings\
";
cout << "[4] Quit\
";
cout << "\
\
Choose:";
}
void secdisplay()
{
cout << "\
\
[1] both players rated OR both players provisional\
";
cout << "[2] one player rated and one player provisional\
";
cout << "[3] Back";
cout << "\
\
Choose:";
}
int newratingsame(int ro, int s, int rop)
{
return (ro + (s*21) + ((rop-ro)/25));
}
int newratingdiffr(int ro, int s, int rop)
{
return (ro + (s*6) + ((rop-ro)/100));
}
int newratingdiffp(int ro, int s, int rop)
{
return ((.8*ro) + (.2*rop) + (s*80);
}</code>
</pre></div>
it gives me these errors<br>
(everything in int main is fine)<br>
after int main,<br>
starting with the void initdisplay, i get errors that say the following:<br><div style="margin:20px;margin-top:5px;">
<div class="smallfont" style="margin-bottom:2px;">Quote:</div>
<table border="0" cellpadding="6" cellspacing="0" width="99%"><tr><td class="alt2" style="border:1px inset;">
<div>Originally Posted by <strong>dev-c++compiler</strong></div>
<div style="font-style:italic;">117 C:\\Dev-Cpp\<br>
ew.cpp expected primary-expression before "void"<br>
117 C:\\Dev-Cpp\<br>
ew.cpp expected `;' before "void"<br>
129 C:\\Dev-Cpp\<br>
ew.cpp expected primary-expression before "void"<br>
129 C:\\Dev-Cpp\<br>
ew.cpp expected `;' before "void"<br>
137 C:\\Dev-Cpp\<br>
ew.cpp a function-definition is not allowed here before '{' token<br>
137 C:\\Dev-Cpp\<br>
ew.cpp expected `,' or `;' before '{' token<br>
141 C:\\Dev-Cpp\<br>
ew.cpp a function-definition is not allowed here before '{' token<br>
141 C:\\Dev-Cpp\<br>
ew.cpp expected `,' or `;' before '{' token<br>
145 C:\\Dev-Cpp\<br>
ew.cpp a function-definition is not allowed here before '{' token<br>
145 C:\\Dev-Cpp\<br>
ew.cpp expected `,' or `;' before '{' token<br>
147 C:\\Dev-Cpp\<br>
ew.cpp expected `}' at end of input</div>
</td>
</tr></table></div>
so really everything is right except there are really only 2 types of errors, one about the ; and } and token; and the other about the 'expected primary expression' which i thought i had....i dont understand what is wrong i declared function prototype and used them in the program then said what they do after int main<br>
im really frustrated the code should work<br>
of course i whipped this off in under an hour <img alt="" class="inlineimg" src="/images/smilies/biggrin.gif" style="border:0px solid;" title="Big Grin"> so there could easily be something i just missed <img alt="" class="inlineimg" src="/images/smilies/frown.gif" style="border:0px solid;" title="Frown"><br>
idk, please help me fellow coders!!!
 

·
Premium Member
Joined
·
2,767 Posts
I don't have a C++ compiler on this machine so I can't tell you exactly what the issue is. It looks like it's trying to include your function declaration in the other part of the code because it's expecting a primary expression somewhere. Generally, that means you haven't finished off some other expression in your main function. The solution? I would go over the code with a fine tooth comb and make sure you've terminated all your statements with ; and that all program segments are properly braced ie { }.<br><br>
If you haven't properly terminated an earlier code segment, the compiler will just assume that your function declaration at line 117 is still part of your main function. That's my best guess anyway.
 

·
Registered
Joined
·
1,282 Posts
i think i will agree<br><br>
if your interested<br><div style="margin:20px;margin-top:5px;">
<div class="smallfont" style="margin-bottom:2px;">Quote:</div>
<table border="0" cellpadding="6" cellspacing="0" width="99%"><tr><td class="alt2" style="border:1px inset;">Compiler: Default compiler<br>
Executing g++.exe...<br>
g++.exe "C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp" -o "C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.exe" -I"C:\\Dev-Cpp\\lib\\gcc\\mingw32\\3.4.2\\include" -I"C:\\Dev-Cpp\\include\\c++\\3.4.2\\backward" -I"C:\\Dev-Cpp\\include\\c++\\3.4.2\\mingw32" -I"C:\\Dev-Cpp\\include\\c++\\3.4.2" -I"C:\\Dev-Cpp\\include" -L"C:\\Dev-Cpp\\lib"<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp: In function `int main()':<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:117: error: expected primary-expression before "void"<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:117: error: expected `;' before "void"<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:129: error: expected primary-expression before "void"<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:129: error: expected `;' before "void"<br><br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:137: error: a function-definition is not allowed here before '{' token<br><br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:137: error: expected `,' or `;' before '{' token<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:141: error: a function-definition is not allowed here before '{' token<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:141: error: expected `,' or `;' before '{' token<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:145: error: a function-definition is not allowed here before '{' token<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:145: error: expected `,' or `;' before '{' token<br>
C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp:147: error: expected `}' at end of input<br><br>
Execution terminated</td>
</tr></table></div>
<br>
edit: i managed to get it down to 2 errors. but theyre in line 146:<br><br><div style="margin:20px;margin-top:5px;">
<div class="smallfont" style="margin-bottom:2px;">Quote:</div>
<table border="0" cellpadding="6" cellspacing="0" width="99%"><tr><td class="alt2" style="border:1px inset;">146 C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp expected `)' before ';' token<br>
146 C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp [Warning] converting to `int' from `double'</td>
</tr></table></div>
edit again: down to one error<br><br><div style="margin:20px;margin-top:5px;">
<div class="smallfont" style="margin-bottom:2px;">Quote:</div>
<table border="0" cellpadding="6" cellspacing="0" width="99%"><tr><td class="alt2" style="border:1px inset;">146 C:\\Documents and Settings\\sleeper\\Desktop\\Untitled1.cpp [Warning] converting to `int' from `double'</td>
</tr></table></div>
edit for a third time:<br><br>
i got it to compile. it still gives me 1 error but it compiles and it runs.<br><br><div class="bbcode_code">
<div class="bbcode_code_head">Code:</div>
<pre>
<code>/////////////////////////////


///////////////////////////////

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;
int newratingsame(int ro, int s, int rop);
int newratingdiffr(int ro, int s, int rop);
int newratingdiffp(int ro, int s, int rop);
void initdisplay();
void secdisplay();
int main()
{
string b,c,d,e,f,g,h;
int a, ro, s, rop;
initdisplay();
cin >>a;
if (a == 1)
{
cout <<"Name of new player (no spaces):";
getline (cin,b);
c=b+"ratings.txt";
ofstream myfile3("namelist.txt");
myfile3 <<b <<"\
";
ofstream myfile2(c.c_str());
d="1200";
myfile2 <<d;
cout <<"Player added successfully, press enter to continue:";
system("PAUSE");
initdisplay();
}
if (a == 2)
{
int ab,pa,pb;
secdisplay();
cin >>ab;
if(ab==1)
{
string i,ia,is,ias;
int ans,ansa;
ifstream myfile4("namelist.txt");
while (! myfile4.eof() )
{
getline (myfile4,e);
cout << e << endl;
}
cout<<"Player 1's NAME:";
getline (cin, i);
i=i+"ratings.txt";
ifstream myfile7(i.c_str());
cout<<"Player 2's NAME:";
getline (cin, ia);
ia=ia+"ratings.txt";
ifstream myfile6(ia.c_str());
while (! myfile7.eof() )
{
getline (myfile7,is);
cout << is << endl;
}
while (! myfile6.eof() )
{
getline (myfile6,ias);
cout << ias << endl;
}
cout <<"\
enter P1 original rating (see above):";
cin >>pa;
cout <<"\
enter P2 rating (see above):";
cin >>pb;

//player 1
cout <<"did P1 win (1) lose(-1) or draw(0); enter correct number:";
cin >>s;
ans=newratingsame(pa,s,pb);
ofstream myfile8(i.c_str());
myfile8 <<ans;
//player2
cout <<"did P2 win (1) lose(-1) or draw(0); enter correct number:";
cin >>s;
ansa=newratingsame(pb,s,pa);
ofstream myfile9(ia.c_str());
myfile9 <<ansa;
cout <<"done";
system("PAUSE");
initdisplay();

<span style="color:#00FF00;">}</span>
}
if (a == 3)
{
ifstream myfile4("namelist.txt");
while (! myfile4.eof() )
{
getline (myfile4,e);
cout << e << endl;
}
cout << "Enter name of player whose rating you want to see:";
getline (cin,f);
g=f+"ratings.txt";
ifstream myfile5(g.c_str());
while (! myfile5.eof() )
{
getline (myfile5,h);
cout << h << endl;
}
initdisplay();
}
if (a ==4)
{system("PAUSE");}
system("PAUSE");
return 0;
}
void initdisplay()
{
cout << "////////////////////////////////////\
";
cout << "Chess Rating Manager v1.0\
";
cout << "Sreenath Pillai\
";
cout << "////////////////////////////////////\
\
\
\
";
cout << "[1] Add Player\
";
cout << "[2] Score Game\
";
cout << "[3] Check Ratings\
";
cout << "[4] Quit\
";
cout << "\
\
Choose:";
}
void secdisplay()
{
cout << "\
\
[1] both players rated OR both players provisional\
";
cout << "[2] one player rated and one player provisional\
";
cout << "[3] Back";
cout << "\
\
Choose:";
}
int newratingsame(int ro, int s, int rop)
{
return (ro + (s*21) + ((rop-ro)/25));
}
int newratingdiffr(int ro, int s, int rop)
{
return (ro + (s*6) + ((rop-ro)/100));
}
int newratingdiffp(int ro, int s, int rop)
{
return <span style="color:#FF0000;">(</span>(.8*ro) + (.2*rop) + (s*80);
}</code>
</pre></div>
what i removed is in red and what i added is in green<br>
dont forget to remove the color tags if you copy it. that would prbly destroy your code and give you errors.
 

·
Banned
Joined
·
2,226 Posts
Discussion Starter #4
thanks. LOL i got it myself the EXACT SAME way you did sleeper, found the two missing (one { and one ) )<br>
got it to run<br>
nothing works right though<br>
if you can sleeper try running press 1 to add player and you will see it doesnt work. also try to score player, still doesnt work
 

·
Registered
Joined
·
1,282 Posts
i think you need to have the input for player 1 and then the input for player 2 after you enter player 1 and press enter. ill look into coding that little section so that it works tonight.
 

·
Banned
Joined
·
2,226 Posts
Discussion Starter #6
i found an easier way to make the program<br>
it works perfectly and is compact<br>
it can be found at<br><a href="http://www.sreenathpillai.frih.net/simplechessprog.rar" target="_blank">www.sreenathpillai.frih.net/simplechessprog.rar</a><br>
edit:added as attachment as well
 

·
Registered
Joined
·
1,282 Posts
thats what i was trying to get at with cin >> but it was being glitchy and i was getting pissed.
 
1 - 7 of 7 Posts
Top