2011-10-19 4 views
0

다음은 내 코드 목록입니다. 친절하게보세요. 옵션 1은 값을 입력합니다. 반면 옵션 5는 값을 표시합니다. 내 프로그램에 문제가 있습니다.값 입력 및 표시

값을 입력하고 표시하는 데 문제가 없지만 정상적으로 표시됩니다. 때 등, 예를 들면 값이 2 세트 I 입력 ..

(1, 1, 타입 O, 1, 1, 1, 1) < - 제 1 세트. (2, 2, 타입 K, 2, 2, 2, 2) < - 두 번째 세트.

그것을 표시

FIRST DISPLAY (2, 2, 타입 K, 2, 2, 2, 2) & 2ND 디스플레이 (2,2, 0,0,0, 공백 , 0).

왜 그렇습니까? 도움을 주신 모든 분들께 미리 감사드립니다.

//DECLARATIONS 
MissionPlan m; 
int i; 
PointTwoD pArray[2]; 
vector<PointTwoD> point2DVector = vector<PointTwoD>(); // instantiate an empty vector 
vector<PointTwoD>::iterator point2DVectorIterator; 
PointTwoD point2D; 
LocationData locData; 
int travdistance = 0; 
int OptionChosen; 

//OPTION CHOSEN 
if (OptionChosen == 1) 
{ 
    //declarations 
    int i=0; 
    int x, y, earth, moon; 
    float Particulate, Plasma, civIndex; 
    string sType; 
    string mystr; 

    cout<<"[Input Statistical data] \n"; 

    cin.ignore(); 
    cout<<"Please enter x-ordinate: "; 
    getline (cin,mystr); 
    stringstream(mystr) >> x; 

    cout<<"Please enter y-ordinate: "; 
    getline (cin,mystr); 
    stringstream(mystr) >> y; 

    //cin.ignore(); 
    cout<<"Please enter sun type: "; 
    getline (cin,sType); 

    cout<<"Please enter no. of earth-like planets: "; 
    getline (cin,mystr); 
    stringstream(mystr) >> earth; 

    cout<<"Please enter no. of earth-like moons: "; 
    getline (cin,mystr); 
    stringstream(mystr) >> moon; 

    cout<<"Please enter ave. particulate density (%-tage): "; 
    getline (cin,mystr); 
    stringstream(mystr) >> Particulate; 

    cout<<"Please enter ave. plasma density (%-tage): "; 
    getline (cin,mystr); 
    stringstream(mystr) >> Plasma; 

    PointTwoD point2D = PointTwoD(x,y,locData, civIndex=0); 
    point2DVector.push_back(point2D); // Insert newly formed point2D object to insert into the vector of PointTwoD objects 
    point2DVector[i].setxCOORD(x); 
    point2DVector[i].setyCOORD(y); 
    point2DVector[i].locData.setSunType(sType); 
    point2DVector[i].locData.setNoOfEarth(earth); 
    point2DVector[i].locData.setNoOfMoon(moon); 
    point2DVector[i].locData.setPartDensity(Particulate); 
    point2DVector[i].locData.setPlasDensity(Plasma); 
    cout<<"\n"; 
    cout<<"Record successfully stored. Going back to the main menu ... \n"; 
    i++; 
} 


if (OptionChosen == 5) 
{ 
    for(int i=0; i< point2DVector.size();i++) 
    { 
     cout << "************************************************ \n"; 
     cout << "   DISPLAY INPUT RECORDS    \n"; 
     cout << "************************************************ \n"; 
     cout << "x-ordinate:    " << point2DVector[i].getxCOORD() << "\n"; 
     cout << "y-ordinate:    " << point2DVector[i].getyCOORD() << "\n"; 
     cout << "sun type:    " << point2DVector[i].locData.getSunType() << "\n"; 
     cout << "no. of earth-like planets:  " << point2DVector[i].locData.getNoOfEarth() << "\n"; 
     cout << "no. of earth-like moons:  " << point2DVector[i].locData.getNoOfMoon() << "\n"; 
     cout << "ave. particulate density:  " << point2DVector[i].locData.getPartDensity() << "%" "\n"; 
     cout << "ave. plasma density:   " << point2DVector[i].locData.getPlasDensity() << "%" "\n"; 
    } 
} 

답변

1

그것은 간단하다 : 당신은 당신이 완전히 point2dVector 컬렉션에 추가하기 전에 point2d를 구성하는 더 나을 것이다 당신은 옵션 1

을 선택할 때마다 0으로 i를 재설정하는

if (OptionChosen == 1) 
{ 
    //declarations 
    int i=0; 

:

PointTwoD point2D = PointTwoD(x,y,locData, civIndex=0); 
point2D.setxCOORD(x); 
point2D.setyCOORD(y); 
point2D.locData.setSunType(sType); 
point2D.locData.setNoOfEarth(earth); 
point2D.locData.setNoOfMoon(moon); 
point2D.locData.setPartDensity(Particulate); 
point2D.locData.setPlasDensity(Plasma); 
point2DVector.push_back(point2D); // Insert newly formed point2D object to insert into 

그런 다음 i 변수를 제거 할 수 있습니다. 즉, 복사 생성자가 올바르게 정의되었는지 확인해야합니다.

제쳐두고, 디버거를 사용하는 방법을 배우는 데 시간을 할애해야합니다. 그것은 당신이 당신의 문제를 발견하고 해결하는 데 도움이되었을 것입니다.

0

어쩌면 i를 0으로 재설정하지 않아도됩니다.

관련 문제