C++上的未知错误,错误:';'之前的预期主表达式 代币

1 c++

C++上的未知错误,错误:';'之前的预期主表达式 令牌.这是我用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.

} 
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助,谢谢.

tem*_*def 8

我认为错误是你的最后一行结束如下:

<<F<<;
Run Code Online (Sandbox Code Playgroud)

请注意,<<运算符仅应用于一个参数.你的意思是写这样的东西吗?

<<F<<endl;
Run Code Online (Sandbox Code Playgroud)

为了它的价值,我强烈建议为了清晰起见将该输出线分成多行.你现在拥有的是正确的,但阅读起来非常困难.将其重写为

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<<;
Run Code Online (Sandbox Code Playgroud)

因为行编号信息会更有用,所以会更容易发现这个错误.另外,我建议在<<运算符之间添加空格,以使其更容易阅读.


hmj*_*mjd 5

templatetypedef指出了编译器错误,但代码中还有另一个问题:for循环是无限的:

for (t=0; t<30000; t+1)
Run Code Online (Sandbox Code Playgroud)

应该:

for (t=0; t<30000; t++)
Run Code Online (Sandbox Code Playgroud)

或者作为t一个float,基于使用增量(operator ++)浮动坏风格?:

for (t = 0; t < 30000; t+=1.0f)
Run Code Online (Sandbox Code Playgroud)