简体   繁体   中英

Unknown error on C++, error: expected primary expression before ';' token

Unknown error on C++, error: expected primary expression before ';' token. Here is the code I wrote in C++:

 #include <iostream>
 #include <math.h>
 #include <stdio.h>
 #define G 6.674E-11
 using namespace std;

 int main()
 {
 //Ms = Mass of sun, Me = Mass of Earth, Fg = Gravitational force between them, As =                                   Acceleration of Sun, Ae = Acceleration of Earth, Ve_x
 // = initial velocity of Earth in x direction, Ve_y = initial velocity of Earth in y          direction, Vs_x = initial velocity of the Sun in x direction
 // Vs_y = initial velocity of sun in y direction, t = time, F = Gravitational force   `    between the two bodies.

    float Ms, Me, Fg, As, Ae, Ve_x, Ve_y, Vs_x, Vs_y, pos_E, pos_S, r_x, r_y, r, t;
    float S_dist;
    float E_dist;
    float F;
    float Ve[2];
    float Vs[2];
    float pe[2];
    float ps[2];


  FILE *fileptr;

    cout <<"Enter mass of the Sun in kg\n";
    cin >> Ms;
    cout <<"Enter mass of the earth in kg\n";
    cin >> Me;
    cout <<"Enter intial velocity of the sun in x direction in m/s\n";
    cin >> Vs[0];
    cout <<"Enter intial velocity of the sun in y direction in m/s\n";
    cin >> Vs[1];
    cout <<"Enter intial velocity of the earth in x direction in m/s\n";
    cin >> Ve[0];
    cout <<"Enter intial velocity of the earth in y direction in m/s\n";
    cin >> Ve[1];
    cout <<"Enter intial position of the sun in x component\n";
    cin >> ps[0];
    cout <<"Enter intial position of the sun in y direction\n";
    cin >> ps[1];
    cout <<"Enter intial position of the earth in x direction\n";
    cin >> pe[0];
    cout <<"Enter intial position of the earth in y direction\n";
    cin >> pe[1];


  for (t=0; t<30000; t+1)
 {
  float E_dist;
  float S_dist;
  float F;

    E_dist=sqrt( ((pe[0]-pe[0])*(pe[0]-pe[0])) + ((pe[1]-pe[1])*(pe[1]-pe[1])) );
    S_dist=sqrt( ((ps[0]-ps[0])*(ps[0]-ps[0])) + ((ps[1]-ps[1])*(ps[1]-ps[1])) );

    r_x=( (pe[0]-pe[0]) - (ps[0]-ps[0]) );
    r_y=( (pe[1]-pe[1]) - (ps[1]-ps[1]) );
    r= sqrt( (r_x)*(r_x) + (r_y)*(r_y) );

    F=(G*Me*Ms)/(r*r);

    Ae = F/Me;
    As = F/Ms;

    Ve_x = Ve[0];
    Ve_y = Ve[1];
    Vs_x = Vs[0];
    Vs_y = Vs[1];
    }
    cout<<"At time "<<t<<":\n The position of the Sun is "<<S_dist<<"\n The position of   the Earth is "<<E_dist
    <<"\n The acceleration of the Sun is "<<As<<" \n The acceleration of the Earth is "<<Ae<<" \nThe velocity of the Sun in the x direction is "
    <<Vs_x<<" \n The velocity of the Sun in the y direction is "<<Vs_y<<" \n The velocity of the Earth in the x direction is "<<Ve_x<<
    " \n The velocity of the Earth in the y direction is "<<Ve_y<<" \n The gravitational force between the Sun and the Earth is "<<F<<; // ERROR OCCURRED HERE.

} 

Would be grateful for any help, thanks.

I think the error is that your last line ends like this:

<<F<<;

Notice the << operator is being applied to just one argument. Did you mean to write something like this?

<<F<<endl;

For what it's worth, I would strongly suggest splitting that output line into multiple lines for clarity. What you have now is correct, but it's incredibly difficult to read. Rewriting it as

cout << "At time " <<t<<":\n The position of the Sun is "<<S_dist<<"\n";
     << " The position of   the Earth is "<<E_dist << "\n";
     << "The acceleration of the Sun is "<<As<<"\n"
     << "The acceleration of the Earth is "<<Ae<<"\n";
     << "The velocity of the Sun in the x direction is "<<Vs_x<<" \n";
     << "The velocity of the Sun in the y direction is "<<Vs_y<<" \n";
     << "The velocity of the Earth in the x direction is "<<Ve_x<< "\n";
     << "The velocity of the Earth in the y direction is "<<Ve_y<<" \n";
     << "The gravitational force between the Sun and the Earth is "<<F<<;

would have made this error much easier to spot, since the line numbering information would be more useful. Also, I would suggest adding spaces in-between the << operators to make it a bit easier to read.

templatetypedef has pointed out the compiler error, but there is another problem in the code: the for loop is infinite:

for (t=0; t<30000; t+1)

should be:

for (t=0; t<30000; t++)

or as t is a float , based on Is using increment (operator++) on floats bad style? :

for (t = 0; t < 30000; t+=1.0f)

your first syntax error is because of having another << at the end of statement cout<<F<<; just change it to cout<<F; unless you have another var to print or you want to use endline like: cout<<F<<endl; .

you have another one syntax error too! your main returns int but you didn't returned anything at the end:

return 0;

Another very very very important issue is about the variable F !

Logical error!!!

int main()
{
....
  float S_dist; //real S_dist !!!
  float E_dist; //real E_dist !!!
  float F;      //real F !!!
  for (t=0; t<30000; t++)
  {

     float F; // just exists in for!!!
     float E_dist; //just exists in for!!!
     float E_dist; //just exists in for!!!
  ....
     F=(G*Me*Ms)/(r*r); //changing local F, which just exists in for
     E_dist=sqrt( ((pe[0]-pe[0])*(pe[0]-pe[0])) + ((pe[1]-pe[1])*(pe[1]-pe[1])) );
     S_dist=sqrt( ((ps[0]-ps[0])*(ps[0]-ps[0])) + ((ps[1]-ps[1])*(ps[1]-ps[1])) );
  }
  cout<<F<<E_dist<<S_dist; //this prints the main vars!
  return 0;
} 

be sure to read about local variables more. and keep in mind that t+1 doesn't change the t at all! you should write t++ or t+=1 in the last part of the for statement instead, unless you will get an infinite loop, cause your t does not grow at all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM