Outputting char pointer of nodes from linked list prints out garbage after calling display function.

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();
};

#include "Bank.h"
#include <iostream>

using namespace std;

Bank::Bank()
{
accountNumber = 9999;
balance = 0;
}
Bank::Bank(int accountNum, double bal)
{
accountNumber = accountNum;
balance = bal;
owner = nullptr;
}
double Bank::withdraw(double amount)
{
if (getBalance() < amount)
{
cout << "Insufficient balance." << endl;
balance = 0;
}
else
{
balance -= amount;
}
return balance;
}
double Bank::deposit(double amount)
{
balance += amount;
return balance;
}
int Bank::getAccountNumber()
{
return accountNumber;
}
double Bank::getBalance()
{
return balance;
}
char* Bank::getOwner()
{
return owner;
}
void Bank::setAccountNumber(int num)
{
accountNumber = num;
}
void Bank::setBalance(double num)
{
balance = num;
}
void Bank::setOwner(char* name)
{
int size = 0;
while (name[size] != '\0')
size++;
owner = new char[size+1];
size = 0;
while(name[size] != '\0')
{
owner[size] = name[size];
size++;
}
owner[size] = '\0';
}
Bank::~Bank()
{
delete[] owner;
}

Bank* Bank::getNext()
{
return next;
}
void Bank::setNext(Bank* n)
{
next = n;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Bank.h"
#include <iomanip>
#include <string>
using namespace std;

void displayAccountInfo(Bank obj);
void copystr(char* dst, char* src);
bool compstr(char* first, char* second);
int getLength(char* ptr);
void strConcatenate(char* dst, char* app);
void displayCString(char* ptr);

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();

inFile >> accountNum;
newBank->setAccountNumber(accountNum);

inFile >> balance;
newBank->setBalance(balance);

inFile.ignore();
inFile.getline(dummyString, 1000, '\n');

newBank->setOwner(dummyString);

info->appendBank(newBank);

displayAccountInfo(*newBank);
cout << newBank->getOwner();

}
char name[] = "John Hancock";
char* namePtr = name;

cout << "Searching for John Hancock account..." << endl;
Bank* search = info->findAccount(namePtr);
if (search != nullptr)
{
cout << "ACCOUNT FOUND: " << search->getOwner() << endl;
cout << "Amount: $" << search->getBalance() << endl;
cout << "Withdrawing... $600" << endl;
search->withdraw(600);
cout << "Amount: $" << search->getBalance() << endl;
cout << "Withdrawing... $300" << endl;
search->withdraw(300);
cout << "Amount: $" << search->getBalance() << endl;
cout << "Depositing... $400" << endl;
search->deposit(400);
cout << "Amount: $" << search->getBalance() << endl;
}
else
cout << "John Hancock account does not exist." << endl;
}
info->sortAscending();

delete[] dummyString;
delete info;
inFile.close();
return 0;
}

void displayAccountInfo(Bank obj)
{
cout << fixed << setprecision(2);
cout << "Account Number: " << obj.getAccountNumber() << endl;
cout << "Balance: " << obj.getBalance() << endl;
cout << "Owner: " << obj.getOwner() << endl;
cout << endl;
}

bool compstr(char* first, char* second)
{
int counter = 0;
if (getLength(first) != getLength(second)) // Checks size of both names
{
return false;
}
else
{
while (first[counter] != '\0')
{
if (first[counter] != second[counter])
return false;
counter++;
}
if (second[counter] == '\0')
return true;
}
return false;
}
int getLength(char* ptr)
{
int i = 0;
while (ptr[i] != '\0')
{
i++;
}
return i+1;
}

javascript:tx('output')
Account Number: 1000258915
Balance: 456.78
Owner: Homer J. Simpson

▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌Account Number: 1000251784
Balance: 893.67
Owner: John Hancock

▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌Account Number: 1000253791
Balance: 148.92
Owner: Jane Doe

▌▌▌▌▌▌▌▌▌▌▌▌▌Searching for John Hancock account...
John Hancock account does not exist.')
Last edited on
why did you reinvent strcpy (copyst) ?

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.
Last edited on
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.
Topic archived. No new replies allowed.