11 Replies - 1724 Views - Last Post: 16 September 2009 - 01:40 PM Rate Topic: -----

#1 dtigers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 09

Error: expected primary-expression before else

Post icon  Posted 16 September 2009 - 01:07 PM

Hi I don't understand the why i keep getting primary-expression error messages.
Any help would be greatly appreciated.


This my code and the error messages follow:
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main() {
   ofstream outfile("graph.txt");
   int i,j;

   for (i=10;i>=0; i=i-1); {
	 for (j=0; j<=0; j++);{
	   if (i>5); {
		if ((j==i*2-10) || (j==20-(i*2-10)) || j==10)
		   outfile<<"X";
		else
			outfile<<".";
		else if (i==5)
			outfile<<"X";
		else
		if ((j==i*2+10) || (j==20 - (i*2+10)) || j==10
			outfile<<"X";
		else
			outfile<<".";
	 }
	}
	outfile<<endl;
  }
  for (i=5;i>0;i=i-1);{
	 for (j=0;j<=20;j++);{
		if (j==10);
		  outfile << "X";
		else
		  outfile << ".";
	 }
	 outfile<<endl;
  }
  outfile.close();

  cout >>'All Done!'>> endl;

  return 0;
}


Error Messages:
project1.cpp:47:10: warning: character constant too long for its type
project1.cpp: In function âint main()â:
project1.cpp:25: error: expected primary-expression before âelseâ
project1.cpp:25: error: expected `;' before âelseâ
project1.cpp:27: error: expected primary-expression before âelseâ
project1.cpp:27: error: expected `;' before âelseâ
project1.cpp:30: error: expected primary-expression before âelseâ
project1.cpp:30: error: expected `;' before âelseâ
project1.cpp:40: error: expected primary-expression before âelseâ
project1.cpp:40: error: expected `;' before âelseâ
project1.cpp:47: error: no match for âoperator>>â in âstd::cout >> 1869505825â

*Mod Edit: added code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Error: expected primary-expression before else

#2 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2992
  • View blog
  • Posts: 19,038
  • Joined: 14-September 07

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:08 PM

Use code tags.

also, one problem: if (j==10);
Was This Post Helpful? 0
  • +
  • -

#3 dtigers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:10 PM

What are code tags? I am new at this
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2706
  • View blog
  • Posts: 10,578
  • Joined: 15-July 08

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:11 PM

View Postdtigers, on 16 Sep, 2009 - 12:10 PM, said:

What are code tags? I am new at this

Whap your code with [ code ] code here and [/ code] (without the spaces of course)
Was This Post Helpful? 0
  • +
  • -

#5 ellimist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:13 PM

View Postdtigers, on 16 Sep, 2009 - 12:10 PM, said:

What are code tags? I am new at this

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main() {
ofstream outfile("graph.txt");
int i,j;

for (i=10;i>=0; i=i-1); {
for (j=0; j<=0; j++);{
if (i>5); {
if ((j==i*2-10) || (j==20-(i*2-10)) || j==10)
outfile<<"X";
else
outfile<<".";
else if (i==5)
outfile<<"X";
else
if ((j==i*2+10) || (j==20 - (i*2+10)) || j==10
outfile<<"X";
else
outfile<<".";
}
}
outfile<<endl;
}
for (i=5;i>0;i=i-1);{
for (j=0;j<=20;j++);{
if (j==10);
outfile << "X";
else
outfile << ".";
}
outfile<<endl;
}
outfile.close();

cout >>'All Done!'>> endl;

return 0;
}





You're problem is you do not put a ; after an if statement. Also if there is more than one statement in and if/else statement you have to use {}. ie

if (j ==10)
{
cout <<"then do this";
cout <<"and this";
}
else
{
cout <<"then do this";
cout <<"and this";
}


Was This Post Helpful? 0
  • +
  • -

#6 dtigers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:15 PM

I am still confused could you give me an example using my code.
Thanks

thanks
Was This Post Helpful? 0
  • +
  • -

#7 ellimist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:18 PM

can't do your homework for you man. Just take out the semicolons after the if statements. instead of
if(j==10);

put
if(j==10)


Same goes for your for statements. And just so you know, your errors are telling you exactly what's wrong

EDIT : and one more thing. "<<" thats the operator you use with cout. ex.

cout << "All Done!"<<endl;


This post has been edited by ellimist: 16 September 2009 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#8 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:19 PM

What a mess..@.@
Consider this code.
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main() {
ofstream outfile("graph.txt");
int i,j;

for (i=10;i>=0; i=i-1){
	for (j=0; j<=0; j++){
		if (i>5) {
			if ((j==i*2-10) || (j==20-(i*2-10)) || j==10)
				outfile<<"X";
			else
				outfile<<".";
		}
		else if (i==5)
			outfile<<"X";
		else if ((j==i*2+10) || (j==20 - (i*2+10)) || (j==10))
			outfile<<"X";
		else
			outfile<<".";
	}

outfile<<endl;
}
for (i=5;i>0;i=i-1){
	for (j=0;j<=20;j++){
		if (j==10)
			outfile << "X";
		else
			outfile << ".";
	}
	outfile<<endl;
}
outfile.close();

cout <<"All Done!"<< endl;

return 0;
}



Proper indentations increases readability. Remember that.

This post has been edited by AntonWebsters: 16 September 2009 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 dtigers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:29 PM

I still have the following error messages:
project1.cpp:47:11: warning: character constant too long for its type
project1.cpp: In function âint main()â:
project1.cpp:23: error: expected `;' before âelseâ
project1.cpp:25: error: expected primary-expression before âelseâ
project1.cpp:25: error: expected `;' before âelseâ
project1.cpp:26: error: expected `;' before â{â token
project1.cpp:30: error: expected primary-expression before âelseâ
project1.cpp:30: error: expected `;' before âelseâ
project1.cpp:47: error: no match for âoperator>>â in âstd::cout >> 1869505825â

For my code:
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main() {
   ofstream outfile("graph.txt");
   int i,j;

   for (i=10;i>=0; i=i-1); {
	 for (j=0; j<=0; j++);{
	   if (i>5); {
		 if ((j==i*2-10) || (j==20-(i*2-10)) || j==10);
		   outfile << "X"
		else
		  outfile << ".";
		else if (i==5);
		  outfile << "X"
		else
		  if ((j==i*2+10) || (j==20 - (i*2+10)) || j==10;
			  outfile << "X"
		else
			outfile << ".";
	 }
	}
	outfile << endl;
  }
  for (i=5;i>0;i=i-1);{
	 for (j=0;j<=20;j++);{
		if (j==10)
		  outfile << "X";
		else
		  outfile << ".";
	 }
	 outfile << endl;
  }
  outfile.close();

  cout >> 'All Done!' >> endl;

  return 0;
}


*Mod edit: added code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#10 ellimist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:36 PM

USE THE CODE TAGS
Was This Post Helpful? 0
  • +
  • -

#11 dtigers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:37 PM

Thanks everyone for the help
Was This Post Helpful? 0
  • +
  • -

#12 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Error: expected primary-expression before else

Posted 16 September 2009 - 01:40 PM

Dude, did you even take a look at the code I posted previously?
Anyways, cleaned up the mess for you again.
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main() {
ofstream outfile("graph.txt");
int i,j;

for (i=10;i>=0; i=i-1){
for (j=0; j<=0; j++){
if (i>5){
	if ((j==i*2-10) || (j==20-(i*2-10)) || (j==10))
		outfile << "X";
	else
		outfile << ".";
}
else if (i==5)
	outfile << "X";
else if ((j==i*2+10) || (j==20 - (i*2+10)) || (j==10))
	outfile << "X";
else
	outfile << ".";

}
outfile << endl;
}
for (i=5;i>0;i=i-1){
	for (j=0;j<=20;j++){
		if (j==10)
			outfile << "X";
		else
			outfile << ".";
	}
	outfile << endl;
}
outfile.close();

cout << "All Done!" << endl;

return 0;
}



Most of the errors consist of missing of ; at the end of the statements.
And there is no ; at the end of a for loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1