User Input to a Value Stored in an Integer Array C++ - c++

I'm trying to make it so every time the user inputs a value, it's stored in an array in a place that represents a stat. Hard to explain so here's the code:
void MainChar::CharacterCreation()
{
int statPoints = 20;
int health = 0;
int magicDamage = 0;
int magicResist = 0;
int physicalResist = 0;
int physicalDamage = 0;
int magicOffMastery = 0;
int physicalOffMastery = 0;
int magicDefMastery = 0;
int physicalDefMastery = 0;
// SETS STATS AND THIER RESPECTIVE ARRAY PLACEMENT
int statArray[9];
statArray[0] = health;
statArray[1] = magicDamage;
statArray[2] = magicResist;
statArray[3] = physicalResist;
statArray[4] = physicalDamage;
statArray[5] = magicOffMastery;
statArray[6] = physicalOffMastery;
statArray[7] = magicDefMastery;
statArray[8] = physicalDefMastery;
std::string stats[9];
stats[0] = "Health : " ;
stats[1] = "Magic Damage : " ;
stats[2] = "Magic Resist : " ;
stats[3] = "Physical Resist : " ;
stats[4] = "Physical Damage : " ;
stats[5] = "Magic Offensive Mastery : " ;
stats[6] = "Physical Offensive Mastery : ";
stats[7] = "Magic Defensive Mastery : " ;
stats[8] = "Physical Defensive Mastery : ";
int statString = 0;
int statInt = 0;
while (statPoints > 0)
{
std::cout << "*******************************************************************************" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "* CHARACTER CREATION *" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << " Health : " << health * 10 << std::endl;
std::cout << std::endl;
std::cout << " Magic Damage : " << magicDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Resist : " << magicResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Resist : " << physicalResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Damage : " << physicalDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Offensive Mastery : " << magicOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Offensive Mastery : " << physicalOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Magic Defensive Mastery : " << magicDefMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Defensive Mastery : " << physicalDefMastery << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << "STAT POINTS: " << statPoints << std::endl;
std::cout << stats[statString] ;
std::cin >> statArray[statInt] ;
statPoints -= statArray[statInt];
++statString;
++statInt;
}
}
As you might notice, I'm trying to have the user change the value of health, which is stored in statArray[statInt], which equates to statArray[0], then I ++statInt. The idea was I'd be able to have the user input all his stats one at a time. Instead of my intention, whats happening is it's taking the user input as a reference to the array slot. (statArray[0], statArray[1]) etc. Instead of the stat associated with that array slot.

To achieve what you want to do you could use a union:
union Stat {
struct {
int health;
int magicDamage;
int magicResist;
// ...
};
int array[3];
};
int main() {
Stat stat;
stat.array[1] = 42;
cout<<stat.magicDamage<<endl; // should give 42
};
However a better solution would be to use a map:
map<string,int> stat;
const char *keys[] = {"health","magicDamage","magicResist"};
int main() {
for (int i=0;i<3; ++i) {
cout<<"enter "<<keys[i]<<endl;
cin>>stat[keys[i]];
}
}

Change statArraydefinition to use pointers instead of values
int *statArray[9];
statArray[0] = &health;
statArray[1] = &magicDamage;
statArray[2] = &magicResist;
statArray[3] = &physicalResist;
statArray[4] = &physicalDamage;
statArray[5] = &magicOffMastery;
statArray[6] = &physicalOffMastery;
statArray[7] = &magicDefMastery;
statArray[8] = &physicalDefMastery;
And update next lines:
std::cout << *stats[statString] ;
std::cin >> *statArray[statInt] ;
statPoints -= *statArray[statInt];

Related

C++ Why numeric limits doesn't work for uint8_t and int8_t? [duplicate]

This question already has answers here:
cout not printing unsigned char
(5 answers)
Closed 2 years ago.
I recently noticed numeric_limits::max() and numeric_limits::min() don't seem to work for uint8_t and int8_t. Is there a reason for this or could it be a bug? I tried on my own computer using gcc compiler:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
std::cout << "numeric_limits<uint8_t>::max() = " << numeric_limits<uint8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << numeric_limits<int8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << numeric_limits<int8_t>::min() << std::endl;
std::cout << "numeric_limits<uint16_t>::max() = " << numeric_limits<uint16_t>::max() << std::endl;
std::cout << "numeric_limits<int16_t>::max() = " << numeric_limits<int16_t>::max() << std::endl;
std::cout << "numeric_limits<int16_t>::min() = " << numeric_limits<int16_t>::min() << std::endl;
std::cout << "numeric_limits<uint32_t>::max() = " << numeric_limits<uint32_t>::max() << std::endl;
std::cout << "numeric_limits<int32_t>::max() = " << numeric_limits<int32_t>::max() << std::endl;
std::cout << "numeric_limits<int32_t>::min() = " << numeric_limits<int32_t>::min() << std::endl;
std::cout << "numeric_limits<uint64_t>::max() = " << numeric_limits<uint64_t>::max() << std::endl;
std::cout << "numeric_limits<int64_t>::max() = " << numeric_limits<int64_t>::max() << std::endl;
std::cout << "numeric_limits<int64_t>::min() = " << numeric_limits<int64_t>::min() << std::endl;
return 0;
}
gives output:
numeric_limits<uint8_t>::max() = �
numeric_limits<int8_t>::max() =
numeric_limits<int8_t>::min() = �
numeric_limits<uint16_t>::max() = 65535
numeric_limits<int16_t>::max() = 32767
numeric_limits<int16_t>::min() = -32768
numeric_limits<uint32_t>::max() = 4294967295
numeric_limits<int32_t>::max() = 2147483647
numeric_limits<int32_t>::min() = -2147483648
numeric_limits<uint64_t>::max() = 18446744073709551615
numeric_limits<int64_t>::max() = 9223372036854775807
numeric_limits<int64_t>::min() = -9223372036854775808
It does work. The output is interpreted as ASCII characters though. If you cast to int before you print, you will see the correct values:
std::cout << "numeric_limits<uint8_t>::max() = " << static_cast<int>(numeric_limits<uint8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << static_cast<int>(numeric_limits<int8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << static_cast<int>(numeric_limits<int8_t>::min()) << std::endl;
std::cout << "numeric_limits<uint8_t>::max() = " << std::to_string(numeric_limits<uint8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << std::to_string(numeric_limits<int8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << std::to_string(numeric_limits<int8_t>::min()) << std::endl;
try to convert them to string, before inserting them into cout.
int8 types are probably defined as chars, so don't print the values as charbut as ints:
int main() {
std::cout << "numeric_limits<uint8_t>::max() = " << (int)numeric_limits<uint8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << (int)numeric_limits<int8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << (int)numeric_limits<int8_t>::min() << std::endl;
}

c++ vectors/classes/structs to shorted verbose code

I recently began reading my Sam's teach yourself C++ in 24 hours (5th Edition) book and came across Hour 3's first exercise where the question was something along the lines of create const ints for the values of touchdown extra point field goal and safety. I did this for a college game, where I printed the score for each team at the end of each quarter and then added them up for the final score. See code below:
#include <iostream>
int displayScore1(int oregon1, int oregonState1) {
if (oregon1 >= oregonState1) {
std::cout << " Oregon: " << oregon1 << " Oregon State: " << oregonState1 << "\n";
} else {
std::cout << " Oregon State: " << oregonState1 << " Oregon: " << oregon1 << "\n";
}
return 0;
}
int displayScore2(int oregon2, int oregonState2) {
if (oregon2 >= oregonState2) {
std::cout << " Oregon: " << oregon2 << " Oregon State: " << oregonState2 << "\n";
} else {
std::cout << " Oregon State: " << oregonState2 << " Oregon: " << oregon2 << "\n";
}
return 0;
}
int displayScore3(int oregon3, int oregonState3) {
if (oregon3 >= oregonState3) {
std::cout << " Oregon: " << oregon3 << " Oregon State: " << oregonState3 << "\n";
} else {
std::cout << " Oregon State: " << oregonState3 << " Oregon: " << oregon3 << "\n";
}
return 0;
}
int displayScore4(int oregon4, int oregonState4) {
if (oregon4 >= oregonState4) {
std::cout << " Oregon: " << oregon4 << " Oregon State: " << oregonState4 << "\n";
} else {
std::cout << " Oregon State: " << oregonState4 << " Oregon: " << oregon4 << "\n";
}
return 0;
}
int finalScore(int oregonFinal, int oregonStateFinal) {
if (oregonFinal > oregonStateFinal) {
std::cout << " Final Score: " << " Oregon " << oregonFinal << " Oregon State " << oregonStateFinal << "\n";
} else {
std::cout << " Final Score: " << " Oregon State " << oregonStateFinal << " Oregon " << oregonFinal << "\n";
}
return 0;
}
int main () {
const int touchdown = 6;
const int fieldGoal = 3;
const int extraPoint = 1;
const int safety = 2;
int oregon1 = 0;
int oregon2 = 0;
int oregon3 = 0;
int oregon4 = 0;
int oregonState1 = 0;
int oregonState2 = 0;
int oregonState3 = 0;
int oregonState4 = 0;
int oregonFinal = 0;
int oregonStateFinal = 0;
oregon1 = touchdown + extraPoint;
oregonState1 = touchdown + extraPoint;
displayScore1(oregon1, oregonState1);
oregon2 = touchdown + extraPoint;
oregonState2 = touchdown + extraPoint;
displayScore2(oregon2, oregonState2);
oregon3 = touchdown + extraPoint + fieldGoal;
oregonState3 = touchdown + extraPoint;
displayScore3(oregon3, oregonState3);
oregon4 = 0;
oregonState4 = touchdown + extraPoint + fieldGoal + fieldGoal;
displayScore4(oregon4, oregonState4);
oregonFinal = oregon1 + oregon2 + oregon3 + oregon4;
oregonStateFinal = oregonState1 + oregonState2 + oregonState3 + oregonState4;
finalScore(oregonFinal, oregonStateFinal);
return 0;
}
I understand that this is the long way to do it and I got the output to be what I wanted. However, being new to C++, I'm not entirely sure how to write something more flexible for re-use. My question is, is there a more efficient way to do this? Or is there a way to accomplish the same outcome/output in less code? I was happy i figured out the original problem, but I would like to understand/learn efficiency as well as the basics. I understand that vectors and structs/classes may be an avenue, I just don't really understand the reference materials.
So first things first, strings are expensive, consider using an enum and then using a map to translate that to a string, for example:
enum Teams {
OREGON,
OREGON_STATE
};
const map<Teams, string> Teams2string = { { OREGON, "Oregon" }, { OREGON_STATE, "Oregon State" } };
Then consider that the best way to store scores would be in a vector<pair<int, int>> particularly since we don't know how many, if any, overtime periods will be played. Thus the vector could be resized on a per-game basis.
Finally we need to make a game object which incorporates this information, and provides it's own displayScore method:
struct Game {
const Teams first;
const Teams second;
const vector<pair<int, int>> score;
void displayScore(const int period) {
if(period < size(score)) {
if(score[period].first >= score[period].second) {
cout << Teams2string[first] << ": " << score[period].first << ' ' << Teams2string[second] << ": " << score[period].second << endl;
} else {
cout << Teams2string[second] << ": " << score[second].first << ' ' << Teams2string[first] << ": " << score[first].second << endl;
}
}
}
};

Getting large negative numbers printing arrays

Hello Im getting negative valhes when trying to output some variables to the screen.
Ive looked this up and in most cases its an uninitialized variable but I cant find anything wrong.
Saying I have too much code to text ratio but I really dont know how to reiterate so heres some filler.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Input;
int WepType;
string Weapon;
string WeaponType;
int Stats[3];
int AtkStats[4];
cout << "Pick your weapon.\n" << endl;
cout << "{===================================================}\n";
cout << "[ ]\n";
cout << "[ Iron Longsword Sharp Fishing Spear ]\n";
cout << "[ 1 ]\n";
cout << "[ ]\n";
cout << "[ Ornate Hunting Bow ]\n";
cout << "[ ]\n";
cout << "{===================================================}\n";
//Weapon Selection
cin >> Input;
if(Input == 1)
{
WepType = 1;
Weapon = "Iron Longsword";
WeaponType = "OneHanded";
Stats[0] = Stats[0] + 10;
Stats[1] = Stats[1] + 0;
Stats[2] = Stats[2] + 0;
AtkStats[0] = AtkStats[0] + 10;
AtkStats[1] = AtkStats[1] + 0;
AtkStats[2] = AtkStats[2] + 0;
AtkStats[3] = AtkStats[3] + 0;
cout << "Weapon = " << Weapon << endl;
cout << "Weapon Type = " << WeaponType << endl;
cout << "Health = " << Stats[0] << endl;
cout << "Physical = " << AtkStats[0] << endl;
cout << "Light = " << AtkStats[1] << endl;
cout << "Dark = " << AtkStats[2] << endl;
cout << "Temporal = " << AtkStats[3] << endl;
}
return 0;
}
The problem is here:
int Stats[3];
int AtkStats[4];
You should do:
int Stats[3] = {0, 0, 0};
int AtkStats[4] = {0, 0, 0, 0};
Or as BlastFurnace pointed out in the comments (which I forgot about):
int Stats[3] = {}; // Initialize all elements to zero.
int AtkStats[4] = {};
In order to initialize the values. Right now they are just random junk, so when you assign, you get errors.

How to print data from a structure in a loop

I want a loop that prints the values for each soda.
When I try to print machine[j].name, an error is thrown stating that no operator is found or no acceptable conversion exists.
struct Soda
{
string name;
double price;
int quantity;
} a, b, c, d, e;
int main(int argc, char** argv)
{
Soda machine[5];
a.name = "Cola";
a.price = .80;
a.quantity = 20;
b.name = "Root Beer";
b.price = .75;
b.quantity = 20;
c.name = "Lemon-Lime";
c.price = .90;
c.quantity = 20;
d.name = "Grape - Mango";
d.price = .80;
d.quantity = 20;
e.name = "Cream";
e.price = .80;
e.quantity = 20;
cout << setw(5) << " Name" << setw(15) << " Cost"
<< setw(15) << " Stock" << setw(14) << "";
for (int i = 0; i < 5; i++)
for (int j = 0; j < 3; j++){
cout << left << setw(5) << machine[j].name
<< setw(15) << " Cost" << setw(15) << " Stock"
<< setw(14);
return 0;
}
It appears that you are mixing up the array with the declaration for separate variables. You have set values on the non-array variables and then you are trying to loop through the array.
Instead, you should set the values on the actual elements of the array.
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
struct Soda
{
string name;
double price;
int quantity;
};
int main(){
Soda machine[5];
machine[0].name = "Cola";
machine[0].price = .80;
machine[0].quantity = 20;
machine[1].name = "Root Beer";
machine[1].price = .75;
machine[1].quantity = 20;
machine[2].name = "Lemon-Lime";
machine[2].price = .90;
machine[2].quantity = 20;
machine[3].name = "Grape - Mango";
machine[3].price = .80;
machine[3].quantity = 20;
machine[4].name = "Cream";
machine[4].price = .80;
machine[4].quantity = 20;
cout << "Name Cost Stock" << endl;
for (int i = 0; i < 5; i++)
cout << machine[i].name << " " << machine[i].price << " " << machine[i].quantity << endl;
}
You are trying to print out the contents of objects (array of objects ) machine[i], when instead you have assigned values to some different objects: a, b, c, d, e.
Solution:
1. Either print a.name, b.name etc.
2. or instead of
a.name = "Cola";
a.price = .80;
write:
machine[i].name = "Cola";
machine[i].price = .80;
Since you marked this as C++, I suggest you try a vector (for machine).
int main()
{
vector<Soda> machine;
Soda tmpSoda;
{
tmpSoda.name = "Cola";
tmpSoda.price = .80;
tmpSoda.quantity = 20;
machine.push_back(tmpSoda);
}
{
tmpSoda.name = "Root Beer";
tmpSoda.price = .75;
tmpSoda.quantity = 20;
machine.push_back(tmpSoda);
}
// ....
{
tmpSoda.name = "Cream";
tmpSoda.price = .80;
tmpSoda.quantity = 20;
machine.push_back(tmpSoda);
}
Now all soda's are in the vector (named machine), so you can now report each element in a loop.
cout << "Name Cost Stock" << endl;
for (int i=0; i<machine.size(); ++i)
{
cout << machine[i].name << " "
<< machine[i].price << " "
<< machine[i].quantity << endl;
}
Since you marked this as C++, I suggest you try a vector of class instances. Note how the ctor fills in the values of each Soda, and the "show" method uses the private data attributes of each instance.
class Soda
{
public:
Soda(std::string aName,
double aPrice,
int aQuantity) :
name(aName),
price(aPrice),
quantity(aQuantity)
{
}
~Soda() {
name.erase();
quantity = 0;
}
std::string show() {
std::stringstream ss;
ss << std::right << std::setw(14) << name << " "
<< std::left << std::setw(8) << price << " "
<< std::setw(7) << quantity;
return (ss.str());
}
static std::string showHdr() {
std::stringstream ss;
ss << std::right << std::setw(14) << "Name" << " "
<< std::left << std::setw(8) << "Price" << " "
<< std::left << std::setw(7) << "Stock";
return (ss.str());
}
private:
std::string name;
double price;
int quantity;
};
Main now reduces to:
std::vector<Soda> machine;
// load the machine with soda's
machine.push_back(Soda("Cola", .80, 20));
machine.push_back(Soda("Root Beer", .75, 20));
machine.push_back(Soda("Lemon-Lime", .90, 20));
machine.push_back(Soda("Grape - Mango", .80, 20));
machine.push_back(Soda("Cream", .80, 20));
// show machine inventory
{
// hdr
std::cout << Soda::showHdr() << std::endl;
for (auto i : machine)
std::cout << i.show() << std::endl;
}

Hot Dog Stand static function issue

I need this program to create a new HotDogStand object that is able to track how many hot dogs are sold by each stand individually and all together, and I cannot figure out how to make my static method work to find the total number of hot dogs sold between all stands. Can someone point me in the right direction please?
#include <iostream>
using namespace std;
class HotDogStand
{
public:
HotDogStand(int id, int hds);
void justSold();
int getNumSold();
int getID();
int getTotalSold();
private:
int idNum;
int hotDogsSold;
static int totalSold;
};
HotDogStand::HotDogStand(int id, int hds)
{
idNum = id;
hotDogsSold = hds;
return;
}
void HotDogStand::justSold()
{
hotDogsSold++;
return;
}
int HotDogStand::getNumSold()
{
return hotDogsSold;
}
int HotDogStand::getID()
{
return idNum;
}
int HotDogStand::getTotalSold()
{
totalSold = 0;
totalSold += hotDogsSold;
}
int main()
{
HotDogStand s1(1, 0), s2(2, 0), s3(3, 0);
s1.justSold();
s2.justSold();
s1.justSold();
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << "." << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << "." << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << "." << endl;
cout << "Total sold = " << s1.getTotalSold() << endl;
cout << endl;
s3.justSold();
s1.justSold();
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << "." << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << "." << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << "." << endl;
cout << "Total sold = " << s1.getTotalSold() << endl;
}
Globally (outside of the class), you have to define the static variable:
int HotDogStand::totalSold = 0;
Change
void HotDogStand::justSold()
{
hotDogsSold++;
totalSold++; // increment here
return;
}
And
int HotDogStand::getTotalSold()
{
return totalSold; // just return value
}