I'm a beginner at programming, I checked other questions like mine but I didn't understand anything.
Sadly, I have a code and in that code I created a class. With the help of methods in the class, I'm changing some variables that i created in the class. But when i try to print them out, i can't. Maybe I'm doing something wrong but anyway i need help, My code doesn't show any kind of syntax errors, but when i run the code with F5, console just shuts down and i can't see the updated variables. Instead my debug window has lots of missing file stuff.
( I tried return player.x; kind of things too, same thing happens)
#include <iostream>
using namespace std;
class Entity {
public:
int x, y, speed;
auto Move(int a, int b , int speed)
{
x += a * speed;
y += b * speed;
}
};
int main()
{
Entity player;
player.Move(1, -1, 4);
cout << player.x << endl;
cout << player.y << endl;
}
Adding getchar(); or Sleep(5000); or system("pause") type of things can keep my console open, but there is another problem now, i played with low numbers but it printed out something around negative 800 thousand. Thanks for the replies, and i didnt get the undefined behaviour thing. Are you saying that i can't access a variable in main that i created in class "entity"?
Problems are all sorted out thanks to all of you. You shouldn't leave any variables undefined if you are going to play with them, i added some values to them and Now i get 4 and -4 as x and y coordinates, not something around 800 thousand or million stuff. (x and y 0, and speed 1 if any newbies like me is interested).