So I'm trying to make a simple turn based game in terminal. I have a header file, and two cpp files. The idea is to make the game highly expandable as a learning tool.
Anyways, when I printout player.hp, I get 1, when I was expecting 500. The eventual goal is to start player.hp at 500, and troll.hp at 500, and run attacK::number to calculate damage every turn.
#include <iostream>
#include "characters.h"
int main()
{
char select;
character player;
character troll;
std::cout << "Hello, welcome to wizardworld version 1" << std::endl;
std::cout << "there is a troll in the room you are in, engage in combat?" << std::endl;
std::cout << "enter a to begin combat" << std::endl;
std::cin >> select;
if(select=='a')
std::cout << "You have entered combat" << std::endl;
elsereturn 0;
std::cout << "Both your hp, and the troll's hp start at 500, you will take turns attacking each other until one of you runs out" << std::endl;
std::cout << player.hp << std::endl;
srand(time(0));
Die d;
int result = d.roll();
std::cout << result << std::endl;
return 0;
}
in main.cpp, when I cout player.hp I get 1, and I don't get it. I'm new to programming, and this object oriented stuff is hard.
#include <iostream>
#include "characters.h"
int main()
{
char select;
character player;
character troll;
std::cout << "Hello, welcome to wizardworld version 1" << std::endl;
std::cout << "there is a troll in the room you are in, engage in combat?" << std::endl;
std::cout << "enter a to begin combat" << std::endl;
std::cin >> select;
if(select=='a')
std::cout << "You have entered combat" << std::endl;
elsereturn 0;
std::cout << "Both your hp, and the troll's hp start at 500, you will take turns attacking each other until one of you runs out" << std::endl;
std::cout << "the players hp is" << player.hp() << std::endl;
std::cout << "the troll's hp is" << troll.hp() << std::endl;
player.hp();
attack theplayer;
attack thetroll;
std::cout << "to attack enter a" << std::endl;
std::cin >> select;
if(select=='a')
theplayer.number();
std::cout << player.hp() << std::endl;
elsereturn 0;
srand(time(0));
Die d;
int result = d.roll();
std::cout << result << std::endl;
return 0;
}
1 2 3 4
main.cpp: In function ‘int main()’:
main.cpp:29:2: error: ‘else’ without a previous ‘if’
29 | elsereturn 0;
| ^~~~
The if statement only applies to the theplayer.number(). It doesn't apply to the cout statement. The else isn't matched hence the warning. What you really have is: