Need help with C++ I have all of this already (minor syntax errors) and need hel
ID: 3812230 • Letter: N
Question
Need help with C++
I have all of this already (minor syntax errors) and need help with is in picture. It is a continuing project this is what I have already need to figure out the rest.
Date.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include
using namespace std;
class Date
{
private:
int month;
int day;
int year;
public:
int getMonth();
int getDay();
int getYear();
void setMonth(int month);
void setDay(int day);
void setYear(int year);
void set(int month, int day, int year);
Date operator++()
{
month++;
day++;
year++;
return *this;
}
bool operator < (const Date & d)
{
if (year < d.year)
{
return true;
}
else if (year > d.year)
{
return false;
}
else if (month < d.month)
{
return true;
}
else if (month > d.month)
{
return false;
}
else if (day < d.day)
{
return true;
}
else if (day > d.day)
{
return false;
}
}
bool operator == (const Date & d)
{
if (month == d.month && day == d.day && year == d.year)
return true;
else
return false;
}
bool operator <= (const Date & d)
{
return (*this < d || *this == d);
}
void print()
{
cout << month << "/" << day << "/" << year << endl;
}
};
#endif
Date.cpp
#include "Date.h"
#include
using namespace std;
void Date::setMonth(int month_)
{
month = month_;
}
int Date::getMonth()
{
return month;
}
void Date::setDay(int day_)
{
day = day_;
}
int Date::getDay()
{
return day;
}
void Date::setYear(int year_)
{
year = year_;
}
int Date::getYear()
{
return year;
}
main.cpp
//Devante Drake
//CS 1337
//HW 4-3
#include
#include "Account.h"
#include "Date.h"
#include
#include
using namespace std;
int getMonth(string d)
{
d.substr(0, 2);
}
int getDay(string d)
{
d.substr(3, 2);
}
int getYear(string d)
{
d.substr(4, 4);
}
void main()
{
int accountNumber;
string name;
double balance, amount;
bool flag = false;
bool found = false;
int NUM_ACCOUNT = 5, i = 0;
Account *accounts;
accounts = new Account[NUM_ACCOUNT];
int total = 0;
const int NUM_DATES = 4;
string dateArray[NUM_DATES] = { "02/12/2017", "02/12/2018", "02/13/2017", "03/12/2017" };
cout << "*********************************** ";
cout << "Part A - Driver for the Date class: ";
cout << "*********************************** ";
int Date d1; d2;
for (int i = 0; i < NUM_DATES; i++)
for (int j = 0; j < NUM_DATES; j++)
{
month = getMonth(dateArray[i]);
day = getDay(dateArray[i]);
year = getYear(dateArray[i]);
d1.set(month, day, year);
cout << " ================ ";
cout << "d1: ";
d1.print();
month = getMonth(dateArray[j]);
day = getDay(dateArray[j]);
year = getYear(dateArray[j]);
d2.set(month, day, year);
cout << " d2: ";
d2.print();
cout << endl;
if (d1 < d2) cout << "d1 < d2 ";
if (d2 < d1) cout << "d2 < d1 ";
if (d1 == d2) cout << "d2 == d1 ";
if (d1 <= d2) cout << "d1 <= d2 ";
if (d2 <= d1) cout << "d2 <= d1 ";
}
cout << "*********************************** ";
cout << "****************END**************** ";
cout << "*********************************** ";
cout << " Hello, how may I help you today?" << endl;;
cout << endl;
do
{
//Lists the options
cout << "1. Create account with complete information" << endl;
cout << "2. Create default account with no parameters" << endl;
cout << "3. Deposit to account" << endl;
cout << "4. Withdraw from account " << endl;
cout << "5. Display information of all accounts" << endl;
cout << "6. Display totalNetDeposits" << endl;
cout << "7. Exit the program" << endl;
cout << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:// Will create the five accounts
if (i < NUM_ACCOUNT) {
flag = false;
cout << "Enter your account number : ";
cin >> accountNumber;
for (int x = 0; x < NUM_ACCOUNT; x++) {
if (accountNumber == accounts[x].getAccountNumber()) {
cout << "Account number already exist" << endl;
flag = true;
break;
}
}
if (!flag) {
accounts[i].setAccountNumber(accountNumber);
cin.ignore();
cout << "Enter your name : ";
getline(cin, name);
accounts[i].setName(name);
cout << "Enter amount : ";
cin >> balance;
accounts[i].setBalance(balance);
cout << endl;
i++;
}
}
else
cout << "Reached max number of accounts" << endl;
break;
case 2:
if (i < NUM_ACCOUNT)
i++;
else
cout << "Reached max number of accounts" << endl;
break;
case 3:
// Deposits money to account
cout << "Enter your account number : ";
cin >> accountNumber;
cout << "Enter amount : ";
cin >> amount;
cout << endl;
for (int x = 0; x < NUM_ACCOUNT; x++) {
if (accountNumber == accounts[x].getAccountNumber())
{
flag = accounts[x].deposit(amount);
if (flag) {
found = true;
cout << amount << " deposited in your account" << endl;
cout << "Your previous Account balance was " << accounts[x].getBalance() - amount << endl;
cout << "Your new Account balance is " << accounts[x].getBalance() << endl;
cout << endl;
Account::setTotalNetDeposits(Account::getTotalNetDeposits() + amount);
break;
}
else {
cout << "Amount is negative" << endl;
break;
}
}
}
if (!found)
cout << "Account number is not found" << endl;
cout << endl;
break;
case 4:
//Withdraws money from the account
cout << "Enter your account number : ";
cin >> accountNumber;
cout << "Enter amount : ";
cin >> amount;
for (int x = 0; x < NUM_ACCOUNT; x++) {
if (accountNumber == accounts[x].getAccountNumber())
{
flag = accounts[x].withdraw(amount);
if (flag) {
found = true;
cout << "Welcome! " << accounts[x].getName() << endl;
cout << "Transaction done" << endl;
cout << "Your remaining amount is : " << accounts[x].getBalance() << endl;
cout << endl;
Account::setTotalNetDeposits(Account::getTotalNetDeposits() - amount);
break;
}
else {
cout << "Insufficient balance" << endl;
break;
}
}
}
if (!found)
cout << "Account number is not found" << endl;
cout << endl;
break;
case 5:
//Displays information of all accounts
cout << "Account Number :" << setw(10) << "Account Holder name :" << setw(10) << " Account Balance :" << endl;
for (int z = 0; z < NUM_ACCOUNT; z++)
{
cout << setw(25) << left;
cout << accounts[z].getAccountNumber() << setw(19);
cout << accounts[z].getName() << setw(34);
cout << setw(45) << accounts[z].getBalance() << left << endl;
cout << endl;
}
break;
case 6:
total = 0;
for (int x = 0; x < NUM_ACCOUNT; x++) {
total += accounts[x].getBalance();
}
cout << "Total net deposits is: $" << total << endl << endl;
break;
case 7:
exit(0);
break;
default:
cout << "Wrong Choice" << endl;
}
} while (true);
}
Account.cpp
#include "Account.h"
#include
using namespace std;
double Account::totalNetDeposits;
int Account::numberAccounts;
Account::Account()
{
accountNumber = 9999;
ownerName = " ";
balance = 0.0;
}
Account::Account(int number, string name, double bal)
{
setAccount(number, name, bal);
}
void Account::setAccount(int number, string name, double bal)
{
accountNumber = number;
ownerName = name;
balance = bal;
setTotalNetDeposits(getTotalNetDeposits() + bal);
setNumberAccounts(getNumberAccounts() + 1);
}
double Account::getBalance()
{
return balance;
}
bool Account::withdraw(double amnt) // Checks the withdraw balance
{
if (amnt > getBalance())
{
return false;
}
else
{
setBalance(getBalance() - amnt);
return true;
}
}
bool Account::deposit(double amnt) //Checks the deposit balance
{
if (amnt < 0)
{
return false;
}
else
{
setBalance(getBalance() + amnt);
return true;
}
}
int Account::getAccountNumber() //Accessors
{
return accountNumber;
}
string Account::getName()
{
return ownerName;
}
void Account::setAccountNumber(int number) //Mutators
{
accountNumber = number;
}
void Account::setName(string name)
{
ownerName = name;
}
void Account::setBalance(double bal)
{
balance = bal;
setTotalNetDeposits(getTotalNetDeposits() + bal);
}
void Account::setTotalNetDeposits(double amount)
{
totalNetDeposits = amount;
}
double Account::getTotalNetDeposits()
{
return totalNetDeposits;
}
int Account::getNumberAccounts()
{
return numberAccounts;
}
void Account::setNumberAccounts(int num)
{
numberAccounts = num;
}
Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include
using namespace std;
class Account
{
private:
int accountNumber;
double balance;
string ownerName;
static double totalNetDeposits;
static int numberAccounts;
public:
Account(); //Default Constructor
Account(int number, string name, double bal);
bool withdraw(double amount);
bool deposit(double amount);
int getAccountNumber(); //Accessors
string getName();
double getBalance();
void setAccount(int number, string name, double bal); //Mutators
void setAccountNumber(int Account);
void setName(string name);
void setBalance(double amount);
static void setTotalNetDeposits(double amount);
static double getTotalNetDeposits();
static int getNumberAccounts();
static void setNumberAccounts(int num);
};
#endif
Explanation / Answer
I think you have got the following errors.
main.cpp:20:11: error: ‘::main’ must return ‘int’
void main()
^
main.cpp: In function ‘int main()’:
main.cpp:39:13: error: expected initializer before ‘d1’
int Date d1; d2;
^
main.cpp:39:17: error: ‘d2’ was not declared in this scope
int Date d1; d2;
^
main.cpp:43:12: error: ‘month’ was not declared in this scope
month = getMonth(dateArray[i]);
^
main.cpp:44:12: error: ‘day’ was not declared in this scope
day = getDay(dateArray[i]);
^
main.cpp:45:12: error: ‘year’ was not declared in this scope
year = getYear(dateArray[i]);
^
main.cpp:46:12: error: ‘d1’ was not declared in this scope
d1.set(month, day, year);
^
main.cpp:199:18: error: ‘exit’ was not declared in this scope
exit(0);
I have corrected them and also modified getYear, month day methods in main.cpp to return an integer. The following is the code.
main.cpp:
#include<iostream>
#include "Account.h"
#include "Date.h"
#include<string>
#include<iomanip>
using namespace std;
#include<stdlib.h>
int getMonth(string d)
{
return atoi(d.substr(0, 2).c_str());
}
int getDay(string d)
{
return atoi(d.substr(3, 2).c_str());
}
int getYear(string d)
{
return atoi(d.substr(4, 4).c_str());
}
int main()
{
int accountNumber;
string name;
double balance, amount;
bool flag = false;
bool found = false;
int NUM_ACCOUNT = 5, i = 0;
Account *accounts;
accounts = new Account[NUM_ACCOUNT];
int total = 0;
int month,year,day;
const int NUM_DATES = 4;
string dateArray[NUM_DATES] = { "02/12/2017", "02/12/2018", "02/13/2017", "03/12/2017" };
cout << "*********************************** ";
cout << "Part A - Driver for the Date class: ";
cout << "*********************************** ";
Date d1, d2;
for (int i = 0; i < NUM_DATES; i++)
for (int j = 0; j < NUM_DATES; j++)
{
month = getMonth(dateArray[i]);
day = getDay(dateArray[i]);
year = getYear(dateArray[i]);
d1.set(month, day, year);
cout << " ================ ";
cout << "d1: ";
d1.print();
month = getMonth(dateArray[j]);
day = getDay(dateArray[j]);
year = getYear(dateArray[j]);
d2.set(month, day, year);
cout << " d2: ";
d2.print();
cout << endl;
if (d1 < d2) cout << "d1 < d2 ";
if (d2 < d1) cout << "d2 < d1 ";
if (d1 == d2) cout << "d2 == d1 ";
if (d1 <= d2) cout << "d1 <= d2 ";
if (d2 <= d1) cout << "d2 <= d1 ";
}
cout << "*********************************** ";
cout << "****************END**************** ";
cout << "*********************************** ";
cout << " Hello, how may I help you today?" << endl;;
cout << endl;
do
{
//Lists the options
cout << "1. Create account with complete information" << endl;
cout << "2. Create default account with no parameters" << endl;
cout << "3. Deposit to account" << endl;
cout << "4. Withdraw from account " << endl;
cout << "5. Display information of all accounts" << endl;
cout << "6. Display totalNetDeposits" << endl;
cout << "7. Exit the program" << endl;
cout << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:// Will create the five accounts
if (i < NUM_ACCOUNT) {
flag = false;
cout << "Enter your account number : ";
cin >> accountNumber;
for (int x = 0; x < NUM_ACCOUNT; x++) {
if (accountNumber == accounts[x].getAccountNumber()) {
cout << "Account number already exist" << endl;
flag = true;
break;
}
}
if (!flag) {
accounts[i].setAccountNumber(accountNumber);
cin.ignore();
cout << "Enter your name : ";
getline(cin, name);
accounts[i].setName(name);
cout << "Enter amount : ";
cin >> balance;
accounts[i].setBalance(balance);
cout << endl;
i++;
}
}
else
cout << "Reached max number of accounts" << endl;
break;
case 2:
if (i < NUM_ACCOUNT)
i++;
else
cout << "Reached max number of accounts" << endl;
break;
case 3:
// Deposits money to account
cout << "Enter your account number : ";
cin >> accountNumber;
cout << "Enter amount : ";
cin >> amount;
cout << endl;
for (int x = 0; x < NUM_ACCOUNT; x++) {
if (accountNumber == accounts[x].getAccountNumber())
{
flag = accounts[x].deposit(amount);
if (flag) {
found = true;
cout << amount << " deposited in your account" << endl;
cout << "Your previous Account balance was " << accounts[x].getBalance() - amount << endl;
cout << "Your new Account balance is " << accounts[x].getBalance() << endl;
cout << endl;
Account::setTotalNetDeposits(Account::getTotalNetDeposits() + amount);
break;
}
else {
cout << "Amount is negative" << endl;
break;
}
}
}
if (!found)
cout << "Account number is not found" << endl;
cout << endl;
break;
case 4:
//Withdraws money from the account
cout << "Enter your account number : ";
cin >> accountNumber;
cout << "Enter amount : ";
cin >> amount;
for (int x = 0; x < NUM_ACCOUNT; x++) {
if (accountNumber == accounts[x].getAccountNumber())
{
flag = accounts[x].withdraw(amount);
if (flag) {
found = true;
cout << "Welcome! " << accounts[x].getName() << endl;
cout << "Transaction done" << endl;
cout << "Your remaining amount is : " << accounts[x].getBalance() << endl;
cout << endl;
Account::setTotalNetDeposits(Account::getTotalNetDeposits() - amount);
break;
}
else {
cout << "Insufficient balance" << endl;
break;
}
}
}
if (!found)
cout << "Account number is not found" << endl;
cout << endl;
break;
case 5:
//Displays information of all accounts
cout << "Account Number :" << setw(10) << "Account Holder name :" << setw(10) << " Account Balance :" << endl;
for (int z = 0; z < NUM_ACCOUNT; z++)
{
cout << setw(25) << left;
cout << accounts[z].getAccountNumber() << setw(19);
cout << accounts[z].getName() << setw(34);
cout << setw(45) << accounts[z].getBalance() << left << endl;
cout << endl;
}
break;
case 6:
total = 0;
for (int x = 0; x < NUM_ACCOUNT; x++) {
total += accounts[x].getBalance();
}
cout << "Total net deposits is: $" << total << endl << endl;
break;
case 7:
exit(0);
break;
default:
cout << "Wrong Choice" << endl;
}
} while (true);
return 0;
}
Date.cpp:
#include "Date.h"
#include<iostream>
using namespace std;
void Date::setMonth(int month_)
{
month = month_;
}
int Date::getMonth()
{
return month;
}
void Date::setDay(int day_)
{
day = day_;
}
int Date::getDay()
{
return day;
}
void Date::setYear(int year_)
{
year = year_;
}
int Date::getYear()
{
return year;
}
void Date::set(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
Date.h
#ifndef DATE_H
#define DATE_H
#include<iostream>
using namespace std;
class Date
{
private:
int month;
int day;
int year;
public:
Date(){month=day=year=0;}
int getMonth();
int getDay();
int getYear();
void setMonth(int month);
void setDay(int day);
void setYear(int year);
void set(int month, int day, int year);
Date operator++()
{
month++;
day++;
year++;
return *this;
}
bool operator < (const Date & d)
{
if (year < d.year)
{
return true;
}
else if (year > d.year)
{
return false;
}
else if (month < d.month)
{
return true;
}
else if (month > d.month)
{
return false;
}
else if (day < d.day)
{
return true;
}
else if (day > d.day)
{
return false;
}
}
bool operator == (const Date & d)
{
if (month == d.month && day == d.day && year == d.year)
return true;
else
return false;
}
bool operator <= (const Date & d)
{
return (*this < d || *this == d);
}
void print()
{
cout << month << "/" << day << "/" << year << endl;
}
};
#endif