2012-04-05 1 views
1

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. 

} 

이 덕분에 어떤 도움을 보내 주시기 바랍니다 는 여기에 내가 C++로 작성한 코드입니다.

+3

이것은 (루프에서) 잘못되었습니다. 't + 1'. 그것은't'의 값을 변화시키지 않습니다. 표현을 평가하고 결과를 버립니다. 무한 루프가 있습니다. 또한 ... 루프 인덱스 변수의 부동 소수점 숫자? –

+0

이것은 Google에서 가장 많이 검색 한 문제 중 하나입니다 (700 만 개의 결과 만). 컴파일러 작성자가 무엇이 잘못되었는지를 알려줄 것이라고 생각할 것입니다. –

+0

안녕하세요 @Pat. 스택 오버플로에 오신 것을 환영합니다. 오류를 찾을 수 있도록 프로그램을 게시 해 주셔서 감사합니다. 오류가없는 모든 행을 처음으로 삭제 한 경우 훨씬 더 작은 프로그램을 남겨두면 더 많은 도움이됩니다. 이 디버깅 기술에 대한 자세한 내용은 http://sscce.org를 참조하십시오. 다시 한 번 환영합니다! –

답변

8

나는 오류가 마지막 라인은 다음과 같이 끝나는 생각 :

<<F<<; 

주의가 << 연산자는 하나 개의 인수에 적용되고있다. 이런 식으로 편지 쓰려고 했니?

<<F<<endl; 

그것은 가치가 무엇인지, 내가 강하게 분할을 제안 들어 그 명확성을 위해 여러 줄에 출력 라인. 당신이 지금 가지고있는 것은 정확하지만 읽는 것은 대단히 어렵습니다.

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<<; 

으로 다시 작성하면 줄 번호 매기기 정보가 더 유용하기 때문에이 오류를 쉽게 찾아 낼 수 있습니다. 또한 좀 더 읽기 쉽게하기 위해 << 연산자 사이에 공백을 추가하는 것이 좋습니다.

+0

추가 cout은 필수적인 FYI가 아니며 없이는 읽을 수 있습니다. –

+0

동의, 전 사업자를 지키고 다른 모든 진술을 잊어 버리 겠어. – chris

+0

@ 0A0D - 의견에 감사드립니다. 결정된. – templatetypedef

5

templatetypedef 컴파일러 에러를 지적했지만, 다른 코드에 문제가있다 : for 루프가 무한 :

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

이어야 : t 같은

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

또는이되고 float, Is using increment (operator++) on floats bad style? 기준 :

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

사실이지만 OPs 오류는 컴파일 타임 오류입니다. +1 (어쨌든 질문에 대한 답변이 아님) – Marlon

+0

좋은 찾기는하지만 문제를 해결하지 못합니다 (이 질문에서 파생 된 다른 질문으로 바뀔 수 있음) +1 –

+0

@ Marlon, 나는 대답하지 않습니다. 문제. 컴파일러 오류를 해결하는 대답을 참조하십시오. – hmjd

0

첫 번째 구문 오류가 다른 문 끝에 있기 때문에 cout<<F<<;cout<<F; 그냥 인쇄 할 다른 var 또는 끝 줄을 사용하지 않는 한 cout<<F<<endl; 있습니다.

다른 구문 오류가 있습니다. 당신의 main 반환 값 int하지만 당신은 마지막에 아무것도 반환되지 않은 :

return 0;

또 다른 아주 아주 아주 중요한 문제는 변수 F에 관한 것입니다!

논리적 오류 !!!

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; 
} 

로컬 변수에 대해 자세히 읽어보십시오. t+1t을 전혀 변경하지 않는다는 점에 유의하십시오. 대신 for 문장의 마지막 부분에 t++ 또는 t+=1을 써야합니다! 무한 루프가 발생하지 않으면 t이 전혀 성장하지 않습니다!

관련 문제