输入和输入显示值

时间:2011-10-19 09:59:00

标签: c++ input vector

以下是我的代码列表。请好好看看。 选项1是输入值。选项5显示值。 我的程序的问题是。

输入和显示1组值时没有问题,它显示正常。 当我输入2组值时,例如..

  

(1,1,O型,1,1,1,1)< - 第1组。 (2,2,类型K,2,2,2,2)< -   第二盘。

显示为

  

第一个显示(2,2,类型K,2,2,2,2)&第二个显示(2,2,   BLANK,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";
    }
}

2 个答案:

答案 0 :(得分:1)

很简单:

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

每次选择选项1时,您都会将i重置为零。

在添加到point2d集合之前,您最好完全构建point2dVector

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变量。这确实意味着您需要确保正确定义了复制构造函数。

顺便说一句,花些时间学习如何使用调试器。那会帮助你找到并解决你的问题。

答案 1 :(得分:0)

也许你不应该每次都把我重置为0 ......