The program successfully prints out the char pointer (owner), which displays the owner's name of the bank account. However, after calling the displayAccountInfo function, the char pointer (owner) now contains garbage before it. This prevents me from properly comparing the name with a manually inputted name stored in a c-string. The display function itself correctly outputs the owner name, but when trying to compare it afterward with another c-string it becomes garbage. I am unable to figure out why this is happening since I'm not altering the c-string through the function or afterwards; I'm only displaying it through cout. Help would be appreciated thank you!
javascript:tx('code')
#pragma once
class Bank {
private:
int accountNumber;
double balance;
char* owner;
Bank* next;
public:
Bank();
~Bank();
Bank(int accountNum, double bal);
double withdraw(double amount);
double deposit(double amount);
int getAccountNumber();
double getBalance();
char* getOwner();
void setAccountNumber(int num);
void setBalance(double num);
void setOwner(char* name);
void setNext(Bank* n);
Bank* getNext();
};
class LinkedList {
private:
Bank* head;
Bank* tail;
public:
LinkedList()
{
head = nullptr;
tail = nullptr;
}
void appendBank(Bank* newBank)
{
if (head != nullptr)
{
Bank* iter = head;
while (iter->getNext() != nullptr)
{
iter = iter->getNext();
}
iter->setNext(newBank);
tail = iter->getNext();
}
else
{
head = newBank;
tail = newBank;
}
}
Bank* findAccount(char* name)
{
if (head != nullptr)
{
int i = 0;
Bank* iter = head;
while (iter != nullptr)
{
if (compstr(iter->getOwner(), name))
{
return iter;
}
iter = iter->getNext();
}
return iter;
}
else
{
cout << "head is null" << endl;
return head;
}
}
Bank* getHead()
{
return head;
}
void sortAscending()
{
bool sorted = false;
while (!sorted)
{
sorted = true;
if (head == nullptr || head->getNext() == nullptr)
return;
Bank* iter = head;
while (iter != nullptr)
{
iter->setNext(iter->getNext());
if (iter->getBalance() > iter->getNext()->getBalance())
{
Bank* temp = iter;
iter = iter->getNext();
iter->setNext(temp);
sorted = false;
}
iter = iter->getNext();
}
}
}
Bank* setHead(Bank* n)
{
head = n;
}
};
int main()
{
LinkedList* info = new LinkedList();
char *dummyString = new char[1000];
string buffer;
int accountNum;
double balance;
ifstream inFile;
inFile.open("bankInfo.txt");
if (!inFile)
{
cout << "File could not be opened." << endl;
}
else
{
int i = 0;
while (!inFile.eof())
{
Bank* newBank = new Bank();
can you use code-tags? <> in the side editor, edit your post and apply them please. or [code ] (I see you tried but this forum didn't recognize your attempt).
Still Looking. I can't compile it, missing too much.
at a guess you are trying to do c-strings and kind of manhandling the char*s, probably messed that up. Your best bet may be to print the broken string at the start and end of each function that you call (cout << "function name start" << " " << owner << endl; … etc for function name end, in each one.. until you see where it goes wrong. Then look at that function in detail.
I managed to work past it. As far as why the c-string was getting corrupted after calling the function. I still don't know. But I simply reassigned the owner c-string to dummyString (which is where the original name was read in to) using the setOwner method from the bank class.