C++ C++;can';不填充数据
我试图用x和y值填充向量。但它似乎没有添加,只是覆盖了 首先 main.cppC++ C++;can';不填充数据,c++,C++,我试图用x和y值填充向量。但它似乎没有添加,只是覆盖了 首先 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 &
#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
object没有理由首先保留Point
对象的向量。您可能希望在那里保留原始坐标的历史记录/序列,并具有如下内容:
Point mypt;
while (options == "yes") {
mypt.AddCoords(x, y);
// read more coords/options
}
// do stuff on filled mypt object
intmain()
{
//不需要全局变量,只需在此处定义局部变量即可
int x,y;
点-点;
字符串选项;
//不应将点的向量存储在Point类本身中。
//这与点无关。类通常应该
//仅包含相关信息(例如,Point仅包含x和y坐标)。
向量点向量;
//do while将至少执行一次循环内容
//当while条件不再满足时,它将停止
做
{
cout x;
库蒂;
点向量。向后推(点(x,y));
cout选项;
}而(选项==“是”)
//不需要检查选项是否为“否”
//如果您已经退出了上面的do/while循环,那么假设您没有退出
//要输入更多坐标。
点向量(点向量)的dosomething;
返回0;
}
在具有点矢量的函数doSomething中,可以放置用于输出X和Y坐标的代码。(也可以直接在主函数中循环向量。)
此外,您还可以向Point类中添加名为ToString或Print的成员函数,以便为您完成这项工作
编辑:我实际上并没有编译这个,它只是让您了解如何重写代码。您应该:
- 没有全局变量
- 支持流输入(输出)的点类
- 存储的数据不属于point类(为什么一个差的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;
}
#包括
#包括
#包括
#包括
结构点{
int x;
int-y;
};
std::istream&operator>>(std::istream&in,Point&Point){
返回>>点x>>点y;
}
typedef std::向量点存储;
int main()
{
点存储点存储;
点-点;
while(true){
std::cout point>>std::ws&&point_input.eof()){
点存储。向后推(点);
}
否则{
std::字符串编号;
std::istringstream无输入(行);
//跳过前导空格,读“否”,快速跳过尾随空格
//并确保不留下任何附加字符。
如果(无输入>>无>>标准::ws&&no\u输入.eof()&&no==“否”){
打破
}
throw std::无效的_参数(行);
}
}
}
for(PointStorage::const_iterator pos=point_storage.begin();
pos!=点存储.end();
++pos)
{
STD::因为你是在重写第一个值,数据只在上次输入之后才输出。另外,你熟悉<代码>……而循环吗?它在这里很有用。新的C++?一些基本指针:避免使用<命名空间STD > <代码> > <代码>,避免全局变量,并且GET/SET方法不被使用太多。像其他一些语言(如java)一样,是的,我是C++新手,我慢慢地学习它,请避免像“点(x,y)”这样的事情。-例如,把第二点放低一点。这段代码非常混乱。我认为你应该重新开始。它看起来像是一段代码,你写了一些相当合理的东西开始,但当它不起作用时,你只是添加了越来越多的东西,使它越来越混乱。但是这里有一个线索,当你想让事情发生不止一次时,你把它放在一个l面向对象。在代码中,您在while循环中提问,但在while循环中没有向向量添加任何内容。因此,您不会向向量添加多个值。
X: 1,Y: 1
X: 2,Y: 2
X: 3,Y: 3
Point mypt;
while (options == "yes") {
mypt.AddCoords(x, y);
// read more coords/options
}
// do stuff on filled mypt object
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;
}
#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;
}