2013-10-11 129 views
0

我想用x和y值填充我的向量。但似乎并没有增加,只是首先覆盖了 。C++不能填充数据

的main.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; 
    } 
} 

Point.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 

Point.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 
+0

这是因为您*覆盖了第一个值 - 数据仅在最后一次输入后才会输出。另外,你是否熟悉'do..while'循环?这在这里很有用。 – 2013-10-11 17:11:00

+0

C++新手?一些基本的指针:避免使用名称空间标准,避免使用全局变量,并且Get/Set方法的使用与其他语言(如Java)不同。 – crashmstr

+0

是的我是新来的C++,我慢慢学习它 – user2211678

回答

-1

每次进入“不”时重新创建Point对象与最终COORDS。这就是为什么你只保留最后一对。

在不相关的说明中,您应该大幅简化代码。没有理由让Point对象首先保留Point对象的向量。你可能想保留原有坐标的历史/序列,并有类似:

Point mypt; 

while (options == "yes") { 
    mypt.AddCoords(x, y); 
    // read more coords/options 
} 

// do stuff on filled mypt object 
+0

感谢您的建议 – user2211678

2
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坐标。 (您也可以直接在主函数中直接循环访问矢量)

另外,您可以向Point类中添加一个名为ToString或Print的成员函数来为您完成工作。

编辑:我没有真正编译这个,只是让你知道如何重写你的代码。

+0

感谢您的意见 – user2211678

0

你应该有:

  • 没有全局变量
  • Point类支持流输入(输出)
  • 出点类的存储数据(一个贫穷的点,为什么要管呢?)
  • 带验证的流输入。

例子:

#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; 
} 

注意:抛出异常可能是一个错误的决定,但它简化了的例子。

+0

感谢您的意见 – user2211678