Here I have an array within the private section of my class which is unable to retain the values set in different members of the clas; during the fight sequence both of the values in the array equate to 0.
#include <iostream>
#include <string>
#include<math.h>
#include<cstdlib>
using namespace std;
class champions {
private:
int health_array[2] = { };
int attack_array[2] = { };
public:
void health_attack_set() {
int health_set{};
int attack_set{};
for (int i = 0; i < 2; i++) {
health_array[i] = health_set;
attack_array[i] = attack_set;
}
}
void fight_sequence(int random_champion, int random_opponent) {
cout << "FIGHT\n\n";
while (health_array[random_champion] > 0 or health_array[random_opponent] > 0) {
(health_array[random_opponent] -= attack_array[random_champion]);
if (health_array[random_opponent] <= 0) {
break;
}
(health_array[random_champion] -= attack_array[random_opponent]);
}
if (health_array[random_champion] > 0) {
cout << "CHAMPION 1 WINS!!!";
}
if (health_array[random_opponent] > 0) {
cout << "CHAMPION 2 WINS!!!";
}
if (health_array[random_champion] == 0 && health_array[random_opponent] == 0) {
cout << "NO ONE WINS!!";
}
}
void champion_1() {
health_attack_set();
health_array[0] = 400;
attack_array[0] = 150;
}
void champion_2() {
health_attack_set();
health_array[1] = 500;
attack_array[1] = 100;
}
};
int main() {
champions fight;
fight.fight_sequence(0, 1);
return 0;
}
I believe it may be a simple mistake but hard to spot; thank you for any help that is given.
I think your problems are coming from not understanding how to structure your code in the presence of classes and multiple objects. I have restructured your code below in a cleaner and more common way (it is not the only way to do it, but it is more typical). Hopefully there are not typos.
#include <iostream>
#include <string>
#include<math.h>
#include<cstdlib>
using namespace std;
class Champion{
public:
int health;
int attack;
Champion(int health_, int attack_) : health(health_), attack(attack_) {
}
};
void fight_sequence(Champion& champion, Champion& opponent) {
cout << "FIGHT\n\n";
while (champion.health > 0 || opponent.health > 0) {
(opponent.health -= champion.attack);
if (opponent.health <= 0) {
break;
}
(champion.health -= opponent.attack);
}
if (champion.health > 0) {
cout << "CHAMPION 1 WINS!!!";
}
if (opponent.health > 0) {
cout << "CHAMPION 2 WINS!!!";
}
if (champion.health == 0 && opponent.health == 0) {
cout << "NO ONE WINS!!";
}
}
int main() {
Champion champion_1(400, 150);
Champion champion_2(500, 100);
fight_sequence(champion_1, champion_2);
return 0;
}
Related
I'm trying to print the structure in an array whose int prejetih member is highest.
#include <iostream>
using namespace std;
struct Tekma {
int prejetih;
};
Tekma najvec_kosev(Tekma tekme[]) {
int maksi = 0, index = 0;
for (int i = 0;i < 2;i++) {
if (maksi < tekme[i].prejetih) {
index = i;
maksi = tekme[i].prejetih;
}
}
return tekme[index];
}
void vpis(Tekma tekme[]) {
for (int i = 0;i < 2;i++)
cin >> tekme[i].prejetih;
}
int main() {
Tekma tekme[2];
vpis(tekme);
cout << najvec_kosev(tekme);
}
The compiler reports
C++ no operator matches these operands
operand types are: std::ostream << Tekma
over cout << najvec_kosev(tekme);
Here using a solution with std::vector and fixing your cout problem:
#include <iostream>
#include <vector>
using namespace std;
struct Tekma {
int prejetih;
};
Tekma najvec_kosev(vector<Tekma>& tekme) {
Tekma lowest = tekme[0]
for(auto& t : tekme) {
if(lowest.prejetih < t.prejetih) {
lowest = t;
}
}
return lowest ;
}
void vpis(vector<Tekma>& tekme) {
int input;
while(true) {
cin >> input;
// Check if the input is valid else quit the loop.
if(input == valid) {
Tekma newStruct = {input};
tekme.push(newStruct);
}
else {
// Stop the loop
break;
}
}
}
int main() {
vector<Tekma> tekme;
vpis(tekme);
cout << najvec_kosev(tekme).prejetih; // This fixes your error.
}
Visual studio said I got no issue but, every time I try to run my code I get an assertion failure error, and the error says that vector subscript is out of range, what should I do to fix this, I dont really know what I am doing wrong.
#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
class Human
{
private:
int ap;
int hp;
public:
Human()
{
srand(time(NULL));
ap = rand() % 3 + 8;
hp = rand() % 2 + 9;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class Skeleton
{
private:
int ap;
int hp;
public:
Skeleton()
{
srand(time(NULL));
ap = rand() % 3 + 3;
hp = rand() % 2 + 4;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class game
{
private:
std::vector<Human*> hum;
std::vector<Skeleton*> ske;
bool adv;
int sC;
int hC;
public:
game(int h, int s)
{
srand(time(NULL));
adv = rand() % 2;
sC = s;
hC = h;
for (int i = 0; i < h; i++) { hum.push_back( new Human()); }
for (int i = 0; i < h; i++) { ske.push_back(new Skeleton()); }
}
~game()
{
for (int i = 0; i < hum.size(); i++)
{
Human* current = hum.back();
hum.pop_back();
delete current;
}
for (int i = 0; i < ske.size(); i++)
{
Skeleton* current = ske.back();
ske.pop_back();
delete current;
}
}
void start()
{
int x = hC-1;
int y = sC-1;
bool quit = false;
while (!quit)
{
if (adv)
{
ske[y]->dmg(hum[x]->getAp());
if (ske[y]->getHp() <= 0) { y--; ske.pop_back();}
adv = 0;
}
if (!adv)
{
hum[x]->dmg(ske[y]->getAp());
if (hum[x]->getHp() <= 0) { x--; hum.pop_back(); }
adv = 1;
}
if (hum.size() == 0 || ske.size() == 0)
{
cout << "human left : " << hum.size() << "skeleton left : " << ske.size();
quit = true;
}
}
}
};
int main()
{
game g1(10, 5);
g1.start();
return 0;
}
I always get this error box.
this is the error message i got no idea what are they talkin about.
I also get this, what is this?
this
ive fixed it thx to all of ur comments,
this is the completed code, it may look like garbage but it runs
#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
class Human
{
private:
int ap;
int hp;
public:
Human()
{
ap = rand() % 3 + 8;
hp = rand() % 2 + 9;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class Skeleton
{
private:
int ap;
int hp;
public:
Skeleton()
{
ap = rand() % 3 + 3;
hp = rand() % 2 + 4;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class game
{
private:
std::vector<Human*> hum;
std::vector<Skeleton*> ske;
bool adv;
int sC;
int hC;
public:
game(int h, int s)
{
srand(time(NULL));
adv = rand() % 2;
sC = s;
hC = h;
for (int i = 0; i < h; i++) { hum.push_back( new Human()); }
for (int i = 0; i < s; i++) { ske.push_back(new Skeleton()); }
}
~game()
{
for (int i = 0; i < hum.size(); i++)
{
Human* current = hum.back();
hum.pop_back();
delete current;
}
for (int i = 0; i < ske.size(); i++)
{
Skeleton* current = ske.back();
ske.pop_back();
delete current;
}
}
void start()
{
int x = hum.size()-1;
int y = ske.size()-1;
bool quit = false;
while (!quit)
{
if (hum.size() == 0 || ske.size() == 0)
{
cout << "human left : " << hum.size() << "skeleton left : " << ske.size();
quit = true;
break;
}
if (adv)
{
ske.at(y)->dmg(hum.at(x)->getAp());
if (ske.at(y)->getHp() <= 0) { y--; ske.pop_back();}
adv = 0;
}
else if (!adv)
{
hum.at(x)->dmg(ske.at(y)->getAp());
if (hum.at(x)->getHp() <= 0) { x--; hum.pop_back(); }
adv = 1;
}
}
}
};
int main()
{
game g1(8, 20);
g1.start();
return 0;
}
Your final code is somewhat complex than it should be, I have simplified it by:
Remove the windows related header files, they are not used
Use member initializer lists
Modify dmg to return the latest hp
Remove the unnecessary index in start, to use back
Remove the vector of pointers, it's Ok to use objects here
Move the srand call into main(Actually we can use std::random here)
Simplify the while loop in start
#include <ctime>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
class Human {
private:
int ap;
int hp;
public:
Human() : ap(rand() % 3 + 8), hp(rand() % 2 + 9) {}
int getAp() { return ap; }
int getHp() { return hp; }
int dmg(int x) { return hp -= x; }
};
class Skeleton {
private:
int ap;
int hp;
public:
Skeleton() : ap(rand() % 3 + 3), hp(rand() % 2 + 4) {}
int getAp() { return ap; }
int getHp() { return hp; }
int dmg(int x) { return hp -= x; }
};
class game {
private:
bool adv;
int sC;
int hC;
std::vector<Human> hum;
std::vector<Skeleton> ske;
public:
game(int h, int s) : adv(rand() % 2), sC(s), hC(h), hum(h), ske(s) {}
void start() {
while (!hum.empty() && !ske.empty()) {
if (adv) {
if (ske.back().dmg(hum.back().getAp()) <= 0) {
ske.pop_back();
}
} else {
if (hum.back().dmg(ske.back().getAp()) <= 0) {
hum.pop_back();
}
}
adv = !adv;
}
cout << "human left : " << hum.size() << ", skeleton left : " << ske.size();
}
};
int main() {
srand(time(NULL));
game g1(8, 20);
g1.start();
return 0;
}
Online demo
In function start, ske[y] is accessed with a negative index. std::vector does not like negative indexes, this needs to be fixed.
I've been trying to solve this task, but I had no positive results.
So, my task is to check if a string is a base 16 number.
example : s="1AB", it will show YES 427
Here is my code.
#include <iostream>
#include <string.h>
using namespace std;
int power (int a, int b)
{
if(b==1) return a;
else return a*power(a,b-1);
}
void conv(char s[],int &n)
{
int S=0,i,p=0;
for(i=n-1;i>=0;i--)
{
if(s[i]>='0' && s[i]<='9')
S+=(s[i]-48) * power(16,p); //ex:s[i]='1' ==> S+=(49-49)*...
else S+=(s[i]-55) * power(16,p); //s[i]='A' ==> S+=(65-55) *...
p++;
}
}
int main()
{
int n,i,k=0;
char s[255];
cin.get(s,255);
cin.get();
n=strlen(s);
for(i=0;i<n;i++)
{
if(strchr("0123456789ABCDEF",s[i])) k++;
}
if(k==0) cout<<"not in base 16";
else{
conv(s,n); cout<<s;}
return 0;
}
If you want almost clean solution, you can check mine:
#include <iostream>
using namespace std;
bool IsHex(string& in) {
for (char d : in) {
if (!isxdigit(d)) return false;
}
return true;
}
int Convert(string& in) {
int val = 0;
for (char d : in) {
val = val * 16 + (isdigit(d)? d - '0' : 10 + (isupper(d)? d - 'A' : d - 'a'));
}
return val;
}
int main() {
string in;
cin >> in;
if (!IsHex(in)) cout << "Not a correct hex number" << endl;
else cout << "YES " << Convert(in) << endl;
return 0;
}
And I have made some changes in your code to make it work. You can easily find out changes.
#include <iostream>
#include <string.h>
using namespace std;
int power (int a, int b)
{
if (b == 0) return 1;
if(b==1) return a;
else return a*power(a,b-1);
}
int conv(char s[],int &n)
{
int S=0,i,p=0;
for(i=n-1;i>=0;i--)
{
if(s[i]>='0' && s[i]<='9')
S+=(s[i]-48) * power(16,p); //ex:s[i]='1' ==> S+=(49-49)*...
else S+=(s[i]-55) * power(16,p); //s[i]='A' ==> S+=(65-55) *...
p++;
}
return S;
}
int main()
{
int n,i,k=0;
char s[255];
cin.get(s,255);
cin.get();
n=strlen(s);
for(i=0;i<n;i++)
{
if(!strchr("0123456789ABCDEF",s[i])) break;
}
if(i < n) cout<<"not in base 16";
else{
cout << conv(s,n) << endl;}
return 0;
}
You can just use std::isxdigit, for example:
#include <cctype>
#include <string>
#include <iostream>
bool IsThisStringAHexNumber(std::string const &str)
{
for (size_t i = 0, n = str.length(); i < n; ++i)
if (!std::isxdigit(str[i]))
return false;
return true;
}
int main()
{
std::cout << std::boolalpha << IsThisStringAHexNumber("298722h2jjh") << std::endl;
std::cout << std::boolalpha << IsThisStringAHexNumber("2abc66f") << std::endl;
return 0;
}
Prints:
false
true
I have been getting outputs and sometimes even getting the complete output, however I don't quite understand why I'd be getting said outputs. I want to believe it has something to do with my overloading of said operator "<<" when I perform the operation of outputting the information in the "OpenSea" object. Any leads would be tremendously helpful and thank you for your time!
Note: I included only the necessary files but will update if you think you need more to understand the full scope of my situation. Also there is no need to give me tips on how to format my code as I know a lot of it can be reworked for optimization.
main.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
#include "aquaSea.h"
using namespace std;
int main ()
{
srand (time (NULL));
// Simulation
Sea OpenSea (17, 17);
cout << OpenSea;
}
aquaClasses.h <-- "main" header file
#ifndef AQUACLASSES_H
#define AQUACLASSES_H
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iomanip>
// Constants
const short int AMT_FISH = 200;
const short int AMT_KWHALES = 2;
const short int AMT_PENGS = 50;
const short int MAX_SIM_INTERATIONS = 10000;
const short int MAX_SEA_GRID = 25;
const short int MAX_SEA_SIZE = 625;
// Functions
template <typename TYPE> int calculate_distance (int obj1_x, int obj1_y,
TYPE obj2);
template <typename TYPE> int calculate_direction (int obj1_x, int obj1_y,
TYPE obj2);
#endif
aquaSea.h
#ifndef AQUASEA_H
#define AQUASEA_H
// Forward Declarations to circumvent circular dependency
class Fish;
class Penguin;
class Killer_Whale;
class Sea
{
private:
char m_grid [MAX_SEA_GRID][MAX_SEA_GRID];
int m_fish;
int m_kwhales;
int m_pengs;
int m_size;
int m_height;
int m_width;
void clear ();
void populate ();
public:
Sea (const int grid_size_height, const int grid_size_width);
friend std::ostream &operator<<(std::ostream &os, Sea obj);
int getHeight () const {return m_height;};
int getWidth () const {return m_width;};
void setCell (int obj_x, int obj_y, char cell_symbol);
// Appropriate accessor/mutator functions
};
#endif
aquaSea.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaPenguin.h"
#include "aquaKillerWhale.h"
#include "aquaSea.h"
using namespace std;
// - [row][column]
Sea::Sea (const int grid_size_height, const int grid_size_width)
{
m_fish = 200;
m_kwhales = 2;
m_pengs = 50;
if ((grid_size_height || grid_size_width) <= 0)
{
if (grid_size_height <= 0 && grid_size_width > 0)
{
m_size = MAX_SEA_GRID * grid_size_width;
m_width = grid_size_width;
m_height = MAX_SEA_GRID;
}
else if (grid_size_width <= 0 && grid_size_height > 0)
{
m_size = MAX_SEA_GRID * grid_size_height;
m_width = MAX_SEA_GRID;
m_height = grid_size_height;
}
else if (grid_size_height <= 0 && grid_size_width <= 0)
{
m_size = MAX_SEA_GRID * MAX_SEA_GRID;
m_height = MAX_SEA_GRID;
m_width = MAX_SEA_GRID;
}
}
else
{
m_size = grid_size_height * grid_size_width;
m_height = grid_size_height;
m_width = grid_size_width;
}
clear ();
populate ();
}
ostream &operator<<(ostream &os, Sea obj)
{
os << "Sea Height: " << obj.m_width << endl << "Sea Wdith: "
<< obj.m_width << endl << "Sea Size: " << obj.m_size
<< endl;
os << "Grid View: " << endl;
for (int i = 0; i < obj.m_height; i++)
{
for (int j = 0; j < obj.m_width; j++)
{
os << obj.m_grid [i][j];
}
os << endl;
}
return os;
}
void Sea::clear ()
{
for (int i = 0; i < m_height; i++)
{
for (int j = 0; j < m_width; j++)
{
m_grid[i][j] = 'O';
}
}
return;
}
void Sea::populate ()
{
// Special simluation variables
bool applyFish = true;
bool applyKWhales = false;
bool applyPengs = false;
int amtFishPop = 35;
int amtKWhalesPop = 2;
int amtPengPop = 20;
int index = 0;
int randGridRow = 0;
int randGridCol = 0;
int totalPop = amtFishPop + amtKWhalesPop + amtPengPop;
Fish arr_fish [AMT_FISH];
Killer_Whale arr_kwhales [AMT_KWHALES];
Penguin arr_pengs [AMT_PENGS];
for (int i = 0; i < totalPop; i++)
{
// Grab random place on grid to apply and check if grid plot is open
randGridRow = rand () % 16;
randGridCol = rand () % 16;
while (m_grid [randGridRow][randGridCol] != 'O')
{
randGridRow = rand () % 16;
randGridCol = rand () % 16;
}
// Populate and Update Fish
if (amtFishPop > 0 && applyFish == true)
{
arr_fish[index].setX (randGridCol);
arr_fish[index].setY (randGridRow);
setCell (arr_fish[index].getX (), arr_fish[index].getY (), 'F');
amtFishPop--;
index++;
}
else if (amtFishPop == 0)
{
applyFish = false;
applyKWhales = true;
index = 0;
}
// Populate and Update Killer Whales
if (amtKWhalesPop > 0 && applyKWhales == true)
{
arr_kwhales[index].setX (randGridCol);
arr_kwhales[index].setY (randGridRow);
setCell (arr_kwhales[index].getX (), arr_kwhales[index].getY (), 'K');
amtKWhalesPop--;
index++;
}
else if (amtKWhalesPop == 0)
{
applyKWhales = false;
applyPengs = true;
index = 0;
}
// Populate and Update Penguins
if (amtPengPop > 0 && applyPengs == true)
{
arr_pengs[index].setX (randGridCol);
arr_pengs[index].setY (randGridRow);
setCell (arr_pengs[index].getX (), arr_pengs[index].getY (), 'P');
amtPengPop--;
index++;
}
else if (amtPengPop == 0)
{
applyPengs = false;
index = 0;
}
}
return;
}
void Sea::setCell (int obj_x, int obj_y, char cell_symbol)
{
m_grid [obj_x][obj_y] = cell_symbol;
return;
}
Types of Output
Wanted:
- Image
Unwanted:
- Image
STOP! AFTER THIS POINT IS OPTIONAL CODE TO FURTHER UNDERSTAND THE SITUATION
Other Code Of Reference If You Want But I Don't Think It's Needed
aquaFish.h
#ifndef AQUAFISH_H
#define AQUAFISH_H
class Fish
{
private:
int m_fish_amt_food;
int m_fish_x;
int m_fish_y;
bool m_fish_alive;
public:
Fish ();
int getX () const {return m_fish_x;};
int getY () const {return m_fish_y;};
void setX (int new_x) {m_fish_x = new_x;};
void setY (int new_y) {m_fish_y = new_y;};
int getFishAmtFood () const {return m_fish_amt_food;};
void move ();
};
#endif
aquaFish.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
using namespace std;
Fish::Fish ()
{
int randNum = rand () % 10 + 1;
m_fish_amt_food = randNum;
m_fish_x = -1;
m_fish_y = -1;
}
void Fish::move ()
{
int randDir = rand () % 8 + 1;
if (randDir == 1)
{
m_fish_y++;
}
else if (randDir == 2)
{
m_fish_x++;
m_fish_y++;
}
else if (randDir == 3)
{
m_fish_x++;
}
else if (randDir == 4)
{
m_fish_x++;
m_fish_y--;
}
else if (randDir == 5)
{
m_fish_y--;
}
else if (randDir == 6)
{
m_fish_x--;
m_fish_y--;
}
else if (randDir == 7)
{
m_fish_x--;
}
else if (randDir == 8)
{
m_fish_x--;
m_fish_y++;
}
return;
}
aquaPenguin.h
#ifndef AQUAPENGUIN_H
#define AQUAPENGUIN_H
// Forward Declarations to circumvent circular dependancy
class Sea;
class Fish;
class Killer_Whale;
class Penguin
{
private:
int m_peng_health; // 0-100
int m_peng_x;
int m_peng_y;
bool m_peng_alive;
public:
Penguin ();
int getX () const {return m_peng_x;};
int getY () const {return m_peng_y;};
void setX (int new_x) {m_peng_x = new_x;};
void setY (int new_y) {m_peng_y = new_y;};
void move (Sea obj_sea, Fish arr_fish [], int arr_fish_size,
Killer_Whale arr_kwhale [], int arr_kwhale_size);
};
#endif
aquaPenguin.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
#include "aquaSea.h"
#include "aquaFunctions.cpp"
using namespace std;
Penguin::Penguin ()
{
m_peng_health = rand () % (81 - 60) + 60;
m_peng_x = -1;
m_peng_y = -1;
m_peng_alive = false;
}
void Penguin::move (Sea obj_sea, Fish arr_fish [], int arr_fish_size,
Killer_Whale arr_kwhale [], int arr_kwhale_size)
{
int actuDistToFish = 8;
int currDistToFish = 0;
int tempMoveX = 0;
int tempMoveY = 0;
int amtMove = 0;
int direction = 0;
int fishIndex = 0;
bool moveAwayKillerWhale = false;
bool fishInRange = false;
// Determine amount of cells to move in sea
if (m_peng_health >= 81 && m_peng_health <= 100)
{
amtMove = 5;
}
else if (m_peng_health >= 61 && m_peng_health <= 80)
{
amtMove = 4;
}
else if (m_peng_health >= 41 && m_peng_health <= 60)
{
amtMove = 3;
}
else if (m_peng_health >= 21 && m_peng_health <= 40)
{
amtMove = 2;
}
else if (m_peng_health >= 1 && m_peng_health <= 20)
{
amtMove = 1;
}
else
{
cout << "Chicken of the sea at: " << m_peng_x << " " << m_peng_y
<< endl;
return;
}
// ADD: Find if any killer whales are near first and if so then penguin just moves away
// Find if any fish are near <-- THIS IS WRONG, YOU NEED TO FIND THE CLOSEST FISH
for (int i = 0; i < arr_fish_size; i++)
{
currDistToFish = calculate_distance (m_peng_x, m_peng_y,
arr_fish[i]);
if (currDistToFish <= 8)
{
if (currDistToFish < actuDistToFish)
{
actuDistToFish = currDistToFish;
fishIndex = i;
fishInRange = true;
}
}
}
// ADD: If fish and whale are found do something, we decide. Otherwise move randomly Peng See Dist 8.0
// ADD Move 1 tick then gauge situation again
for (int k = 0; k < amtMove; k++)
{
if (fishInRange == true && moveAwayKillerWhale == false)
{
tempMoveX = m_peng_x; // temp used for storing before changing
tempMoveY = m_peng_y; // temp used for storing before changing
direction = calculate_direction (m_peng_x, m_peng_y,
arr_fish[fishIndex]);
cout << "Penguin pos before moving: " << m_peng_x << ","
<< m_peng_y << endl;
cout << "Closest Fish pos: " << arr_fish[fishIndex].getX () << ","
<< arr_fish[fishIndex].getY () << endl;
if (m_peng_health == 0)
{
cout << "Chicken of the sea at: " << m_peng_x << " " << m_peng_y
<< endl;
return;
}
if (direction == 1)
{
actuDistToFish--;
m_peng_health--;
tempMoveY++;
}
else if (direction == 2)
{
actuDistToFish--;
m_peng_health--;
tempMoveX++;
tempMoveY++;
}
else if (direction == 3)
{
actuDistToFish--;
m_peng_health--;
tempMoveX++;
}
else if (direction == 4)
{
actuDistToFish--;
m_peng_health--;
tempMoveX++;
tempMoveY--;
}
else if (direction == 5)
{
actuDistToFish--;
m_peng_health--;
tempMoveY--;
}
else if (direction == 6)
{
actuDistToFish--;
m_peng_health--;
tempMoveX--;
tempMoveY--;
}
else if (direction == 7)
{
actuDistToFish--;
m_peng_health--;
tempMoveX--;
}
else if (direction == 8)
{
actuDistToFish--;
m_peng_health--;
tempMoveX--;
tempMoveY++;
}
else
{
cout << "[ERROR]: Penguin direction messed up." << endl;
}
// MODIFY: Lastly check if out of bounds and then move peng
if (tempMoveX > obj_sea.getWidth ()
|| tempMoveX < -(obj_sea.getWidth ()))
{
m_peng_x = m_peng_x; // AKA dont move
m_peng_y = m_peng_y;
}
else if (tempMoveY > obj_sea.getHeight ()
|| tempMoveY < -(obj_sea.getHeight ()))
{
m_peng_x = m_peng_x; // AKA dont move
m_peng_y = m_peng_y;
}
else
{
obj_sea.setCell (m_peng_x, m_peng_y, 'O'); // Delete old cell
m_peng_x = tempMoveX;
m_peng_y = tempMoveY;
obj_sea.setCell (m_peng_x, m_peng_y, 'P'); // Set new cell
}
// Check if peng eats after moving
if (actuDistToFish == 0)
{
// Stop moving
amtMove = 0;
// Eat fish
m_peng_health += arr_fish[fishIndex].getFishAmtFood ();
// ADD: Remove fish from grid
}
cout << "Penguin pos after moving: " << m_peng_x << ","
<< m_peng_y << endl;
}
else if (fishInRange == false && moveAwayKillerWhale == true)
{
}
else if (fishInRange == false && moveAwayKillerWhale == false)
{
// If no fish, movement is random, else it's towards fish how ever many
// step the penguin can go
direction = rand () % 8 + 1;
if (direction == 1)
{
m_peng_y++;
}
else if (direction == 2)
{
m_peng_x++;
m_peng_y++;
}
else if (direction == 3)
{
m_peng_x++;
}
else if (direction == 4)
{
m_peng_x++;
m_peng_y--;
}
else if (direction == 5)
{
m_peng_y--;
}
else if (direction == 6)
{
m_peng_x--;
m_peng_y--;
}
else if (direction == 7)
{
m_peng_x--;
}
else if (direction == 8)
{
m_peng_x--;
m_peng_y++;
}
else
{
cout << "[ERROR]: Penguin random direction messed up." << endl;
}
}
}
return;
}
aquaKillerWhale.h
#ifndef AQUAKILLERWHALE_H
#define AQUAKILLERWHALE_H
class Penguin;
class Killer_Whale
{
private:
int m_kwhale_amt_pengs;
int m_kwhale_x;
int m_kwhale_y;
public:
Killer_Whale ();
int getX () const {return m_kwhale_x;};
int getY () const {return m_kwhale_y;};
void setX (int new_x) {m_kwhale_x = new_x;};
void setY (int new_y) {m_kwhale_y = new_y;};
void move (Penguin arr_peng []);
};
#endif
aquaKillerWhale.cpp
#include "aquaClasses.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
using namespace std;
Killer_Whale::Killer_Whale ()
{
m_kwhale_x = -1;
m_kwhale_y = -1;
}
void Killer_Whale::move (Penguin arr_peng [])
{
return;
}
aquaFunctions.cpp
#include "aquaClasses.h"
using namespace std;
// Functions
template <typename TYPE>
int calculate_direction (int obj1_x, int obj1_y, TYPE obj2)
{
int calculatedX = obj2.getX () - obj1_x;
int calculatedY = obj2.getY () - obj1_y;
int direction = 0;
if (calculatedX == 0 && calculatedY > 0)
{
direction = 1;
}
else if (calculatedX > 0 && calculatedY > 0)
{
direction = 2;
}
else if (calculatedX > 0 && calculatedY == 0)
{
direction = 3;
}
else if (calculatedX > 0 && calculatedY < 0)
{
direction = 4;
}
else if (calculatedX == 0 && calculatedY < 0)
{
direction = 5;
}
else if (calculatedX < 0 && calculatedY < 0)
{
direction = 6;
}
else if (calculatedX < 0 && calculatedY == 0)
{
direction = 7;
}
else if (calculatedX < 0 && calculatedY > 0)
{
direction = 8;
}
else
{
cout << "[ERROR]: Direction calculation failed." << endl;
}
return direction;
}
template <typename TYPE>
int calculate_distance (int obj1_x, int obj1_y, TYPE obj2)
{
int distance = sqrt ((obj1_x - obj2.getX ())
* (obj1_x - obj2.getX ())
+ (obj1_y - obj2.getY ())
* (obj1_y - obj2.getY ()));
return distance;
}
I am trying to build the below code in Visual Studio and get the following error in the last 10 lines: Error: Expression must have pointer-to-object type.
Can somebody take a look and tell me what is missing here.
Here is the code:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct emptyPeak
{
unsigned int t;
int accel;
}peaks[10];
struct ts0
{
int dt;
int N;
float NValid;
double t0;
int data[36];
int v[36];
};
struct emptyPeakConstant
{
int minSeparation_ms;
int minLocalRange;
float minRangeFraction;
int localRangeHalfWidth_ms;
};
int findAccExtrema()
{
int tStart = 10, tEnd = 5;
unsigned int candidate_t;
int type = 0;
int NPeaks=2;
int n, m, L;
struct emptyPeak peaks;
struct emptyPeakConstant pc;
struct ts0 ts0copy;
pc.minSeparation_ms = 325;
pc.minLocalRange=696;
pc.minRangeFraction=0.62;
pc.localRangeHalfWidth_ms=250;
if(ts0copy.dt==0 || ts0copy.N<3 || ts0copy.NValid<3)
{
return 0;
}
int findMaxima;
if(type == 0 || type == 2)
{
findMaxima = 1;
}
else
{
findMaxima=0;
}
int findMinima;
if(type == 1 || type == 2)
{
findMinima = 1;
}
else
{
findMinima=0;
}
L=round(pc.localRangeHalfWidth_ms/ts0copy.dt);
int minAcc=ts0copy.data[0];
int maxAcc=ts0copy.data[0];
int mnAcc=0;
int K=0;
for(n=0;n<=ts0copy.N-1;n++)
{
if(~ts0copy.v[n])
{
continue;
}
if(ts0copy.data[n] > maxAcc)
{
maxAcc=ts0copy.data[n];
}
if(ts0copy.data[n] < minAcc)
{
minAcc=ts0copy.data[n];
}
mnAcc=mnAcc+ts0copy.data[n];
K=K+1;
}
if(K==0)
{
return 0; // Instead of return in the matlab code
}
mnAcc=mnAcc/K;
float thresholdMaxima = minAcc + pc.minRangeFraction*(maxAcc - minAcc);
float thresholdMinima = maxAcc - pc.minRangeFraction*(maxAcc - minAcc);
for(n=1;n<=ts0copy.N-2;n++)
{
candidate_t=ts0copy.t0 + n*ts0copy.dt;
if(candidate_t<tStart || candidate_t > tEnd || ~ts0copy.v[n])
{
continue;
}
int hasMaxima;
if(findMaxima && (ts0copy.data[n] >= thresholdMaxima) && (ts0copy.data[n] >=ts0copy.data[n-1]) && (ts0copy.data[n] >= ts0copy.data[n+1]))
{
hasMaxima = 1;
}
else
{
hasMaxima = 0;
}
int hasMinima;
if(findMinima && (ts0copy.data[n] <= thresholdMinima) && (ts0copy.data[n] <=ts0copy.data[n-1]) && (ts0copy.data[n] <= ts0copy.data[n+1]))
{
findMinima = 1;
}
else
{
findMinima = 0;
}
if(!hasMaxima && !hasMinima)
{
continue;
}
int maxDelta = 0;
for(m=n-L;m<=n+L;m++)
{
if(m<0 || m>(ts0copy.N-1))
{
continue;
}
unsigned int delta = abs(ts0copy.data[m] - ts0copy.data[n]);
if(ts0copy.v[m] && delta > maxDelta)
{
maxDelta=delta;
}
}
if(maxDelta < pc.minLocalRange)
{
continue;
}
if(NPeaks == 0 || (candidate_t - peaks[NPeaks-1].t) > pc.minSeparation_ms)
{
NPeaks=NPeaks+1;
}
else if(abs(ts0copy.data[n]-mnAcc) < abs(peaks[NPeaks-1].accel-mnAcc))
{
continue;
}
}
peaks[NPeaks-1].t = candidate_t;
peaks[NPeaks-1].accel=ts0copy.data[n];
return NPeaks;
}
There are a few issues in the code (without necessarily resolving the actual issue).
1) did you really want to do
struct emptyPeak
{
unsigned int t;
int accel;
}peaks[10];
?
or did you mean
struct emptyPeak
{
unsigned int t;
int accel;
peaks[10];
}
2) if you did declare the above struct correctly, then did you also mean to declare a variable inside of your function findAccExtrema() as such:
struct emptyPeak peaks;
because now you are operating on a struct, that is NOT an array, as if it is an array. This is probably why you are seeing the issue that you are.