I am making a rabbit population simulator for a challenge I am doing. I have a map of class objects. I also have a list which contains the keys for the map. Each round of the simulator I want to add more objects to the class, and update my map and list. To do this I wrote a separate "creation" function which will randomly generate new class objects and add them to a map and add the keys to a list.
The problem is when I call the creation function, then iterate through the map using the list it shows the map or list is empty (not sure which). If I iterate through the map before leaving the function it only shows the newest objects. If I move the code for the function into the main function, it works correctly (calling it twice gives me the new objects created in the first iteration of the code, and also the objects created by the second iteration).
I am guessing that a new list or map is being created each time the function is called and is overwriting the old list or map. How can I get the list and map to pass between the main function and the creation function?
Here is my code:
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include <string>
#include <sstream>
#include <array>
#include <time.h>
#include <conio.h>
#include <map>
#include <list>
class Bunny
{
public:
char fname;
int sex, age, color, status;
Bunny(int, int, int, int);
Bunny();
int s() { return (sex); }
int a() { return (age); }
int c() { return(color);}
int st() { return (status);}
int aging(int age) { return (age + 1); }
};
Bunny::Bunny(int s, int a, int c, int st)
{
sex = s;
age = a;
color = c;
status = st;
}
Bunny::Bunny(){}
void creation(std::map<std::string, Bunny> bunnies, std::list<std::string>names, int births);
std::string firstname(int s, int num)
{
std::string name;
if (s == 0)
{
switch (num)
{
case (0) :
name = "Tim";
break;
case (1) :
name = "Tom";
break;
case (2) :
name = "Mark";
break;
case (3) :
name = "Bob";
break;
case (4) :
name = "Rob";
break;
}
}
if (s == 1)
{
switch (num)
{
case (0) :
name = "Suzy";
break;
case (1) :
name = "Linda";
break;
case (2) :
name = "Mary";
break;
case (3) :
name = "Jan";
break;
case (4) :
name = "Julie";
break;
}
}
return (name);
}
void main()
{
int num = rand() % 5;
int n, births = 10;
std::list<std::string>names;
std::map<std::string, Bunny> bunnies;
srand(time(0));
creation(bunnies, names, births);
std::cout << "Number" << "\t" << "Name" << "\t" << "age" << "\t" << "Sex" << "\t" << "Color" << "\t" << "Vampire?" "\n";
n = 0;
for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++)
{
n++;
std::cout << n << "\t";
std::cout << " " << *it;
std::cout << "\t" << bunnies[*it].a() << "\t" << bunnies[*it].s() << "\t" << bunnies[*it].c() << "\t" << bunnies[*it].st() << "\n";
}
creation(bunnies, names, births);
_getch();
}
/*void year()
{
for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++)
{
bunnies[*it].aging(bunnies[*it].a())
}
}*/
void creation(std::map<std::string, Bunny> bunnies,std::list<std::string> names,int births)
{
int n;
for (n = 0; n < births; n++)
{
int num = std::rand() % 5;
char id = (std::rand() % 100) + 20;
int s = std::rand() % 2;
std::string f = firstname(s, num) + '_' + id;
int a = 0;
int c = std::rand() % 5;
int st;
if (rand() % 50 == 43) st = 1; else st = 0;
bunnies[f] = Bunny(s, a, c, st);
names.push_front(f);
//std::cout << f << " " << bunnies[f].a() << " " << bunnies[f].c() << "\n";
}
std::cout << "Number" << "\t" << "Name" << "\t" << "age" << "\t" << "Sex" << "\t" << "Color" << "\t" << "Vampire?" "\n";
n = 0;
for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++)
{
n++;
std::cout << n << "\t";
std::cout << *it;
std::cout << "\t" << bunnies[*it].a() << "\t" << bunnies[*it].s() << "\t" << bunnies[*it].c() << "\t" << bunnies[*it].st() << "\n";
}
}
Your problem is that your main function is passing your map and list by value instead of passing by reference. This means that your creation function is receiving a copy of the existing map/list, rather than a reference to the original one you created. Since it is then only making edits to that copy, any changes it makes will not be reflected in the main function.
Change your creation function from:
void creation(std::map<std::string, Bunny> bunnies, std::list<std::string>names, int births)
to
void creation(std::map<std::string, Bunny>& bunnies, std::list<std::string>& names, int births)
Related
Here is our code for the task we are almost finishing just the last part we are stuck at
"Fastest: 3 trips (1 Van, 3 Mini-lorry, $645) "
we are not sure how to display the values in the bracket we only able to display 3 trips.
Is there a way to also display the values in the bracket stated as well?
we use
int min = *min_element(vTrips.begin(), vTrips.end());
cout << "Fastest: " << min << " trips" << endl;
but this only display the 3 trips.
#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include<algorithm>
using namespace std;
class CTS //cargo transport system
{
int i;
int cargo, lorryprice, vanprice, lorrysize, vansize, allOps;
public:
void set_cargo(int);
void set_lorryprice(int);
void set_vanprice(int);
void set_lorrysize(int);
void set_vansize(int);
};
void CTS::set_cargo(int total_cargo) {
cargo = total_cargo;
}
void CTS::set_lorryprice(int lorryP) {
lorryprice = lorryP;
}
void CTS::set_vanprice(int vanP) {
vanprice = vanP;
}
void CTS::set_lorrysize(int lorryS) {
lorrysize = lorryS;
}
void CTS::set_vansize(int vanS)
{
vansize = vanS;
}
int main()
{
int cargo, lorryprice, vanprice, lorrysize, vansize, options, i, no_lorry, no_van, cost, trips;
ifstream infile;
infile.open("size.txt");
if (infile.is_open()) {
infile >> cargo;
infile >> lorryprice;
infile >> vanprice;
infile >> lorrysize;
infile >> vansize;
}
CTS run;
run.set_cargo(cargo);
run.set_lorryprice(lorryprice);
run.set_vanprice(vanprice);
run.set_lorrysize(lorrysize);
run.set_vansize(vansize);
infile.close();
options = (cargo / lorrysize) + 1;
no_lorry = (cargo / lorrysize);
no_van = (cargo / vansize) + 3;
if (cargo % lorrysize == 0) {
no_van = -3;
}
if (cargo % lorrysize != 0) {
no_van = ((cargo % lorrysize) / 10) - 3;
}
/*it = numbervan.begin();
for (auto ir = numbervan.rbegin(); ir != numbervan.rend(); ++ir) {
cout << *ir << endl;
}*/
vector<int> vCost, vVan, vTrips, vLorry;
vector <int>::iterator it;
for (i = 1; i < options + 1; i++)
{
int numberlorry = no_lorry;
cout << "Option " << i << ":" << endl;
cout << "Number of Mini-Lorries : " << no_lorry-- << endl;
if (no_van >= -3) {
no_van += 3;
}
cout << "Number of Vans : " << no_van << endl;
int numbervan = no_van;
if (numberlorry > numbervan) {
trips = numberlorry;
}
else {
trips = numbervan;
}
cout << "Trips Needed : " << trips << endl;
cost = (numberlorry * lorryprice) + (no_van * vanprice);
cout << "Total Cost : $" << cost << endl;
vCost.push_back(cost);
vLorry.push_back(numberlorry);
vVan.push_back(numbervan);
vTrips.push_back(trips);
}
int counter = vCost.size() - 1;
//std::vector<int>::reverse_iterator ir = vCost.rbegin();
for (i = 1; i < 4; i++) {
//cout << "Lowest #" << i << ": "<<cost<<endl;
cout << "Lowest #" << i << ": $" << vCost[counter] << "(" << vVan[counter] << " Vans, " << vLorry[counter] << " Mini-Lorry, " << vTrips[counter] << " Trips)" << endl;
counter--;
}
int min = *min_element(vTrips.begin(), vTrips.end()); // this line of code we figured out how to
cout << "Fastest: " << min << " trips" << endl; //display the number of trips using algorithm
return 0;
}
Your design is awkward; you create an instance of CTS run; and never use it.
Assuming that you do your calculations right, you need to know at what index you found min. If you store the iterator returned by min_element(), you can get an index by subtracting vTrips.begin() from it. Then the corresponding elements in your vCost, vLorry and vVan vectors will contain the data you want.
However, it would be easier if you define a struct containing your pre-calculated values, and push that into some vector. In that case, all related data is kept together.
I am trying to create a program that prints out a store inventory. I print out the variables from my objects in the getInventory function. However, when I try and print out the entire inventory in the getStore function it returns nothing. I feel like it is an issue with my pointer properly pointing to my array but I am not sure.
#include <iostream>
#include <string>
class Weapon
{
private:
std::string nameWeapon, messageUsed;
bool ownedWeapon;
double weaponCost;
int numberUses;
public:
//Default constructor
Weapon()
{
nameWeapon = "weapon name";
messageUsed = "weapon sound";
weaponCost = 0;
ownedWeapon = false;
numberUses = 1;
}
//Non-Default Constructor
Weapon(std::string nW, std::string mU,double wC,bool oW, int nU )
{
nameWeapon = nW;
messageUsed = mU;
weaponCost = wC;
ownedWeapon = oW;
numberUses = nU;
}
void getInventory()
{
std::cout << nameWeapon << "\t" << messageUsed << "\t" << weaponCost << "\t" << ownedWeapon << "\t" << numberUses << "\n";
}
void getStore(Weapon* weaponArray)
{
std::cout << "Name" << "\t" << "Sound" << "\t" << "Price" << "\t" << "Owned";
for (int i = 0; i < 9; i++)
{
weaponArray[i].getInventory();
}
}
};
int main()
{
Weapon weaponArray[9]{};
weaponArray[0] = Weapon("Broad Sword","Clang!",50,false,3);
weaponArray[1] = Weapon("Champions Sword","Swoosh!",99.99,false,5);
weaponArray[2] = Weapon("Dagger","Ding!",12.25,false,1);
weaponArray[3] = Weapon("Poisin Dagger","Shh",18.50,false,1);
weaponArray[4] = Weapon("Sturdy Dagger","Gronk!",14.75,false,3);
weaponArray[5] = Weapon("Short Bow","T'wang!",35.75,false,3);
weaponArray[6] = Weapon("Champions Bow","Swoop!",90.15,false,5);
weaponArray[7] = Weapon("Champions Axe","Bash!",110.11,false,6);
weaponArray[8] = Weapon("Throwing Axe","Bonk!",25.75,false,2);
void getStore();
system("pause");
return 0;
}
void getStore();
This is a function stub. It declares a function getStore that takes no arguments and returns nothing to be implemented later. This is almost definitely a typo.
Further, because void getStore(Weapon* weaponArray) is in the Weapon class, you have to call it on a weapon, which doesn't look right. You probably mean to do this:
class Weapon {
// ...
};
void getStore(Weapon* weaponArray, unsigned sz)
{
std::cout << "Name" << "\t" << "Sound" << "\t" << "Price" << "\t" << "Owned";
for (unsigned i = 0; i < sz; i++)
{
weaponArray[i].getInventory();
}
}
int main()
{
Weapon weaponArray[9]{};
weaponArray[0] = Weapon("Broad Sword","Clang!",50,false,3);
//...
getStore(weaponArray, 9);
}
getMessage method extracts the first letter in each word of input string.
Example:
input = "Find the first letters of this Sentence"
output = FtflotS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
class HiddenMessage {
public:
bool space (char c) {
return isspace(c);
}
bool not_space (char c) {
return !isspace (c);
}
string getMessage(string text) {
string ret;
typedef string::const_iterator iter;
iter i, j;
i = text.begin();
while (i != text.end()) {
i = find_if (i, text.end(), not_space); // error here
j = find_if (i, text.end(), space); // error here
if (i != text.end()) {
ret += *i;
}
i = j;
}
return ret;
}
};
//compiler error:
//error: invalid use of non-static member function
I tried making definitions of space and not_space static and it did
not work.
getMessage is called from the main below:
#include <ctime>
#include <cmath>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
if (argc == 1)
{
cout << "Testing HiddenMessage (250.0 points)" << endl << endl;
for (int i = 0; i < 20; i++)
{
ostringstream s; s << argv[0] << " " << i;
int exitCode = system(s.str().c_str());
if (exitCode)
cout << "#" << i << ": Runtime Error" << endl;
}
int T = time(NULL)-1456061889;
double PT = T/60.0, TT = 75.0;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << endl;
cout << "Time : " << T/60 << " minutes " << T%60 << " secs" << endl;
cout << "Score : " << 250.0*(.3+(.7*TT*TT)/(10.0*PT*PT+TT*TT)) << " points" << endl;
}
else
{
int _tc; istringstream(argv[1]) >> _tc;
HiddenMessage _obj;
string _expected, _received;
time_t _start = clock();
switch (_tc)
{
case 0:
{
string text = "compete online design event rating";
_expected = "coder";
_received = _obj.getMessage(text); break;
}
case 1:
{
string text = " c o d e r ";
_expected = "coder";
_received = _obj.getMessage(text); break;
}
case 2:
{
string text = "round elimination during onsite contest";
_expected = "redoc";
_received = _obj.getMessage(text); break;
}
case 3:
{
string text = " ";
_expected = "";
_received = _obj.getMessage(text); break;
}
/*case 4:
{
string text = ;
_expected = ;
_received = _obj.getMessage(text); break;
}*/
/*case 5:
{
string text = ;
_expected = ;
_received = _obj.getMessage(text); break;
}*/
/*case 6:
{
string text = ;
_expected = ;
_received = _obj.getMessage(text); break;
}*/
default: return 0;
}
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
double _elapsed = (double)(clock()-_start)/CLOCKS_PER_SEC;
if (_received == _expected)
cout << "#" << _tc << ": Passed (" << _elapsed << " secs)" << endl;
else
{
cout << "#" << _tc << ": Failed (" << _elapsed << " secs)" << endl;
cout << " Expected: " << "\"" << _expected << "\"" << endl;
cout << " Received: " << "\"" << _received << "\"" << endl;
}
}
}
You have two problems.
The first, is you are supplying non-static class member functions (space and not_space) to find_if which expects a function object or pointer. So, declare them static if you want them to remain your class, or make them global by placing them outside the class.
The second, your string text parameter is non-const, but, you are working with a const interator type. begin() and end() calls will return const or non-const iterator depending on the calling object (text in this case) and whether or not it is const qualified. So, declare your text parameter as const.
The issue that I am having is that with the code below, each plOvr for all of the class objects is the same. This causes them to have the same stats for everything. Also, I have an array with names that should be printed but it is skipping the first value.
using namespace std;
class Player
{
public:
int plOvr;
float plSpg, plSps;
string werk;
void setPlayeName(string);
string plName;
void setPlyrVal()
{
srand (time(NULL));
plOvr = rand()% 29 + 70;
plSps = plOvr / 10;
plSpg = plSps / 2;
}
};
void Player::setPlayeName(string werk)
{
plName = werk;
}
int main()
{
Player plyr1,plyr2,plyr3,plyr4,plyr5;
string firstTime;
string name[5] = {"Eric Gelinas","John Merill", "Jaromir Jagr", "Travis Zajac","Reid Boucher"};
bool firstOp;
cout << "Is this the first time this program has run?" << endl;
cin >> firstTime;
if (firstTime == "Yes" || firstTime == "yes")
{
firstOp == firstOp;
plyr1.setPlyrVal();
plyr1.setPlayeName(name[1]);
plyr2.setPlyrVal();
plyr2.setPlayeName(name[2]);
plyr3.setPlyrVal();
plyr3.setPlayeName(name[3]);
plyr4.setPlyrVal();
plyr4.setPlayeName(name[4]);
plyr5.setPlyrVal();
plyr5.setPlayeName(name[5]);
ofstream playerSaveData;
playerSaveData.open ("savedata.txt");
playerSaveData << plyr1.plName << "," << plyr1.plOvr << "," << plyr1.plSpg << "," << plyr1.plSps << "\n";
playerSaveData << plyr2.plName << "," << plyr2.plOvr << "," << plyr2.plSpg << "," << plyr2.plSps << "\n";
playerSaveData << plyr3.plName << "," << plyr3.plOvr << "," << plyr3.plSpg << "," << plyr3.plSps << "\n";
playerSaveData << plyr4.plName << "," << plyr4.plOvr << "," << plyr4.plSpg << "," << plyr4.plSps << "\n";
playerSaveData << plyr5.plName << "," << plyr5.plOvr << "," << plyr5.plSpg << "," << plyr5.plSps << "\n";
playerSaveData.close();
cout << "done.\n";
}
else
{
firstOp == !firstOp;
}
return 0;
}
You may use std::uniform_int_distribution<int> and an engine as std::mt19937 from <random>.
The engine (as srand) has to be initialized with seed only once.
Your program rewritten:
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <random>
class Player
{
public:
void setPlayeName(const std::string& name) { plName = name; }
void setPlyrVal(std::mt19937& rand_engine)
{
std::uniform_int_distribution<int> distr(70, 98);
plOvr = distr(rand_engine);
plSps = plOvr / 10;
plSpg = plSps / 2;
}
public:
int plOvr;
float plSpg, plSps;
std::string werk;
std::string plName;
};
int main()
{
std::mt19937 rand_engine(time(nullptr));
Player plyrs[5];
const std::string names[5] = {"Eric Gelinas","John Merill", "Jaromir Jagr", "Travis Zajac","Reid Boucher"};
std::cout << "Is this the first time this program has run?" << std::endl;
std::string firstTime;
std::cin >> firstTime;
if (firstTime == "Yes" || firstTime == "yes") {
for (int i = 0; i != 5; ++i) {
plyrs[i].setPlyrVal(rand_engine);
plyrs[i].setPlayeName(names[i]);
}
std::ofstream playerSaveData;
playerSaveData.open ("savedata.txt");
for (const auto& plyr : plyrs) {
playerSaveData << plyr.plName << "," << plyr.plOvr << "," << plyr.plSpg << "," << plyr.plSps << "\n";
}
std::cout << "done." << std::endl;
}
return 0;
}
Live example
You should call srand() only once in the whole program, instead of calling it before each rand().
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
//Class for a card deck:
class CardDeck
{
public:
CardDeck(int theValue, string theSuit);
CardDeck(){}
// Setters--Don't think we will need
void setValue(int theValue);
void setSuit(string theSuit);
// Getters
int getValue();
string getSuit();
private:
int value;
string suit;
};// end CardDeck class
int main()
{
int i = 0;
int gameInPlay = 1;
const string DR = "Dragons";
const string MG = "Mages";
const string WR = "Warriors";
const string CF = "Confessors";
vector<CardDeck> startDeck(52);
vector<CardDeck> tempCards(1);
// Dragons Suit
for (i = 0; i < 13; i++)
{
startDeck[i].setValue(i - 12);
startDeck[i].setSuit("Dragons");
//startDeck[i].setValue(i+1);
// startDeck[i].setSuit("Dragons");
}
// Mages Suit
for (i = 13; i < 26; i++)
{
startDeck[i].setValue(i - 12);
startDeck[i].setSuit("Mages");
}
for (i = 26; i < 39; i++)
{
startDeck[i].setValue(i - 25);
startDeck[i].setSuit("Warriors");
}
for (i = 39; i < 52; i++)
{
startDeck[i].setValue(i - 38);
startDeck[i].setSuit("Confessors");
}
// Output for de-bug
cout << "The first card is " << startDeck[0].getValue() << " of " << startDeck[0].getSuit() << endl;
cout << "The second card is " << startDeck[1].getValue() << " of " << startDeck[1].getSuit() << "\n\n";
//****************************************************************************
// Shuffle the deck
int shuffleTimes = (rand() % 120) + 1;
// Need to shuffle a random # of times, else deck is
// "shuffled" in same order every time
for (int i = 0; i < shuffleTimes; i++)
{
srand(time(0));
for (i = 0; i < startDeck.size(); i++)
{
int second = rand() % startDeck.size();
CardDeck temp = startDeck[i];
startDeck[i] = startDeck[second];
startDeck[second] = temp;
}
}
//*******************************************************************************
// Verify cards are shuffled for de-bug
cout << "After shuffling:\n Value \t Suit\n";
// Output for de-bug
cout << "The first card is " << startDeck[0].getValue() << " of " << startDeck[0].getSuit() << endl;
cout << "The second card is " << startDeck[1].getValue() << " of " << startDeck[1].getSuit() << endl;
// Creat human deck
vector<CardDeck> humanDeck(26);
for (i = 0; i< 26; i++)
{
humanDeck[i] = startDeck[i];
}
// Creat computer deck
vector<CardDeck> computerDeck(26);
for (i = 0; i< 26; i++)
{
computerDeck[i] = startDeck[i + 26];
}
// Output for de-bug
cout << "The first human card is " << humanDeck[0].getValue() << " of " << humanDeck[0].getSuit() << endl;
cout << "The second human card is " << humanDeck[1].getValue() << " of " << humanDeck[1].getSuit() << "\n\n";
cout << "The first computer card is " << computerDeck[0].getValue() << " of " << computerDeck[0].getSuit() << endl;
cout << "The second computer card is " << computerDeck[1].getValue() << " of " << computerDeck[1].getSuit() << "\n\n";
getchar();
return 0;
} // end main
// Functions for CardDeck class
CardDeck::CardDeck(int theValue, string theSuit)
{
value = theValue;
suit = theSuit;
}
void CardDeck::setValue(int theValue)
{
value = theValue;
}
void CardDeck::setSuit(string theSuit)
{
suit = theSuit;
}
int CardDeck::getValue()
{
return value;
}
string CardDeck::getSuit()
{
return suit;
}
Obviously not done with the game, and I am new to C++ and programming so any help will do
I would like some help trying to figure out how to get only positive numbers instead of negative. Also would like to figure out why they return values of the first two outputs are always the same.
Thank you
You probably meant to do this:
for (i = 0; i < 13; i++)
{
startDeck[i].setValue(i+1);
startDeck[i].setSuit("Dragons");
//startDeck[i].setValue(i+1);
// startDeck[i].setSuit("Dragons");
}
Otherwise, startDeck[i].setValue(i-12); will set negative values for i < 12, which is most of that loop.
I'm wondering why you have the correct code there and commented out...what was the issue with it?