Help!! I have spent hours and hours doing this program every way I can think of.
ID: 3569607 • Letter: H
Question
Help!! I have spent hours and hours doing this program every way I can think of. It will work for one part of it and not another. Then, when I think the problem is fixed another errror or problem pops up. The main issue I was having is that the new account information was not showing after interest was calculated. The only thing that would show were the column hearders. I changed something and now I keep getting a linking error. I think I can go back and figure out what I did to cause the linking error but I still can't figure out how to Get the new account information to show. Thank you to someone who can help!!
Develop code to perform the daily account maintenance duties at Bank.com. Every
morning they want to examine the active accounts and either (a) give them interest on their savings or
(b) charge them interest on their credit.
Contents of a1.txt are as follows:
Jim 23.40 0
Bob 58.32 0
Sally -1.34 0
Below is code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
// Function prototypes
//[BEGIN MAIN]
int main()
{
std::string inputFileName;
std::string outputFileName;
double interestRate;
std::cout << "Enter an input filename: ";
std::cin >> inputFileName;
std::cout << "Enter an output filename: ";
std::cin >> outputFileName;
std::cout << "Enter an interest rate (%): ";
std::cin >> interestRate;
std::cout << std::endl;
std::ifstream ifile = GetInputFile(inputFileName);
std::ofstream ofile = GetOutputFile(outputFileName);
std::cout << "Current account status:" << std::endl;
PrintFileContents(ifile);
ProcessAccounts(ifile, ofile, interestRate);
ifile.close();
ofile.close();
std::cout << std::endl;
std::cout << "New account status:" << std::endl;
ifile = GetInputFile(outputFileName);
PrintFileContents(ifile);
ifile.close();
std::cout << "Press ENTER";
std::cin.ignore();
std::cin.get();
return 0;
}
//[END MAIN]
void ProcessAccounts(std::ifstream &ifile, std::ofstream& ofile, double rate)
{
// Every day the bank checks the status of the accounts and processes them
// If the customer has:
// 1) a positive savings balance, pay them interest
// 2) a negative savings balance, transfer it to credit (credit is positive)
// 3) a credit balance, charge them interest
// Note: Unlike normal banks, the interest rate for savings and credit is the same
// Read the info from the input file
// Put the new info into the output file
// The format for the input file and output file should be
// [NAME] [SAVINGS BALANCE] [CREDIT BALANCE]
// There is a single space between each value and a newline at the end
// Put your code here
}
void PrintFileContents(std::ifstream &str)
{
// Print the contents of the file
// First, print the file headers
// Then, print each line.
// Make sure the text is properly formatted
// Remember the functions available in iomanip?
// EXAMPLE:
// Name Savings Credit
// Bob $23.56 $0.00
// Joe $43.52 $0.00
// Sally -$1.58 $0.00
// Put your code here
}
std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream
// Put your code here
}
std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream
// Put your code here
}
void SetInputStreamPos(std::ifstream &str, int pos)
{
// Set the 'g'et cursor to the desired byte in the stream
// Use the beginning of the file as the reference point
// Put your code here
}
void SetOutputStreamPos(std::ofstream &str, int pos)
{
// Set the 'p'ut cursor to the desired byte in the stream
// Use the beginning of the file as the reference point
// Put your code here
}
Output should read:
Enter an input filename: a1.txt
Enter an output filename: a2.txt
Enter an interest rate (%): 25
Current account status:
Name Savings Credit
Jim $23.40 $0.00
Bob $58.32 $0.00
Sally -$1.34 $0.00
New account status:
Name Savings Credit
Jim $29.25 $0.00
Bob $72.90 $0.00
Sally $0.00 $1.68
Press ENTER
Explanation / Answer
include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
void main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
clrscr();
printf(" Program For Binary Search Tree ");
do {
printf(" 1.Create");
printf(" 2.Search");
printf(" 3.Recursive Traversals");
printf(" 4.Exit");
printf(" Enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf(" Enter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf(" Want To enter More Elements?(y/n)");
ans = getch();
} while (ans == 'y');
break;
case 2:
printf(" Enter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
printf(" Parent of node %d is %d", tmp->data, parent->data);
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf(" The Inorder display : ");
inorder(root);
printf(" The Preorder display : ");
preorder(root);
printf(" The Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf(" The %d Element is Present", temp->data);
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.