C++无法填充数据
C++ can't populate data
我正在尝试用x和y值填充我的向量。 但它似乎没有添加,而只是覆盖第一。
主.cpp
#include <iostream>
#include "Point.h"
using namespace std;
int x,y;
Point point;
string options;
void someMethods();
int main()
{
cout << "Please Enter x-Cordinate"<< endl;
cin >> x;
cout << "Please Enter y-Cordinate" << endl;
cin >> y;
cout << "Enter cords again? yes/no"<< endl;
cin >> options;
while (options == "yes") {
cout << "Please Enter x-Cordinate"<< endl;
cin >> x;
cout << "Please Enter y-Cordinate" << endl;
cin >> y;
cout << "Enter cords again? yes/no"<< endl;
cin >> options;
}
if(options == "no") {
Point Point(x,y);
Point.someMethods();
// break;
}
}
点.h
#ifndef Point_Point_h
#define Point_Point_h
#include <vector>
class Point {
private:
int x,y;
public :
Point() {
x = 0;
y = 0;
} //default consrructor
Point(int x,int y);
int getX();
int getY();
void setX(int x);
void setY(int y);
std::vector<Point> storeData;
void someMethods();
};
#endif
点.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(int x,int y) {
setX(x);
setY(y);
}
int Point::getX() {
return x;
}
int Point::getY() {
return y;
}
void Point::setX(int x) {
this->x = x;
}
void Point::setY(int y) {
this->y = y;
}
void Point::someMethods() {
x = getX();
y = getY();
Point Point(x,y);
storeData.push_back(Point);
for (int i=0; i<storeData.size(); i++) {
cout << "X "<< storeData[i].getX() <<"Y " << storeData[i].getY() << endl;
}
// do some methods here after getting the x and y cords;
}
我怎样才能做到例如(我输入 x 和 y 3 次,假设 1,1 2,2 3,3 (
然后它将输出
X: 1,Y: 1
X: 2,Y: 2
X: 3,Y: 3
int main()
{
// don't need global variables, just define local ones here
int x,y;
Point point;
string options;
// You shouldn't store the vector of Points in the Point class itself.
// It doesn't have anything to do with a Point. classes should generally
// only contain relevant information (ex. Point contains only x and y coords).
vector<Point> pointsVector;
// do-while will do the contents of the loop at least once
// it will stop when the while condition is no longer met
do
{
cout << "Please Enter x-Cordinate"<< endl;
cin >> x;
cout << "Please Enter y-Cordinate" << endl;
cin >> y;
pointsVector.push_back(Point(x, y));
cout << "Enter cords again? yes/no"<< endl;
cin >> options;
} while (options == "yes")
// don't really need to check if options is "no"
// if you have exited the do/while loop above, the assumption is that you don't
// want to enter more coordinates.
doSomethingWithTheVectorOfPoints(pointsVector);
return 0;
}
在函数doSomethingWithTheVectorOfPoints中,您可以放置用于输出X和Y坐标的代码。(您也可以直接循环遍历 main 函数中的向量。
此外,还可以向 Point 类添加一个名为 ToString 或 Print 的成员函数来为您完成工作。
编辑:我实际上并没有编译这个,它只是让你知道如何重写你的代码。
你应该有:
- 无全局变量
- 支持流输入(输出(的点类
- 从点类中存储的数据(为什么一个糟糕的点应该管理它?
- 使用验证流式传输输入。
例:
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <vector>
struct Point {
int x;
int y;
};
std::istream& operator >> (std::istream& in, Point& point) {
return in >> point.x >> point.y;
}
typedef std::vector<Point> PointStorage;
int main()
{
PointStorage point_storage;
Point point;
while(true) {
std::cout << "Please enter X and Y xordinates or 'no' to stop input" << std::endl;
std::string line;
if( ! std::getline(std::cin, line))
throw std::invalid_argument(line);
else {
std::istringstream point_input(line);
// Skip leading white spaces, read a point, skip trailing white apace
// and ensure no additional character is left.
if(point_input >> point >> std::ws && point_input.eof()) {
point_storage.push_back(point);
}
else {
std::string no;
std::istringstream no_input(line);
// Skip leading white spaces, read "no", skip trailing white apace
// and ensure no additional character is left.
if(no_input >> no >> std::ws && no_input.eof() && no == "no") {
break;
}
throw std::invalid_argument(line);
}
}
}
for(PointStorage::const_iterator pos = point_storage.begin();
pos != point_storage.end();
++pos)
{
std::cout << pos->x << ", " << pos->y << 'n';
}
return 0;
}
注意:抛出异常可能是一个错误的决定,但它简化了示例。
每次输入"no"时,您都会使用最终坐标重新创建Point
对象。这就是为什么你只保留最后一对。
在不相关的说明中,您可能应该大大简化代码。Point
对象没有理由首先保留Point
对象的向量。您可能希望在那里保留原始坐标的历史记录/序列,并具有类似以下内容的内容:
Point mypt;
while (options == "yes") {
mypt.AddCoords(x, y);
// read more coords/options
}
// do stuff on filled mypt object
相关文章:
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- C++:使用缓冲区中的数据填充结构
- 用数据填充未指定大小的数组
- 缓冲区用不需要的数据填充 char 数组中的最后一个空格
- 使用队列和列表 STL 进行C++分配。在将数据填充到列表或队列中然后打印该数据时遇到问题
- 用原始数据填充 std::vector
- 给定一个填充无符号字符**的 C 函数,如何在没有中间副本的情况下用数据填充 std::vector
- 使用相同的数据填充数组对象或使用指针
- 从原始数据填充成员的正确方法是什么
- 最易读(代码术语)的方式,用 C++03 中的构造数据填充地图<字符串、字符串>
- 从 std::vector<无符号字符中的数据填充结构>
- (VS2015)正在尝试用初始值设定项列表中的数据填充静态映射
- 用大量唯一数据填充 c++ 中的嵌套结构
- 用来自 std::map 的数据填充 QML 列表视图
- 在本地将数据填充到recv缓冲区中
- 如何用音频数据填充AVFrame
- 无法使用sqlite3数据库表的所有行中的数据填充列表,错误:跟踪/断点陷阱(核心转储)
- 用异构boost::融合向量类型数据填充std::vector
- 用类fnc中的数据填充向量
- std::vector 每次使用时都会用相同的数据填充