输入&值的显示

发布于 2024-12-11 01:24:35 字数 3493 浏览 0 评论 0原文

下面是我的代码列表。请您看一下。 选项1是输入值。而选项 5 则显示值。 我的程序的问题是。

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

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

它显示像

首次显示(2, 2, K 型, 2, 2, 2, 2)和第二个显示器(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";
    }
}

Below are the list of my codes. Please kindly take a look.
Option 1 is inputting of values. Whereas Option 5 is displaying of values.
The problem with my program is.

I dont have a problem inputting and displaying 1 set of values, it displays normally.
When i input 2 set of values for example like..

(1, 1, Type O, 1, 1, 1, 1) <-- 1st set. (2, 2, Type K, 2, 2, 2, 2) <--
2nd set.

it displays like

FIRST DISPLAY (2, 2, Type K, 2, 2, 2, 2) & 2ND DISPLAY (2,2,
BLANK,0,0,0,0).

Why is that so?
Thanks in advance to all who helps.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-12-18 01:24:35

很简单:

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

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

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

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

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

It's simple:

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

You are resetting i to zero every time you select option 1.

You will be better off fully constructing point2d before adding to the point2dVector collection:

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 

And then you can remove the i variable. This does mean that you need to make sure the copy constructor is correctly defined.

As an aside, spend some time learning how to use a debugger. That would have helped you find and solve your problem.

笛声青案梦长安 2024-12-18 01:24:35

也许你不应该每次都将 i 重置为 0...

Maybe you should not reset i to 0 every time...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文