When I go to run the program all I get is continuous scrolling of the cout << \"
ID: 3772583 • Letter: W
Question
When I go to run the program all I get is continuous scrolling of the cout << "ERROR : invalid command " << command << " from bank.txt" << endl; line in the program window, but I really can't see where that is coming from, need a little help pleas, thank you in advance.
Main.cpp
#include<iostream>
#include<fstream>
#include<cmath>
#include <string>
using namespace std;
#include "Header.h"
int searchAccount(Account *accounts[], int size, int accNum)
{
for (int i = 0; i<size; i++)
{
if (accounts[i]->getId() == accNum)
{
return i;
}
}
return -1;
}
int main()
{
string command;
string junk;
int accNum, index;
double amount;
Account *accounts[10];
int numOfAccounts = 0;
ifstream infile("bank.txt");
infile.open("bank.txt");
if (!infile.is_open())
{
cout << "ERROR : Not able to open the file, bank.txt" << endl;
return 0;
}
while (!infile.eof())
{
infile >> command;
if (command == "Create")
{
infile >> accNum;
infile >> amount;
if (infile.peek() != ' ')
getline(infile, junk);
if (accNum > 0 && accNum < 10)
{
if (searchAccount(accounts, numOfAccounts, accNum) < 0)
{
accounts[numOfAccounts] = new Account(accNum, amount);
numOfAccounts++;
}
else
cout << "ERROR : The account with id " << accNum << " is already present" << endl;
}
else
cout << "ERROR : Account Number is not in the range 1-9" << endl;
}
else if (command == "Deposit")
{
infile >> accNum;
infile >> amount;
if (infile.peek() != ' ')
getline(infile, junk);
index = searchAccount(accounts, numOfAccounts, accNum);
if (index < 0)
cout << "ERROR : There is no account with id " << accNum << " present" << endl;
else
accounts[index]->deposit(amount);
}
else if (command == "Withdraw")
{
infile >> accNum;
infile >> amount;
if (infile.peek() != ' ')
getline(infile, junk);
index = searchAccount(accounts, numOfAccounts, accNum);
if (index < 0)
cout << "ERROR : There is no account with id " << accNum << " present" << endl;
else
accounts[index]->withdraw(amount);
}
else if (command == "Balance")
{
infile >> accNum;
if (infile.peek() != ' ')
getline(infile, junk);
index = searchAccount(accounts, numOfAccounts, accNum);
if (index < 0)
cout << "ERROR : There is no account with id " << accNum << " present" << endl;
else
cout << "Balance in account " << accNum << " is " << accounts[index]->getBalance() << endl;
}
else
{
if (infile.peek() != ' ')
getline(infile, junk);
cout << "ERROR : invalid command " << command << " from bank.txt" << endl;
}
}
cout << "End of text file. Good Bye!!" << endl;
return 0;
}
Header.h
#ifndef Header_H
#define Header_H
#include <iostream>
using namespace std;
class Account
{
private:
int id;
double balance;
public:
Account();
Account(int newId, double initialBalance);
int getId();
double getBalance();
void deposit(double amount);
void withdraw(double amount);
double balanceAmount();
};
Header.cpp
#include "Header.h"
using namespace std;
Account::Account() {}
Account::Account(int newId, double initialBalance)
{
id = newId;
balance = initialBalance;
}
int Account::getId()
{
return id;
}
double Account::getBalance()
{
return balance;
}
void Account::withdraw(double amount)
{
balance = balance - amount;
}
void Account::deposit(double amount)
{
balance = balance + amount;
}
Explanation / Answer
The program is meant to read commands from a text file with name bank.txt
The valid commands are Create, Deposit, Withdraw and Balance (CASE SENSITIVE)
if the file bank.txt contans any command other than above four, the invalid command error comes.
Create, Deposit and Withdraw commands have two arguments each Account number and amount.
Balance has only one argument Account number
So, a sample bank.txt looks like this
Create 1 10.0
Deposit 1 15.0
Withdraw 1 5.0
Balance 1
Create 2 20.0
Deposit 2 5.0
Withdraw 2 8
Balance 2
You can see bank.txt file and output of the program in the below link
http://www.chegg.com/homework-help/questions-and-answers/need-help-go-c-reads-commands-banktxt-file-program-must-process-following-four-commands-cr-q9949755
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.