0

Hey guys, I have created the following code and I can not figure out how to do one thing, add users. I can do it manually, but I need to prompt the user for two new accounts and then add them to the list. Can someone please inform me of how I need to do this or show me, I would greatly appreciate it!

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Account class
class Account {
private:
	class ListNode
	{
		friend class Account;
		int value;
		double valuetwo;
		double valuethree;
		ListNode *next;

		//Constructor
		ListNode(int value1, double value2, double value3, ListNode *next1 = NULL)
		{
			value = value1;
			valuetwo = value2;
			valuethree = value3;
			next = next1;
		}
	};
	ListNode *head;

	int number;
	double balance;
	double rate;

public:
	Account()
	{ head = NULL; }
	~Account();
	Account (int, double, double);
	void appendNode(int, double, double);
	void insertNode(int, double, double);
	void deleteNode(int, double, double);
	void displayList();


	void setNumber(int);
    void setBalance(double);
    void setRate(double);

	int getNumber();
    double getBalance();
    double getRate();
	void account_details();
	string string_account_details();
};


Account::Account(int number, double balance, double rate) {
                Account::number = number;
                Account::balance = balance;
				Account::rate= rate;
}
void Account::setNumber(int number) {
    Account::number = number;
}

void Account::setBalance(double balance) {
    Account::balance = balance;
}

void Account::setRate(double rate) {
    Account::rate = rate;
}

int Account::getNumber() {
    return number;
}
double Account::getBalance() {
    return balance;
}
double Account::getRate() {
    return rate;
}

void Account::account_details() {
	cout << "The current account number is " << number <<
		" with a balance of $" << balance << " and an interest rate of " << rate << ".\n\n";
}
#include "Account.h"
#include <iostream>
using namespace std;

//Append node
void Account::appendNode(int number, double balance, double rate)
	{
		if (head == NULL)
			head = new ListNode(number, balance, rate);
		else
		{
			//List is not empty
			ListNode *nodePtr;
			nodePtr = head;
			
			while (nodePtr->next != NULL)
				nodePtr = nodePtr->next;

			nodePtr->next = new ListNode(number, balance, rate);
		}
	}

//Display list
void Account::displayList()
{
	ListNode *nodePtr;

	nodePtr = head;
	while (nodePtr)
	{
		cout << "The account number is " << nodePtr->value << ", the balance of the account is $"<< 
		nodePtr->valuetwo <<", and the account rate is " << nodePtr->valuethree <<"\n";
		nodePtr = nodePtr->next;
	}
}

//Insert Node Function
void Account::insertNode(int number, double balance, double rate)
{
	ListNode *nodePtr, *previousNodePtr;

	if ( head ==NULL || head ->value >= number || head ->valuetwo >= balance || head ->valuethree >= rate)
	{
		head = new ListNode(number, balance, rate, head);
	}
	else
	{
		previousNodePtr = head;
		nodePtr = head->next;

		while (nodePtr != NULL && nodePtr->value < number && nodePtr->valuetwo < balance && nodePtr->valuethree < rate)
		{
			previousNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}

		previousNodePtr->next = new ListNode(number, balance, rate, nodePtr);
	}
}

//DeleteNode Function
void Account::deleteNode(int number, double balance, double rate)
{
	ListNode *nodePtr, *previousNodePtr;

	if (!head)
		return;

	if ( head->value == number, head->valuetwo == balance, head->valuethree == rate)
	{
		nodePtr = head;
		head = head->next;
		delete nodePtr;
	}
	else
	{
		nodePtr = head;

		while (nodePtr != NULL && nodePtr->value != number && nodePtr->valuetwo != balance && nodePtr->valuethree != rate)
		{
			previousNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}

		if (nodePtr)
		{
			previousNodePtr->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

//Destructor
Account::~Account()
{
	ListNode *nodePtr, *nextNodePtr;

	nodePtr = head;
	while (nodePtr!=NULL)
	{
		nextNodePtr = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNodePtr;
	}
}

int main() {

	Account accountList;

	int catchVal;
	double catchVal2;
	double catchVal3;
	
	//Get Account information
	cout << "Getting the first account information \n";
	accountList.appendNode(126455, 1000.54, .03);
	
	cout << "Getting the second account information \n";
	accountList.appendNode(254774, 1500.44, .03);
	
	cout << "Getting the third account information \n";
	accountList.appendNode(333411, 230.00, .02);
	
	cout << "Getting the fourth account information \n";
	accountList.appendNode(491377, 1950.50, .03);
	
	cout << "Getting the fifth account information \n";
	accountList.appendNode(677134, 5164.11, .04);
	
	cout << "Getting the sixth account information \n";
	accountList.appendNode(765473, 2540.10, .04);

	cout << "\nHere is the information in the list \n";
	accountList.displayList();
	cout << endl;

	cout << "Adding 2 new items \n";
	cout << "_____________________________________________________________ \n";

		return 0;
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Banfa
0

You mean that you don't know how to use cin to obtain user input to insert into your list or that you don't know how to insert data?

BTW Account::insertNode is a disaster that in most cases will cause your list to corrupt.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.