Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following C++ code runs fine on my classmates compiler but not for me, I wan

ID: 3858376 • Letter: T

Question

The following C++ code runs fine on my classmates compiler but not for me, I wanted to know why this may be?

This is my copilation command: g++ -std=c++11 Bank.cpp ATM.cpp -o Bank

This is my execution command: ./Bank

************* BELOW: Bank.cpp ***********************

#include "ATM.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void printAccount(ATM account);
void askAccountDetails(ATM account);
void archiveAccounts(ATM* accounts);
ATM* grabAccounts();
void updateAccount(ATM account,ATM* accounts);


int main(int argc, char const *argv[]) {

int numOfAccounts = 100;
ATM* accounts = grabAccounts();
int input;

do {

// Prompt User
cout << "Welcome to Bank Of The Student's! ";
cout << "How may I help you today ";
cout << "Enter Number To Make Choice (-1 to Exit Program) ";

// Present Menu
if (accounts[0].getName() == "" && accounts[0].getAccountNumber() == "") {
cout << "1. Create Account ";
}else {
cout << "1. Create Account ";
cout << "2. Already Have An Account ";
}

cin >> input;
cin.ignore();

switch (input) {
case 1: {
string firstName,lastName;
string accountNumber;
float balance;

cout << "Enter First Name: ";
cin >> firstName;

cout << "Enter Last Name: ";
cin >> lastName;

cout << "Enter Account Number: ";
cin >> accountNumber;

cout << "Enter Your Balance: ";
cin >> balance;

ATM newAccount = ATM(firstName,lastName,accountNumber,balance);

// Search for empty slot
for(int i = 0;i < numOfAccounts;i++){
if (accounts[i].getFirstName() == "") {
accounts[i] = newAccount;
break;
}
}

printAccount(newAccount);
archiveAccounts(accounts);
break;
}// End case 1
case 2: {
int bankInput;
int index;
cout << "Enter Number To Make Choice ";
cout << "1. Enter Name: ";
cout << "2. Enter Account Number: ";

cin >> bankInput;
cin.ignore();

switch (bankInput) {
case 1:{
cout << "Enter your name: ";
string name;
getline(cin,name);
cin.ignore();

// Search for person name in array
for(int i = 0;i < numOfAccounts;i++){
string currentAccountName = accounts[i].getName();
transform(currentAccountName.begin(), currentAccountName.end(), currentAccountName.begin(), ::tolower);
transform(name.begin(), name.end(), name.begin(), ::tolower);
if (currentAccountName == name) {
index = i;
break;
}
}
break;
}
case 2:{
cout << "Enter your account number: ";
string accountNumber;
cin.ignore();
getline(cin,accountNumber);
cin.ignore();

// Search for person account number in array
for(int i = 0;i < numOfAccounts;i++){
string currentAccountNumber = accounts[i].getAccountNumber();
if (currentAccountNumber == accountNumber) {
index = i;
break;
}
}
break;
}

default:
break;
}// End inner switch statement
askAccountDetails(accounts[index]);
updateAccount(accounts[index],accounts);
input = -1;
}// End case 2
break;

default:
break;
}
}while (input != -1);
return 0;
}


void printAccount(ATM account){
cout << "Account Name: " << account.getFirstName() << " " << account.getLastName() << endl;
cout << "Account Number: " << account.getAccountNumber() << endl;
cout << "Balance: $ " << account.getBalance() << endl << endl << endl;
}


void askAccountDetails(ATM account){
cout << "1. Withdraw ";
cout << "2. Deposit ";
cout << "3. Print Reciept ";
cout << "4. Exit ";

int input;
cin >> input;

switch (input) {
case 1:{
cout << "Withdraw Amount: ";
cout << "1. $20 2. $40 3. $100 ";
cout << "4. Quick Cash ";
int withdrawInput;
cin >> withdrawInput;
switch (withdrawInput) {
case 1:
account.withdraw(20);
printAccount(account);
break;
case 2:
account.withdraw(40);
printAccount(account);
break;
case 3:
account.withdraw(100);
printAccount(account);
break;
case 4:
int withdraw;
cout << "Enter amount: ";
cin >> withdraw;
account.withdraw(withdraw);
printAccount(account);
break;
default:
cout << "Invalid input! ";
break;
}

break;
}
case 2:{
cout << "Deposit Amount: ";
cout << "1. $20 2. $40 3. $100 ";
cout << "4. Quick Cash ";
int depositInput;
cin >> depositInput;
switch (depositInput) {
case 1:
account.deposit(20);
printAccount(account);
break;
case 2:
account.deposit(40);
printAccount(account);
break;
case 3:
account.deposit(100);
printAccount(account);
break;
case 4:
int deposit;
cout << "Enter amount: ";
cin >> deposit;
account.deposit(deposit);
printAccount(account);
break;
default:
cout << "Invalid input! ";
break;
}

break;
}
case 3:
printAccount(account);
break;
case 4:
break;
default:
break;
}
}

ATM* grabAccounts(){
ifstream input;
input.open("accounts.txt");
int index = 0;
string name,info;
ATM* accounts = new ATM[100];

while (!input.eof()) {
input >> name;
ATM account = ATM();
account.setFirstName(name);
for (int i = 0; i < 3; i++) {
input >> info;
if(i == 0)
account.setLastName(info);
else if(i == 1){
account.setAccountNumber(info);
}else{
account.setBalance(stoi(info));
}
}
accounts[index] = account;
index++;
}
return accounts;
}

void archiveAccounts(ATM* accounts){
ofstream output;
output.open("accounts.txt");

for (int i = 0; i < 100; i++) {
if (accounts[i].getFirstName() != "" && accounts[i].getAccountNumber() != "") {
output << accounts[i].getName();
output << " " << accounts[i].getAccountNumber();
output << " " << accounts[i].getBalance() << endl;
}
}

output.close();
}

void updateAccount(ATM account,ATM* accounts){

ofstream output;
output.open("accounts.txt");

for (int i = 0; i < 100; i++) {
if(accounts[i].getFirstName() == account.getFirstName() && accounts[i].getAccountNumber() == account.getAccountNumber()){
output << accounts[i].getFirstName() << " " << accounts[i].getLastName();
output << " " << accounts[i].getAccountNumber();
output << " " << accounts[i].getBalance() << endl;
}
}
}

************* BELOW: ATM.cpp ***********************

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

using namespace std;

ATM::ATM(){
this->firstName = "";
this->lastName = "";
this->accountNumber = "";
this->balance = 0.0;
}

ATM::ATM(string firstName,string lastName,string accountNumber,float balance){
this->firstName = firstName;
this->lastName = lastName;
this->accountNumber = accountNumber;
this->balance = balance;
}

// Setters
void ATM::setFirstName(string firstName){
this->firstName = firstName;
}

void ATM::setLastName(string lastName){
this->lastName = lastName;
}

void ATM::setAccountNumber(string accountNumber){
this->accountNumber = accountNumber;
}

void ATM::setBalance(float balance){
this->balance = balance;
}

// Getters
string ATM::getName(){
return this->firstName + " " + this->lastName;
}

string ATM::getFirstName(){
return this->firstName;
}

string ATM::getLastName(){
return this->lastName;
}

string ATM::getAccountNumber(){
return this->accountNumber;
}

float ATM::getBalance(){
return this->balance;
}

// Helpers
void ATM::deposit(float deposit){
this->lastDeposit = deposit;
setBalance(deposit + this->balance);
}

void ATM::withdraw(float withdraw){
if(this->balance <= 0.0){
cout << "Insuffecent Funds" << endl;
}else if ((this->balance - withdraw) < 0.0) {
this->lastWithdraw = this->balance;
setBalance(0.0);
}else{
setBalance(this->balance - withdraw);
}
}

************* BELOW: ATM.h ***********************


#ifndef ATM_H_
#define ATM_H_

#include <iostream>
using namespace std;

class ATM {

// Private Members
private:
string firstName;
string lastName;
string accountNumber;
float balance;
float lastDeposit;
float lastWithdraw;

public:
// Constructors
ATM();
ATM(string,string,string,float);

// Setters
void setFirstName(string);
void setLastName(string);
void setAccountNumber(string);
void setBalance(float);

// Getters
string getName();
string getFirstName();
string getLastName();
string getAccountNumber();
float getBalance();

// Helper Methods
void deposit(float);
void withdraw(float);


};

#endif

Explanation / Answer

Hi,

The reason for this program not working on your end but working perfectly fine on your classmates' computer might be one of the following:

1. The version of g++ might be mismatching. Use "g++ -v" or "gcc -v" on your friends' computer as well as on yours and compare your versions as to whether they're same.

2. If there is a version mismatch, install a specific version using "sudo apt-get install package=version". For example, sudo apt-get install g++=4.4

3. Check the directories in which your code lies. There may be import errors.

4. Check the C++ version you have.

Mention in the comments after performing the above steps. We shall further try together to solve this issue.