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

Dani AI

Generated

A quick, safe way to let the program prompt for two accounts is to read the three fields (account number, balance, rate) twice and call the list appending routine. Put this input loop where the program announces the two new items (before the program returns). As warned, avoid using the broken sorted insert until its logic is fixed — append is simpler and safe for adding items to the tail.

Example input loop (needs <limits> included):

#include <limits>

int accNum;
double bal, rate;
for (int i = 0; i < 2; ++i) {
    std::cout << "Enter account number, balance, rate: ";
    if (!(std::cin >> accNum >> bal >> rate)) {
        std::cerr << "Invalid input; try again\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        --i;
        continue;
    }
    accountList.appendNode(accNum, bal, rate);
}

The posted insertNode and deleteNode have concrete bugs that will corrupt a list. As put it, that function is dangerous: mixing three fields with logical && for ordering is wrong, and the deleteNode check uses commas (the comma operator) instead of logical operators. A safe sorted-insert that orders by account number only looks like:

void Account::insertNode(int number, double balance, double rate) {
    ListNode *prev = nullptr, *cur = head;
    while (cur && cur->value < number) { prev = cur; cur = cur->next; }
    if (!prev) head = new ListNode(number, balance, rate, head);
    else prev->next = new ListNode(number, balance, rate, cur);
}

Additional tips: validate input (as shown), prefer a single key for ordering (account number) or write a clear lexicographic comparator if all three fields matter, and fix deleteNode to use && for multi-field equality. If Account objects may be copied, implement or delete copy/assignment to follow the rule of three.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.