我的程序有什么问题?我想保存坐标值,但每次循环后它都会重置为 0,0
What's wrong with my program? I want the coordinate values to be saved but it keeps resetting to 0,0 after every loop
我的程序有什么问题?我希望保存坐标值,但在每次 loop.if 运行我的主文件后,它会一直重置为 0,0 并在同一文件夹中使用 .h,您会看到坐标值在循环完成后重置.非常沮丧。
我的 .h 文件
不保存的部分最有可能在 move() 函数中
非常感谢 :)
我什么都试过了,是不是哪里做错了?
using namespace std;
#include <string>
#define FORWARD 0
#define BACKWARD -1
#define RIGHT 2
#define LEFT -2
#define t true
class Player
{
public:
//x pos
float x;
//y pos
float y;
//id of player
string id;
};
string d;
string mv;
// Making a new player
Player p;
int direction;
void turnForward()
{
direction = FORWARD;
}
void turnBackward()
{
direction = BACKWARD;
}
void turnRight()
{
direction = RIGHT;
}
void turnLeft()
{
direction = LEFT;
}
int spaces;
void move(int spaces, int direction, float x, float y, Player p)
{
x = p.x;
y = p.y;
if(direction == LEFT)
{
x = x - spaces;
cout << x << ","<< y << endl;
}
p.x = x;
if(direction == RIGHT)
{
x = x + spaces;
cout << x << ","<< y << endl;
}
p.x = x;
if(direction == FORWARD)
{
y = y + spaces;
cout << x << ","<< y << endl;
}
p.y = y;
if(direction == BACKWARD)
{
y = y - spaces;
cout << x << ","<< y << endl;
}
p.y = y;
}
我的主文件
#include <iostream>
#import <string>
#include "assets.h"
using namespace std;
void run()
{
while(t)
{
cout << "What direction would you like to turn? Left (L), Right (R), Forward (F), Backward (B) ";
cin >> d;
if(d == "L" || d == "l")
{
turnLeft();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
if(d == "R" || d == "r")
{
turnRight();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
if(d == "F" || d == "f")
{
turnForward();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
if(d == "B" || d == "b")
{
turnBackward();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
}
}
int main(int argc, char *argv[])
{
cout << "Enter in your name (This will be your username): ";
cin >> p.id;
run();
}
主文件调用run()
您是按值而不是按引用传递 p
。所以move()
正在修改播放器的副本。将 move()
更改为:
void move(int spaces, int direction, float x, float y, Player &p)
在您的 move() 方法中,您将 x 和 y 的参数值重置为 p.x 和 p.y。 p 的初始化值在哪里?
每次执行循环时,它都会传递对象 Person 的副本,因此如果您在 move() 方法中更改该对象的任何值,它将不会在 运行() 方法中生效。正如 Barmar 提到的,您必须通过引用传递值或 return 从 move() 方法更新 Person 对象并将其分配回 运行() 方法中的对象 p。
我的程序有什么问题?我希望保存坐标值,但在每次 loop.if 运行我的主文件后,它会一直重置为 0,0 并在同一文件夹中使用 .h,您会看到坐标值在循环完成后重置.非常沮丧。
我的 .h 文件
不保存的部分最有可能在 move() 函数中
非常感谢 :)
我什么都试过了,是不是哪里做错了?
using namespace std;
#include <string>
#define FORWARD 0
#define BACKWARD -1
#define RIGHT 2
#define LEFT -2
#define t true
class Player
{
public:
//x pos
float x;
//y pos
float y;
//id of player
string id;
};
string d;
string mv;
// Making a new player
Player p;
int direction;
void turnForward()
{
direction = FORWARD;
}
void turnBackward()
{
direction = BACKWARD;
}
void turnRight()
{
direction = RIGHT;
}
void turnLeft()
{
direction = LEFT;
}
int spaces;
void move(int spaces, int direction, float x, float y, Player p)
{
x = p.x;
y = p.y;
if(direction == LEFT)
{
x = x - spaces;
cout << x << ","<< y << endl;
}
p.x = x;
if(direction == RIGHT)
{
x = x + spaces;
cout << x << ","<< y << endl;
}
p.x = x;
if(direction == FORWARD)
{
y = y + spaces;
cout << x << ","<< y << endl;
}
p.y = y;
if(direction == BACKWARD)
{
y = y - spaces;
cout << x << ","<< y << endl;
}
p.y = y;
}
我的主文件
#include <iostream>
#import <string>
#include "assets.h"
using namespace std;
void run()
{
while(t)
{
cout << "What direction would you like to turn? Left (L), Right (R), Forward (F), Backward (B) ";
cin >> d;
if(d == "L" || d == "l")
{
turnLeft();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
if(d == "R" || d == "r")
{
turnRight();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
if(d == "F" || d == "f")
{
turnForward();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
if(d == "B" || d == "b")
{
turnBackward();
cout << "Would you like to move " << d << "? Y/N ";
cin >> mv;
if(mv == "Y" || mv == "y")
{
cout << "How many spaces would you like to go " <<d << "? ";
cin >> spaces;
move(spaces, direction, p.x, p.y, p);
}
else
{
run();
}
}
}
}
int main(int argc, char *argv[])
{
cout << "Enter in your name (This will be your username): ";
cin >> p.id;
run();
}
主文件调用run()
您是按值而不是按引用传递 p
。所以move()
正在修改播放器的副本。将 move()
更改为:
void move(int spaces, int direction, float x, float y, Player &p)
在您的 move() 方法中,您将 x 和 y 的参数值重置为 p.x 和 p.y。 p 的初始化值在哪里?
每次执行循环时,它都会传递对象 Person 的副本,因此如果您在 move() 方法中更改该对象的任何值,它将不会在 运行() 方法中生效。正如 Barmar 提到的,您必须通过引用传递值或 return 从 move() 方法更新 Person 对象并将其分配回 运行() 方法中的对象 p。