输入&值的显示
下面是我的代码列表。请您看一下。 选项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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很简单:
每次选择选项 1 时,您都将
i
重置为零。在添加到
point2dVector
之前,最好完全构造point2d
集合:然后您可以删除
i
变量。这确实意味着您需要确保正确定义复制构造函数。顺便说一句,花一些时间学习如何使用调试器。这将帮助您找到并解决您的问题。
It's simple:
You are resetting
i
to zero every time you select option 1.You will be better off fully constructing
point2d
before adding to thepoint2dVector
collection: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.
也许你不应该每次都将 i 重置为 0...
Maybe you should not reset i to 0 every time...