Double being given wrong value when part of object - c++

My code is supposed to be acting like a ATM machine where it can make different type of accounts and keep track of all the transactions and other information in the accounts
My problem is that the double balance which is stored in the BankAccount class which are stored in a vector to keep up with all the different accounts, well when I try and retrieve the balance it gives me "-9.25596e+061" as the balance no matter what I deposited or put in there, or even if it should be zero. The only time I got it to work is when i made balance a global variable but that is pretty much useless for the sake of the program.
activeAcounts is a vector
void BankAccount::deposit(double amount, string name)
{
balance += amount;
for (int i = 0; i < activeAccounts.size(); i++)
{
if (activeAccounts[i].getName() == name)
{
activeAccounts[i].setBalance(balance + amount);
}
}
}
Inquiry is suppose to just return the balance of an account specified by it's name
void BankAccount::inquiry(string n)
{
for (int i = 0; i < activeAccounts.size(); i++)
{
if (activeAccounts.at(i).getName() == n)
{
cout << "Balance: " << activeAccounts[i].getBalance() << endl;
}
}
}

You are more than likely not initializing balance to 0. Member variables need to be initialized, unlike globals. You should initialize balance in the BankAccount constructor.
In addition, since you are using vector, if you've coded a copy constructor and assignment operator for BankAccount, you need to make sure that balance is copied, else your copy will have a bogus balance value.
As to the global variable, when you made balance a global variable, global variables are automatically initialized to 0, which is probably why you didn't have a problem when it was made global.

Related

Vector of a class resetting member variables after for loop

I have an assignment where we need to use this basic structure of vectors and classes to learn about parent and child classes and polymorphism. Here is the code of the function I'm supposed to write:
void assignStudents(vector<Student*>& v) {
for (int i = 0; i < 5; i++)
{
cout << "Enter a study level: ";
string input;
cin >> input;
if (input == "graduate")
{
Graduate inputClass;
Student* inputParentClassPtr = &inputClass;
v.push_back(inputParentClassPtr);
v[i]->addToVector(input);
inputParentClassPtr = nullptr;
}
else if (input == "undergraduate")
{
Undergraduate inputClass;
Student* inputParentClassPtr = &inputClass;
inputParentClassPtr->addToVector(input);
v.push_back(inputParentClassPtr);
}
else
{
cout << "Please enter a valid response, either graduate or undergraduate" << endl;
i--;
}
}
for (size_t i = 0; i < v.size(); i++)
{
vector<string> studyLevels = v[i]->getStudyLevels();
size_t size = studyLevels.size();
for (int j = 0; j < size; j++)
{
cout << studyLevels[j];
}
}
}
I debug the program and every time the first for loop moves on to the next iteration, every member variable inside each object in my vector goes blank, but then when I add a new object into the vector, then call the addToVector() function they come back.
I added the bottom for loop to check if any editing is happening, and once I get to that bottom for loop, every member variable is empty again.
I have the Student class vector where I am adding Undergraduate and Graduate classes to. Every Student class has a protected vector inside called levels. I need to add the class to vector that holds all my objects, then edit the member variable vector to include the string representing the type of class it is.
Why do the member variables (levels) go blank every time it finishes an iteration of the for loop?
I'll just focus on one part, as the same issue appears twice in your code.
{
Graduate inputClass; // create local student "on the stack"
Student* inputParentClassPtr = &inputClass;
v.push_back(inputParentClassPtr); // store address of student
v[i]->addToVector(input);
inputParentClassPtr = nullptr; // has no real effect
} // inputClass goes out of scope and is destroyed here
When the block ends, the local "stack" variables from that block are destroyed. That means the Graduate object is no longer valid, and the pointer you stored in v is now pointing at something unusable.
To fix that, you need to create the objects in dynamic memory.
You should change your vector to store std::unique_ptr<Student>, and create the objects using std::make_unique(), like this:
auto inputParentClassPtr = std::make_unique<Graduate>();
v.push_back(std::move(inputParentClassPtr));
But, if you can't do that, you will need to use new instead, like this:
Student* inputParentClassPtr = new Graduate();
v.push_back(inputParentClassPtr);
Either way, even though inputParentClassPtr is still destroyed at the end of the block, it is only a pointer and the Graduate object it pointed to is still alive.
If you use new, you'll then need to delete all the objects in the vector when you are done using them, or you'll have a memory leak. Using std::unique_ptr will handle that for you.

Using vector of a class that contains static member variable

I had an Airplane class and this Airplane had a vector of Seat class named "m_seat".
In the Constructor of my Airplane, I used the number of seats as the needed parameter to resize the m_seat vector size to the requested size of the user. This was my code:
class Seat;
class Airplane {
vector<Seat> m_seat;
public:
Airplane(int);
};
class Seat{
static int m_seatNumber;
public:
Seat() { m_seatNumber += 10; }
};
int Seat::m_seatNumber = 100;
Airplane::Airplane(int numberOfSeats) {
for (int i = 0; i<numberOfSeats; ++i) {
m_seat.push_back();
}
}
int main()
{
Airplane(80);
return 0;
}
But it gave this error.
std::vector<_Ty,_Aloc>::push_back no overload of function takes 0 arguments,
and if this was really the problem, I had no idea what should I have put in my push_back()? So I tried {}
m_seat.push_back({});
and It worked!
Now, I have another problem which is my main problem(SO rule: Ask only one question at a time!) that all seat numbers appear to be increased to the same number! I also used the "resize" member function of the vector, instead of that loop:
m_seat.resize(numberOfSeats);
But the problem (same increase in the number of the m_seatNumber) remains unsolved.
Non-native English Speaker, Sorry.
Disclaimer: This is a "best guess" answer.
If you wanted each seat to have a different, automatically increasing number, you need two values; one non-static, describing each seat, and one static, describing last-used number:
class Seat{
static int lastSeatNumber;
int seatNumber;
public:
Seat() { seatNumber = lastSeatNumber; lastSeatNumber += 10; }
};
int Seat::lastSeatNumber = 100;
That way each seat will receive its distinct number. This design is bad, however, as it doesn't allow e.g. seat number sharing between two airplanes! It also doesn't allow you to "free up" the numbers of seats you're no longer using, and the number can only keep growing. Also copying a Seat, while possible, won't manipulate that number at all. It'd be much better to allow the Airplane class to assign the seat numbers:
class Seat{
int seatNumber;
public:
Seat(int seatNumber) : seatNumber(seatNumber) { }
};
Airplane::Airplane(int numberOfSeats) {
int seatNumber = 100;
const int numberIncrement = 10;
for (int i = 0; i < numberOfSeats; ++i) {
m_seat.push_back(Seat(seatNumber));
seatNumber += numberIncrement;
}
}
This way you can get the old behavior by adding another parameter to the airplane constructor telling it which number to start counting from.

I keep receiving -2 as my updated salary

I am doing the following with my program:
1) Write the class definition for a class named Employee with name and salary as employee objects. The class contains two member functions: the constructor and a function that allows a program to assign values to the data members.
2) Add two member functions to the Employee class. One member function should allow any program using an employee object to view the contents of the salary data member. The other member function should allow the program to view the contents of the employee name data member.
3) Add another member function to the Employeeclass. The member function should calculate an employee objects new salary, based on a raise percentage provided by the program using the object. Before calculating the raise, the member function should verify that the raise percentage is greater than or equal to zero. If the raise percentage is less than zero, the member function should display an error message.
4) Write a main function that will create an array of employee objects, assign values to the objects, display the names and current salaries for all objects, ask user for the raise percentage and then calculate and display new salaries for all objects.
However, I receive -2 as my new salary after I input the data from the keyboard. I figured another set of eyes could see what I can't and would highly appreciate if someone can lend a hand, or at least steer me in the right direction. Perhaps it is a logic error, or something wrong with my declarations. Thank you for your time.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class EMPLOYEE
{
public:
EMPLOYEE();//
EMPLOYEE(string name, int salary);//
public:
string name;//name to be input
int salary;//salary to be input
int percentage_raise;
int updated_salary;
public:
int enter_values();
int output_values();
int NEW_SALARY();
};
//default constructor
EMPLOYEE::EMPLOYEE()
{
name = "";
salary = 0;
}
//constructor with name/salary variables
EMPLOYEE::EMPLOYEE(string NAME, int SALARY)
{
name= NAME;
salary= SALARY;
}
//name and salary to be input...
int EMPLOYEE::enter_values()
{ cout<<"Enter name and salary: ";
cin>> name;
cin>>salary;
return 0;
}
//output
int EMPLOYEE::output_values()
{ cout<<"Name: "<<name<<endl;
cout<<"Salary: "<<salary<<endl;
return 0;
}
//
int EMPLOYEE::NEW_SALARY()
{
if ( percentage_raise >= 0)
{ int updated_salary;
int raise= (salary *percentage_raise)/100;
updated_salary += raise;
}
else if(percentage_raise< 0)
{ cout<<"Error Message"<<endl;
}
return 0;
}
int main()
{
EMPLOYEE employees[100];
EMPLOYEE percent_to_be_raised;
int i;
for(i =0 ;i<100 ; i++)
{ employees[i]=EMPLOYEE();
employees[i].enter_values();
employees[i].name;
employees[i].salary;
// employees[i].NEW_SALARY();
employees[i].output_values();
cout<<"How much should the salary be raised by?"<<endl;
cin>>percent_to_be_raised.percentage_raise;
cout<<"-----------------------------"<<endl;
cout<<employees[i].name <<"'s new salary is "<<percent_to_be_raised.updated_salary<<endl;
}
}
You need to rewrite this quite alot.
A few pointers:
EMPLOYEE percent_to_be_raised;
Is completely off base. The task states that this calculation should be done in an employee member function. I.e. the raise should be performed as
Employee alfred;
std::cin>> alfred.salary;
double raise;
std::cin>> raise;
alfred.raise_salary(raise); // this is what the task asks for.
Use a naming convention.
Employee
is fine for a c++ class with a capitalized class name convention. EMPLOYEE is not; this looks like a macro name.
Member function usually starts with non-capitalized
Employee::new_salary( the_salary );
Follow the examples you have available from the course material.
Of course
employees[i].name;
employees[i].salary;
Does not do anything. Please review your code in detail and start at the first spot you don't understand.
Note that the OP coding style convention is used to assist the OP. I am aware of the proper naming convention for classes, member functions, and class data members (e.g. see the answer by Captain Giraffe for more).
Inside of:
int EMPLOYEE::NEW_SALARY()
{
if ( percentage_raise >= 0)
{ int updated_salary;
int raise= (salary *percentage_raise)/100;
updated_salary += raise;
}
} // added this to close the function properly
there is a locally declared variable, which is typed identically to the public access data member of the same name. What is the intention here?
Most likely it should be coded like so:
int EMPLOYEE::NEW_SALARY()
{
if ( percentage_raise >= 0)
{
int raise = (salary *percentage_raise)/100;
updated_salary += raise;
}
} // added this to close the function properly
There are design considerations for having all class member data public, as well as having an integer for a percentage. From the calculation above, it looks like only values of one, two, three, etc. are allowed for the percentage number. What is the class supposed to do if a raise is 3.75 percent?
The constructor has to set ALL class data members to something meaningful too. For example, the percentage_raise and updated_salary variables are ignored. Most likely the default constructor has to be updated to:
//default constructor
EMPLOYEE::EMPLOYEE()
{
name = "";
salary = 0;
percentage_raise = 0;
updated_salary = 0;
}
The name and salary constructor has to be updated too. It should probably look like (using the style convention posted by the OP):
//constructor with name/salary variables
EMPLOYEE::EMPLOYEE(string NAME, int SALARY)
{
name = NAME;
salary = SALARY;
percentage_raise = 0;
updated_salary = salary;
}

A few questions regarding overloading stream operators in c++

I am working on a hangman program and I want to output a status report to the user after each guess. As I am using classes, I need to use a 'friend' keyword. I'm ok with the concept of classes and friends however I am struggling to implement it properly in my program.
The main issue is that the numbers of vowels is not being counted and the number of letters the user can try is not updating. The full alphabet is being displayed each time.
The code of my program is substantial and I'm not going to post all of it here. The issues I have are with the functions remainingLetters(); and vowelCount(). I have attached the relevant code snippets. This code will obviously not compile. I'm just hoping that someone might see an obvious mistake that I've missed.
**Hangman.h:**
{
public:
...
int vowelCount()const; //to be implemented
char* remainingLetters()const;//to be implemented
friend ostream& operator<< (ostream &out, Hangman &game);
private:
...
int vowel_count;
char secretWord[MAXWORDLENGTH];
char* remaining_letters;
}
**hangman.cpp:**
{
...
int Hangman::vowelCount()const
{
int vowel_count = 0;
int i;
secretWord[i];
for(i=0; i<strlen(secretWord); i++)
{
if(secretWord[i]=='a'||secretWord[i]=='e'||secretWord[i]=='i'||secretWord[i]=='o'||secretWord[i]=='u')
{
vowel_count++;
}
}
return vowel_count;
}
char* Hangman::remainingLetters()const
{
char alphabet[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char *remaining_letters=new char[ALPHABETSIZE];
for(int i=0; i<strlen(secretWord); i++)
{
for(int j=0; j<ALPHABETSIZE; j++)
{
if(secretWord[i]!=alphabet[j])
{
remaining_letters[j]=alphabet[j];
}
}
}
return remaining_letters;
}
ostream& operator<< (ostream &out, Hangman &game)
{
out << "Number of guesses remaining is: " << game.numGuessesAllowed-game.numWrongGuesses << endl
<< "Number of incorrect guesses so far is: "<<game.numWrongGuesses <<endl
<< "Letters remaining are: "<<game.remaining_letters <<endl
<< "Hint: The secret word contains "<<game.vowel_count <<" vowels"<<endl;
return out;
}
}
**main.cpp**
{
...
cout << game;
...
return 0;
}
You need to call the functions vowelCount() and remainingLetters() for the corresponding variables to be calculated (so that you can output them).
Also, char *remaining_letters=new char[ALPHABETSIZE]; will allocate the memory for the letters and initialize each position with 0 -- which also happens to be the string terminating value. So if the first position (letter 'a') is not set, out<<game.remaining_letters will not output anything
Moreover, the function vowelCount() defines the local variable vowel_count that hides the one in the class (member variable), so the member variable will not get updated (uless you explicitly assign the return value of vowelCount() to that member variable) I suggest you delete the local variable declaration from the vowelCount() function and it will use the member variable, which is what you need
The previous paragraph also applies to the member variable remaining_letters
One more thing: the memory allocated with new is not deallocated automatically, and once you return from the function call, the allocated memory is lost (as in there is no way to access it again, but it still uses memory), unless you assign it when the function returns. You will need to pari each new[] call with its corresponding delete[] call. Better yet: allocate the memory in the constructor of the class and delete it in the destructor for the member variable
Note: yet another bug is in this logic:
for(int i=0; i<strlen(secretWord); i++)
{
for(int j=0; j<ALPHABETSIZE; j++)
{
if(secretWord[i]!=alphabet[j])
{
remaining_letters[j]=alphabet[j];
}
}
}
you cycle through the alphabet and every time the current letter in secret word is not matvching, you assign to remaining_letters. This will assignment will happen every time as there will be a letter in the alphabet that does not match the curent secretWord letter.
To fix this, you will need do something like this (note the inversion of the loops):
int secret_len = strlen(secretWord); // cache length, so we don't recalculate it
// every time: secretWord does not change
for(int j=0; j<ALPHABETSIZE; j++)
{
bool found = false; // assume the current ABC letter is not in secretWord
for(int i=0; i<secret_len; i++)
{
if(secretWord[i]!=alphabet[j])
{
found = true; // but it was
}
}
if (!found) // if it wasn't
{
remaining_letters[j]=alphabet[j];
}
}
Your main problem is that you do not keep the vowels count in the class member vowel_count. You store it in a local variable vowel_count and return it but the class member is never updated.

C++ will this function leak?

I have started out to write a simple console Yahtzee game for practice. I just have a question regarding whether or not this function will leak memory. The roll function is called every time the dices need to be re-rolled.
What it does is to create a dynamic array. First time it is used it will store 5 random values. For the next run it will only re-roll all except for the dice you want to keep. I have another function for that, but since it isn't relevant for this question I left it out
Main function
int *kast = NULL; //rolled dice
int *keep_dice = NULL; //which dice to re-roll or keep
kast = roll(kast, keep_dice);
delete[] kast;
and here's the function
int *roll(int *dice, int *keep) {
srand((unsigned)time(0));
int *arr = new int[DICE];
if(!dice)
{
for(int i=0;i<DICE;i++)
{
arr[i] = (rand()%6)+1;
cout << arr[i] << " ";
}
}
else
{
for(int i=0;i<DICE;i++)
{
if(!keep[i])
{
dice[i] = (rand()%6)+1;
cout << "Change ";
}
else
{
keep[i] = 0;
cout << "Keep ";
}
}
cout << endl;
delete[] arr;
arr = NULL;
arr = dice;
}
return arr;
}
Yes, it can leak. Just for example, using cout can throw an exception, and if it does, your delete will never be called.
Instead of allocating a dynamic array yourself, you might want to consider returning an std::vector. Better still, turn your function into a proper algorithm, that takes an iterator (in this case, a back_insert_iterator) and writes its output there.
Edit: Looking at it more carefully, I feel obliged to point out that I really dislike the basic structure of this code completely. You have one function that's really doing two different kinds of things. You also have a pair of arrays that you're depending on addressing in parallel. I'd restructure it into two separate functions, a roll and a re_roll. I'd restructure the data as an array of structs:
struct die_roll {
int value;
bool keep;
die_roll() : value(0), keep(true) {}
};
To do an initial roll, you pass a vector (or array, if you truly insist) of these to the roll function, which fills in initial values. To do a re-roll, you pass the vector to re-roll which re-rolls to get a new value for any die_roll whose keep member has been set to false.
Use a (stack-allocated) std::vector instead of the array, and pass a reference to it to the function. That way, you'll be sure it doesn't leak.
The way you allocate memory is confusing: memory allocated inside the function must be freed by code outside the function.
Why not rewrite it something like this:
int *kast = new int[DICE]; //rolled dice
bool *keep_dice = new bool[DICE]; //which dice to re-roll or keep
for (int i = 0; i < DICE; ++i)
keep_dice[i] = false;
roll(kast, keep_dice);
delete[] kast;
delete[] keep_dice;
This matches your news and deletes up nicely. As to the function: because we set keep_dice all to false, neither argument is ever NULL, and it always modifies dice instead of returning a new array, it simplifies to:
void roll(int *dice, int *keep) {
for(int i=0;i<DICE;i++)
{
if(keep[i])
{
keep[i] = false;
cout << "Keep ";
}
else
{
dice[i] = (rand()%6)+1;
cout << "Change ";
}
}
cout << endl;
}
Also, you should move the srand call to the start of your program. Re-seeding is extremely bad for randomness.
My suggestion would be to take time out to buy/borrow and read Scott Meyers Effective C++ 3rd Edition. You will save yourselves months of pain in ramping up to become a productive C++ programmer. And I speak from personal, bitter experience.