I am having a hard time figuring this out. Everything else in my project is working so far, but when I try to add to my array of objects, it crashes. I get an error of cannot read string. This is the message, I have tried so many things. Here is the error, and it crashes after the input on the SetNewAccountInfo() function. I am new to programming, and hope this code is formatted on here right. This is a school project so I'm not asking for the answer, maybe just explain why I am unable to add to my array in laymen terms. Pointers and such are confusing so far.
static _Elem *__CLRCALL_OR_CDECL copy(_Elem *_First1, const _Elem *_First2, size_t _Count)
{ // copy [_First2, _First2 + _Count) to [_First1, ...)
**return (_Count == 0 ? _First1** : (_Elem *)_CSTD memcpy(_First1, _First2, _Count));
}
// TestClassC.cpp : main project file.
#include "stdafx.h"
#include "BankClassType.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
using namespace std;
void MainMenu(BankClassType bo[],int num, int cAcct);
void AcctOptionsMenu(BankClassType bo[], int num, int cAcct);
int SetNewAccountInfo();
void PrintAccountInfo();
void AccountDeposit();
void AccountWithdrawal();
int main()
{
const int num = 4;
BankClassType *bo = new BankClassType[num];
BankClassType b;
BankClassType bo0("12345", 'P', "Gates", "Bill", 175253.99, 6875250.23);
BankClassType bo1("12346", 'R', "Jones", "Tommy", 845.21, 2700.00);
BankClassType bo2("12347", 'P', "Asimov", "Isaac", 300.67, 14750.29);
bo[0] = bo0;
bo[1] = bo1;
bo[2] = bo2;
int cAcct = 3;
b.MainMenu(bo, num, cAcct);
_getch();
return 0;
}
//BankClassType.h File
#pragma once
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
#ifndef BankClassType_h
#define BankClassType_h
using namespace std;
class BankClassType
{
private:
string AccountNumber;
string FirstName;
string LastName;
double CheckingBalance;
double SavingsBalance;
char AccountType;
public:
BankClassType()
{
FirstName = "";
LastName = "";
AccountNumber = "";
AccountType = 'p';
CheckingBalance = 0;
SavingsBalance = 0;
}
BankClassType(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal);
//~BankClassType();
void SetNewAccountInfo(BankClassType bo[], int num, int cAcct);
void PrintAccountInfo();
void AccountDeposit(BankClassType bo[], int number, int num, int cAcct);
void AccountWithdrawal(BankClassType bo[], int number, int num, int cAcct);
void AcctOptionsMenu(BankClassType bo[], int num, int cAcct);
void MainMenu(BankClassType bo[], int num, int cAcct);
//void ExistingAccounts(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal);
string getAccountNumber()
{
return AccountNumber;
}
char getAccountType()
{
return AccountType;
}
double getCheckingBalance(double amount)
{
CheckingBalance = CheckingBalance + amount;
return CheckingBalance;
}
double getSavingsBalance(double amount)
{
SavingsBalance = SavingsBalance + amount;
return SavingsBalance;
}
double getWithdrawalCheckingBalance(double amount)
{
CheckingBalance = CheckingBalance - amount;
return CheckingBalance;
}
double getWithdrawalSavingsBalance(double amount)
{
SavingsBalance = SavingsBalance - amount;
return SavingsBalance;
}
string getFirstName()
{
return FirstName;
}
string getLastName()
{
return LastName;
}
};
#endif
//BankClassImp.cpp
#include "stdafx.h"
#include "BankClassType.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
using namespace std;
BankClassType::BankClassType(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal)
{
AccountNumber = acctNumber;
AccountType = acctType;
LastName = lname;
FirstName = fname;
CheckingBalance = cBal;
SavingsBalance = sBal;
/*cout << "\nName: " << LastName << ", " << FirstName;
cout << "\nAccountNumber: " << AccountType << AccountNumber;
cout << "\nChecking Balance: " << CheckingBalance;
cout << "\nSavings Balance: " << SavingsBalance<<endl;
*/
}
/*BankClassType::~BankClassType()
{
delete[] FirstName;
delete[] LastName;
delete[] AccountNumber;
delete[] AccountType;
}*/
void BankClassType::SetNewAccountInfo(BankClassType bo[], int num, int cAcct)
{
//BankClassType * bt = new BankClassType;
BankClassType c;
string fName;
string acctNum;
string lName;
char accType;
double cBal, sBal;
if (cAcct >= 5)
{
"Sorry the accounts are currently full, press any key to return to main menu: ";
c.MainMenu(bo, num, cAcct);
_getch();
}
else
cout << "\nEnter the 5 digit account number: "; cin >> acctNum;
cout << "\nEnter the account type (p) or (r): "; cin >> accType;
cout << "\nEnter the first name: "; cin >> fName;
cout << "\nEnter the last name: "; cin >> lName;
cout << "\nEnter the checking balance: "; cin >> cBal;
cout << "\nEnter the savings balance: "; cin >> sBal;
BankClassType bo3(acctNum, accType, lName, fName, cBal, sBal);
bo[cAcct + 1] = bo3;
}
void BankClassType::PrintAccountInfo()
{
cout << "Name: " << LastName << ", " << FirstName;
cout << "\nAccountNumber: " << AccountType << AccountNumber;
cout << "\nChecking Balance: " << CheckingBalance;
cout << "\nSavings Balance: " << SavingsBalance << endl << endl;
}
void BankClassType::AccountDeposit(BankClassType bo[], int number, int num, int cAcct)
{
int choice;
BankClassType b;
cout << "\nEnter 1 for checking or 2 for savings: ";
cin >> choice;
if (choice == 1)
{
double amount;
cout << "Enter amount to deposit into checking: " << endl;
cin >> amount;
bo[number].getCheckingBalance(amount);
cout << "You added $" << amount << " to checking account number " << bo[number].getAccountNumber();
_getch();
b.MainMenu(bo, num, cAcct);
}
else if (choice == 2)
{
double amount;
cout << "Enter amount to deposit into savings: " << endl;
cin >> amount;
bo[number].getSavingsBalance(amount);
cout << "You added $" << amount << " to savings number" << bo[number].getAccountNumber();
_getch();
b.MainMenu(bo, num, cAcct);
}
else
{
cout << "Incorrect Input, try again: ";
_getch();
AccountDeposit(bo, number, num, cAcct);
}
}
void BankClassType::AccountWithdrawal(BankClassType bo[], int number, int num, int cAcct)
{
int choice;
BankClassType b;
cout << "\nEnter 1 for checking or 2 for savings: ";
cin >> choice;
if (choice == 1)
{
double amount;
cout << "Enter amount to withdrawal from checking: " << endl;
cin >> amount;
bo[number].getWithdrawalCheckingBalance(amount);
cout << "You withdrew $" << amount << " from checking account number " << bo[number].getAccountNumber();
cout << "\nPress any key to return to main menu: ";
_getch();
b.MainMenu(bo, num, cAcct);
}
else if (choice == 2)
{
double amount;
cout << "Enter amount withdrawal from savings: " << endl;
cin >> amount;
bo[number].getWithdrawalSavingsBalance(amount);
cout << "You withdrew $" << amount << " from savings account number " << bo[number].getAccountNumber();
_getch();
b.MainMenu(bo, num, cAcct);
}
else
{
cout << "Incorrect Input, try again";
_getch();
AccountDeposit(bo, number, num, cAcct);
}
}
void BankClassType::MainMenu(BankClassType bo[], int num, int cAcct)
{
cout << "\nWelcome, here are the current accounts on file:\n" << endl;
for (int i = 0; i < cAcct; i++)
{
cout << i + 1 << "--";
bo[i].PrintAccountInfo();
}
int choice;
BankClassType b;
cout << "Enter number (1-5) of account you wish to make changes\n Enter 6 to add an account\nEnter 7 to exit\n --->";
cin >> choice;
if (choice < 6)
b.AcctOptionsMenu(bo, num, cAcct);
else if (choice == 6)
{
bo[cAcct + 1].SetNewAccountInfo(bo, num, cAcct);
cout << "You have successfully added an account!" << endl;
_getch();
cAcct++;
b.MainMenu(bo, num, cAcct);
}
else if (choice == 7)
{
cout << "You have exited, press any key to continue: ";
_getch();
system("cls");
}
else if(choice > 7)
{
cout << "Not a valid input, press a key to try again: ";
_getch();
system("cls");
MainMenu(bo, num, cAcct);
}
}
void BankClassType::AcctOptionsMenu(BankClassType bo[], int num, int cAcct)
{
char choice;
int check = 0;
int count = 0;
int number;
BankClassType b;
while (check == 0) {
cout << "\t\t\n\n" << "Main Menu";
cout << "\t\n\n" << "Select by letter:";
cout << "\t\n" << "(d) - Deposits";
cout << "\t\n" << "(w) - Withdrawals";
cout << "\t\n" << "(s) - Show Account Information.";
cout << "\t\n" << "(q) - Quit Program.\n\n";
cout << "\t" << "Choice: ";
choice = _getche();
switch (choice) {
case 'd':
case 'D':
system("cls");
cout << "\nChoose Account:\n ";
for (int i = 0; i < cAcct; i++) cout << i +1 << "---" << bo[i].getAccountNumber()<< "\n\n" ;
cin >> number;
bo[number].AccountDeposit(bo,number, num, cAcct);
system("cls");
break;
case 'w':
case 'W':
system("cls");
cout << "\nChoose Account:\n ";
for (int i = 0; i < cAcct; i++) cout << i + 1<< "---" << bo[i].getAccountNumber() << "\n\n";
cin >> number;
bo[number].AccountWithdrawal(bo, number, num, cAcct);
system("cls");
break;
case 's':
case 'S':
system("cls");
cout << "\nChoose Account:\n ";
for (int i = 0; i < cAcct; i++) cout << i + 1 << "---" << bo[i].getAccountNumber() << "\n\n";
cin >> number;
bo[number].PrintAccountInfo();
_getche();
system("cls");
break;
case 'q':
case 'Q':
check = 1;
break;
default:
cout << "\nInvalid selection. Press a key to return to main menu.";
_getche();
}
if (check == 1)
break;
}
}
It looks like the array allocated in main is too short. You allocate a block of 4 elements, pass '3' as the cAcct, then bo[cAcct + 1] = bo3. The index will be 4 which is one pass the end of the array. Results are undefined and a crash is likely
Related
I have an assignment here that I've been working on for the past few hours and have been met with a problem. Whenever I compile and run the program in VS Code it throws me into an infinite loop where the text of the "menu" is printed repeatedly. I imagine this has something to do with the for(;;){ loop at the beginning of the menu.
I've deleted the for(;;){ statement at the beginning of the menu, and the continuous scrolling stopped, but I was unable to input any numbers (cases) and the program, essentially, just printed the menu and that was the end.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student {
string name;
float GPA;
public:
string getName() const
{
return name;
}
float getGPA() const
{
return GPA;
}
void setName(string Name)
{
name = Name;
}
void setGPA(float gpa)
{
GPA = gpa;
}
Student(const string& name, float gpa)
: name(name)
, GPA(gpa)
{
}
Student()
{
}
void printDetails(Student s[], int n)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n; i++) {
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
float calcAverageGPA(Student s[], int n)
{
float avg = 0;
float sum = 0;
for (int i = 0; i <= n; i++) {
sum += s[i].getGPA();
}
avg = sum / n;
return avg;
}
float getGPAbyName(Student s[], int n, string name)
{
for (int i = 0; i <= n - 1; i++) {
if (s[i].getName() == name)
return s[i].getGPA();
}
return -1;
}
void showListByGPA(Student s[], int n, float gpa)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n - 1; i++) {
if (s[i].getGPA() > gpa)
;
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
};
int main()
{
int n = 0;
int ch;
Student s[50];
string name;
float GPA;
float avg = 0;
cout << "======================" << endl;
cout << "(1): Add a student" << endl;
cout << "(2): Print the details of a student" << endl;
cout << "(3): Get the GPA of a Student by name." << endl;
cout << "(4): Get names of students based on GPA." << endl;
cout << "(6): Quit the program." << endl;
cout << "Enter your option" << endl;
switch (ch) {
case '1':
cout << "\nEnter student's name" << endl;
cin >> name;
cout << "Enter GPA" << endl;
cin >> GPA;
s[n] = Student(name, GPA);
n++;
break;
case '2':
s[0].printDetails(s, n);
break;
case '3':
cout << "\nEnter the name of a student" << endl;
cin >> name;
GPA = s[0].getGPAbyName(s, n, name);
if (GPA == -1) {
cout << "\nStudent not found!" << endl;
}
else
cout << "\nGPA is: " << endl;
break;
case '4':
cout << "\nEnter GPA: " << endl;
cin >> GPA;
s[0].showListByGPA(s, n, GPA);
break;
case '5':
avg = s[0].calcAverageGPA(s, n);
cout << "\nAverage GPA: " << avg << endl;
break;
case '6':
exit(0);
}
}
I suspect the problem resides in main(). I included the prior blocks of the program in case they were necessary to provide any suggestions.
You obviously want to read in "ch".
You've also made this an int, and don't zero-initialize, which can cause some undefined behaviour if you don't set it beforehand.
The switch case should be
switch(X){
case 1:
// Code
break;
}
etc..
The difference is "1" or 1. It evaluates an enumeration value, instead of strings.
For safety: you might want to add a case default, which is common practice.
I have to write a program for bank.The classes if you will say I will provide but the problem is here,
My Problem is that in my main program I first call a function called BankProgram(); But there are other statements in the function after the function is being called The below code isnt executed at all
cout << "Enter y for opening menu again and n for exiting\t";
cin >> option;
while (option != 'y' || option != 'n')
{
cout << "Enter right value Please! only y or n: ";
char option1;
cin >> option1;
if (option1 == 'y' || option1 == 'n')
{
break;
}
}
if (option == 'y')
{
goto label;
}
else if (option == 'n')
{
cout << "The program is ending now! ";
}
Although I want to display the menu again for the user to do anything in the bank but the program terminates and exits I dont know why.I think there is a problem with object lifetime but I think that must not be the problem
Any help is appreciated.
This is the AddAccount Class
#pragma once
#include<iostream>
#include<string>
#include <vector>
using namespace std;
class Accounts
{
vector<int> Account_ID;
vector<string> AccountType;
vector<int> Customer_ID;
vector<int> Account_Balance;
public:
Accounts();
void WithDraw(int);
void Deposit(int,string,int,int);//Also can be named as add Account
void Balance();
void DeleteAccount(int);
int getAccountsNumber();
};
//Definning classes methods
Accounts::Accounts()
{
//no need to initialize vectors.They work perfect without initializing.C++ has done work for it
}
void Accounts::Deposit(int AID,string AT,int CID,int AB)
{
this->Account_ID.push_back(AID);
this->AccountType.push_back(AT);
this->Customer_ID.push_back(CID);
this->Account_Balance.push_back(AB);
}
void Accounts::WithDraw(int index)
{
cout << "\nThe Account ID of " << (index + 1) << " person is equal to: "
<< this->Account_ID[index] << endl;
cout << "\nThe Account Type of " << (index + 1) << " person is equal to: "
<< this->AccountType[index] << endl;
cout << "\nThe Customer ID of " << (index + 1) << " person is equal to: "
<< this->Customer_ID[index] << endl;
cout << "\nThe Account Balance of " << (index + 1) << " person is equal to: "
<< this->Account_Balance[index] << endl;
}
void Accounts::DeleteAccount(int index)
{
Account_ID.erase(Account_ID.begin()+index);
AccountType.erase(AccountType.begin()+index);
Customer_ID.erase(Customer_ID.begin()+index);
Account_Balance.erase(Account_Balance.begin()+index);
//Displaying that the account is successfully removed from the bank
cout << "\nThe Account ID of " << (index + 1) << " was successfully removed from the bank";
cout << "\nThe Account Type of " << (index + 1) <<" was successfully removed from the bank";
cout << "\nThe Customer ID of " << (index + 1) << " was successfully removed from the bank";
cout << "\nThe Account Balance of " << (index + 1) << " was successfully removed from the bank";
}
//It will display all the balance in the bank
void Accounts::Balance()
{
//The static int is not changed on the iteration where ever used in the loop or where ever
static int sum = 0;//To store the sum of all balance
for (unsigned int i = 0; i < Account_Balance.size(); i++)
{
sum = sum + Account_Balance[i];
}
cout << "The total balance in the bank is equal to : " << sum;
}
int Accounts::getAccountsNumber()
{
return Account_ID.size();
}
This is the Customer Class
#pragma once
#include<string>
using namespace std;
#include"Accounts.h"
#include"Manager.h"
class Customer
{
vector<string> Name;
vector<int> ID;
public:
Customer();
void AddCustomer(string,int);
void PrintAllCustomersData();
void DeleteCustomer(int);
void Print_CustomerDetails(int);
string getCustomerName(int);
};
void Customer::AddCustomer(string n, int id)
{
this->Name.push_back(n);
this->ID.push_back(id);
cout << "\nThe customer " << n << "with Id: " << id << " was successfully added in the Bank.";
}
void Customer::PrintAllCustomersData()
{
for (unsigned int i = 0; i < ID.size(); i++)
{
cout << "\nThe ID of " << (i + 1) << "Customer is : " << ID[i] << " and NAME is : " << Name[i];
}
}
void Customer::DeleteCustomer(int index)
{
Name.erase(Name.begin() + index);
ID.erase(ID.begin() + index);
cout << "\nThe customer with Name : " << Name[index] << " and ID: " << ID[index] << " was successfully deleted\n";
}
void Customer::Print_CustomerDetails(int index)
{
cout << "The Customer Name is : " << Name[index] << endl;
cout << "The Id of Customer is : " << ID[index] << endl;
}
string Customer::getCustomerName(int index)
{
return (Name[index]);
}
This is the Manager Class
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Manager
{
string Name;
string Branch;
int ID;
public:
void Print_ManagerDetails();
friend ostream& operator<<(ostream&, const Manager& M);
friend istream& operator>>(istream&, Manager& M);
};
void Manager::Print_ManagerDetails()
{
cout << "\nThe ID of Manager is : " << ID << endl;
cout << "\nThe Name of Manager is : " << Name << endl;
cout << "\nThe Branch of Manager is : " << Branch << endl;
}
istream& operator>>(istream& input, Manager& M) {
input >> M.ID >> M.Name>>M.Branch;
return input;
}
ostream& operator<<(ostream& output, const Manager& M) {
output << "\nThe ID of Manager is : " << M.ID<<endl;
output << "\nThe Name of Manager is : " << M.Name << "\nThe Branch of Manager is : " << M.Branch<<endl;
return output;
}
This is the Bank Class
#pragma once
#include"Customer.h"
#include"Accounts.h"
class Bank
{
Customer* customers_ptr;
Accounts* Accounts_ptr;
Manager manager;
public:
Bank();
void AddAccount(int,string,int,int);
void DeleteAccount(int);
void AddCustomer(string,int);
void DeleteCustomer(int);
int GetNoOfAccounts();
string GetCustomer_Name(int);
};
Bank::Bank()
{
cout << "\nThe program is in the bank class\n";
}
void Bank::AddAccount(int AID, string AT, int CID, int AB)
{
Accounts_ptr->Deposit(AID, AT, CID, AB);
}
void Bank::DeleteAccount(int index)
{
Accounts_ptr->DeleteAccount(index);
}
void Bank::AddCustomer(string name, int ID)
{
customers_ptr->AddCustomer(name, ID);
}
void Bank::DeleteCustomer(int index)
{
customers_ptr->DeleteCustomer(index);
}
int Bank::GetNoOfAccounts()
{
int num=Accounts_ptr->getAccountsNumber();
return num;
}
string Bank::GetCustomer_Name(int index)
{
string name = customers_ptr->getCustomerName(index);
return name;
}
This is the main program
#include<iostream>
#include<string>
#include<conio.h>
#include<algorithm>
#include"Bank.h"
#include"Customer.h"
#include"Manager.h"
#include"Accounts.h"
using namespace std;
void BankProgram();
void BankProgram()
{
Bank b1;
Accounts A1;
Manager m1;
char options;
cout << "\n\nEnter what you want to do \n1 for entering the managers data,\n2 for showing the managers data "
<< "\n3 for adding a customer in the bank\n4 for adding an account in the bank \n5 for deleting the customer\n"
<< "\n5 for deleting the account\n6 for getting customer name\n7 for getting No. of accounts"
<< "\n8 for seeing all the balance in the bank\nPress 'e' for exit";
cin >> options;
switch (options)
{
case '1':
{
//The manager class data
cout << "\nEnter the name,ID,Branch Of Manager: ";
cin >> m1;
break;
}
case '2':
{
cout << "\nThe data of Manager is :" << m1;
break;
}
case '3':
{
string Cname;
int CID;
cout << "\nEnter the name of customer: ";
cin >> Cname;
cout << "\nEnter the Customer ID: ";
cin >> CID;
b1.AddCustomer(Cname, CID);
break;
}
case '4':
{
int AID;
int CID;
int AB;
string AT;
cout << "\nEnter the name of Account ID: ";
cin >> AID;
cout << "\nEnter the name of Customer ID: ";
cin >> CID;
cout << "\nEnter the name of Account BALANCE: ";
cin >> AB;
cout << "\nEnter the name of Account Type: ";
cin >> AT;
b1.AddAccount(AID, AT, CID, AB);
break;
}
case '5':
{
int index;
cout << "\nEnter the customer which you want to delete: ";
cin >> index;
b1.DeleteCustomer(index);
break;
}
case '6':
{
int index;
cout << "\nEnter the account which you want to delete: ";
cin >> index;
b1.DeleteAccount(index);
break;
}
case '7':
{
int cn;
cout << "\nEnter the customer ID which you want to get name: ";
cin >> cn;
b1.GetCustomer_Name(cn);
break;
}
case '8':
{
b1.GetNoOfAccounts();
break;
}
case '9':
{
A1.Balance();
break;
}
case 'e':
{
cout << "The program is ending now: ";
break;
}
default:
{
cout << "\n\nEnter right value please: \n";
}
}
}
int main()
{
// BankProgram();
bool flag = true;
char option;
label:
BankProgram();
cout << "Enter y for opening menu again and n for exiting\t";
cin >> option;
while (option != 'y' || option != 'n')
{
cout << "Enter right value Please! only y or n: ";
char option1;
cin >> option1;
if (option1 == 'y' || option1 == 'n')
{
break;
}
}
if (option == 'y')
{
goto label;
}
else if (option == 'n')
{
cout << "The program is ending now! ";
}
}
Well it turns out to be very simple
Bank::Bank()
{
cout << "\nThe program is in the bank class\n";
}
void Bank::AddAccount(int AID, string AT, int CID, int AB)
{
Accounts_ptr->Deposit(AID, AT, CID, AB);
}
Accounts_ptr is an uninitialised pointer, using it results in a program crash. You must give Accounts_ptr a value in the Bank constructor.
You have the same issue with customers_ptr.
I have been working on this project for days now and I got most of it down however there are three problems that I am having. I am trying to calculate the average grade from a ifstream file, the program reads it successfully however when I pass the array from one function to another, it just displays 0 and F as I am trying to display the percentage and letter grade.
My second question is how do I get the array to only print out elements that are populated. So if I create an array of 10 and only populate the first three elements, how would I got to only get those first three elements to print only?
And my other question is when I try doing a search, it keeps saying record not found even I typed the name correctly. The record is being search by last name.
Any pointers or help would be greatly appreciated. Thanks!
My code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <conio.h>
using namespace std;
void greeting(string[], string[], double[], double[], double[], int);
int getinfo(string[], string[], double[], double[], double[], int);
int calculateaverageandgrade(string[], string[], double[], double[], double [], int);
void sortandsearch(string[], string[], double[], double[], double[], int);
void goodbye(string[], string[], double[], double[], double[]);
int main() {
string fname[10] = { "" };
string lname[10] = { "" };
double grade[10] = { 0 };
double grade2[10] = { 0 };
double grade3[10] = { 0 };
const int arraysize = 10;
greeting(fname, lname, grade, grade2, grade3, arraysize);
getinfo(fname, lname, grade, grade2, grade3, arraysize);
calculateaverageandgrade(fname, lname, grade, grade2, grade3, arraysize);
system("pause");
return 0;
}
void greeting(string fname[], string lname[], double grade[], double grade2 [], double grade3[], int arraysize) {
int choice = int();
char cont = 'y';
while (cont == 'y' || cont == 'Y')
{
cout << setw(60) << "Main Menu" << endl;
cout << setw(66) << "======================" << endl;
cout << "\t\t\t\tToday we will be calculating grades from a file" << endl << "\t\t\t\tthat you can provide for us using notepad." << endl << "\t\t\t\tBe sure to update your file every time you run the program" << endl;
cout << endl;
cout << setw(64) << "Reading file...." << endl;
cout << setw(66) << "======================" << endl;
cout << endl << endl << endl;
cout << setw(65) << "1. Calculate Grades." << endl;
cout << setw(63) << "2. Search Grades" << endl;
cout << setw(68) << "2. Exit from the Program." << endl;
cout << endl;
cout << setw(70) << "Please Enter your Selection: ";
cin >> choice;
if (cin.fail())
{
cout << "Invalid selection" << endl;
cin.clear();
cin.ignore(1000, '\n');
}
else
{
if (choice == 1)
{
calculateaverageandgrade(fname, lname, grade, grade2, grade3, arraysize);
}
else if (choice == 2) {
sortandsearch(fname, lname, grade, grade2, grade3, arraysize);
}
else if (choice == 3)
{
goodbye(fname, lname, grade, grade2, grade3);
}
else
{
cout << "Invalid Selection!" << endl;
}
}
cout << "\nDo you want to continue (Y/N)....";
cin >> cont;
}
}
int getinfo(string fname[], string lname[],double grade[], double grade2[], double grade3[], int arraysize) {
system("cls");
ifstream in;
in.open("data.txt");
int i = 0;
while (!in.eof()) {
in >> fname[i] >> lname[i] >> grade[i] >> grade2[i] >> grade3 [i];
++i;
}
arraysize = i;
return arraysize;
}
int calculateaverageandgrade(string fname[], string lname[], double grade[], double grade2[], double grade3[], int arraysize) {
system("cls");
const int i = 10;
double total[i];
for (int a = 0; a < arraysize; ++a) {
cout << "Name:" << setw(20) << "Score: " << setw(20) << "Grade: " << endl;;
cout << fname[a] << " " << lname[a];
total[a] = grade[a] + grade2[a] + grade3[a];
total[a] = total[a] / 3;
cout << setw(20) << total[a] << endl;
if (total[a] >= 90) {
cout << setw(40) << "A" << endl;
}
else if (total[a] >= 80) {
cout << setw(41) << "B" << endl;
}
else if(total[a] >= 70) {
cout << setw(41) << "C" << endl;
}
else if (total[a] >= 60) {
cout << setw(41) << "D" << endl;
}
else {
cout << setw(41) << "F" << endl;
}
}
system("pause");
return total[i];
}
void sortandsearch(string fname[], string lname[], double grade[], double grade2[], double grade3[], int arraysize) {
system("cls");
string nameSearch = string();
int i = int();
bool flag = false;
char key = char();
system("cls");
cout << "\nEnter Name to be Searched: ";
cin >> nameSearch;
while (i<arraysize)
{
if (lname[i] == nameSearch)
{
cout << "Record Found --- ";
cout << "Record # " << i + 1 << " contains " << nameSearch << "'s information." << endl;
flag = true;
}
++i;
}
if (flag == false)
{
cout << "\nRecord Not Found!" << endl;
}
cout << "\n\n\nPress any Key to Continue...";
key = _getch();
}
void goodbye(string fname[], string lname[], double grade[], double grade2[], double grade3[]) {
system("cls");
cout << setw(70) << "Thanks for using my program!" << endl;
system("pause");
exit(1);
}
When I run my Main Menu, I am able to add data that is needed for the map, but the problem happens when I go back to the menu and find that when I try to print the data nothing appears. In case you are wondering, there are no errors, it will just not print the data stored within the map when you run print contacts from the menu. Below is the code.
Main
#include <iostream>
using namespace std;
#include "AddContact.h"
//PASSWORD IS "Delta" use upper case D and the rest is lower case
int main() {
AddContact con;
con.Menu();
return 0;
}
AddContact.h
#include <iostream>
#include<cstdlib>
#include<map>
using namespace std;
#ifndef ADDCONTACT_H_
#define ADDCONTACT_H_
class AddContact {
public:
std::map<int, AddContact> people;
private:
string ContactName;
long long ContactPhone;
string Address;
string Email;
string Skype;
public:
AddContact();
AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype);
void ADD();
void print()const;
void view();
void Menu();
void Password();
};
#endif /* ADDCONTACT_H_ */
AddContact.cpp
#include "AddContact.h"
AddContact::AddContact(): ContactName(""), ContactPhone(0), Address(""), Email(""), Skype(""){
}
AddContact::AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype) {
this->ContactName = ContactName;
this->ContactPhone = ContactPhone;
this->Address = Address;
this->Email = Email;
this->Skype = Skype;
}
void AddContact::Password(){
cout << "Please Enter Your Password. " << endl;
string pass;
cin >> pass;
if (pass != "Delta") {
Password();
}
}
void AddContact::Menu(){
cout << "!!MyCircle Contact!!" << endl;
cout << endl;
Password();
int selection;
do {
cout << "***************************" << endl;
cout << "1. Add New Contact " << endl;
cout << "2. Display Contact " << endl;
cout << "3. Quit " << endl;
cout << "***************************" << endl;
cin>> selection;
switch(selection){
case 1:
ADD();
break;
view();
break;
}
} while (selection != 0);
}
void AddContact::ADD() {
for (int i = 0; i < 1; i++) {
string temp1;
cout << "Please Enter Contact Name: " << endl;
cin >> temp1;
long long temp2;
cout << "Please Enter Contact Phone Number: " << endl;
cin >> temp2;
int choice;
cout<< "Press 1 if you would like to add additional contact info or press 0 to return to main menu "<< endl;
cin >> choice;
if (choice == 1) {
string temp3 = "";
cout << "Please enter contact Address: " << endl;
cin>> temp3;
getline(cin, temp3);
string temp4= "";
cout << "Please enter contact Email: " << endl;
cin >> temp4;
string temp5 = "";
cout << "Please enter contact Skype: " << endl;
cin >> temp5;
people[i] = AddContact(temp1, temp2, temp3, temp4, temp5);
} else {
}
}
}
void AddContact::print()const{
cout<<ContactName<<" "<<ContactPhone<<" "<<Address<<" "<<Email<<" "<<Skype<<endl;
}
void AddContact::view(){
for(map<int, AddContact>::iterator it = people.begin(); it != people.end();it++){
it->second.print();
}
}
It looks like you're mising a default in your switch statement:
switch(selection){
case 1:
ADD();
break;
view();
break;
}
is mean to be
switch(selection){
case 1:
ADD();
break;
default:
view();
break;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am nearly done with this program, however I am getting an error that I am unsure how to deal with. I have looked for other solutions but none have seemed to help
The error I'm getting is:
error LNK2019: unresolved external symbol "void __cdecl deposit(class customer * const,int)" (?deposit##YAXQAVcustomer##H#Z) referenced in function _main
C:\Users\D\Desktop\Programs\Project 3\DProject3\Debug\DProject3.exe : fatal error LNK1120: 1 unresolved externals
It seems to have something to do with the deposit function, however I have tried fooling around with it to no avail, if anyone could help it would be much appreciated, I have 3 files for this project
Header
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
struct Date
{
int Day;
int Month;
int Year;
};
struct Name
{
std::string first_name;
std::string middle_name;
std::string last_name;
};
class customer
{
private:
Date birth_date;
Name name;
float balance_saving, balance_checking, initial_saving, initial_checking, amount, account_type;
public:
customer(); //Customer constructor
customer(Date birth_date, Name name, float initial_saving, float initial_checking); // Customer constructor with info
void withdraw(float amount, int account_type);
void deposit(float amount, int account_type);
float check_balance_saving(); // Print out the savings balance on screen
float check_balance_checking(); // Print out the checking balance on screen
void setDate(int Day, int Month, int Year);
Date getDate();
void setName(std::string first_name, std::string middle_name, std::string last_name);
Name getName();
void setSaving(float initial_saving);
void setChecking(float initial_checking);
};
#endif
Implementation File
#include "customer.h"
//Contructor without info / Default info
customer::customer()
{
birth_date.Day = 01;
birth_date.Month = 01;
birth_date.Year = 1901;
name.first_name = "N/A";
name.middle_name = "N/A";
name.last_name = "N/A";
balance_saving = 0;
balance_checking = 0;
}
// Constructor with user input data
customer::customer(Date DMY, Name FML, float initSav, float initCheck)
{
birth_date = DMY;
name = FML;
initial_saving = initSav;
initial_checking = initCheck;
}
void customer::withdraw(float amtW, int accW)
{
amount = amtW;
account_type = accW;
}
void customer::deposit(float amtD, int accD)
{
amount = amtD;
account_type = accD;
}
float customer::check_balance_saving()
{
return initial_checking;
}
float customer::check_balance_checking()
{
return initial_checking;
}
void customer::setDate(int Day, int Month, int Year)
{
birth_date.Day = Day;
birth_date.Month = Month;
birth_date.Year = Year;
}
Date customer::getDate()
{
return birth_date;
}
void customer::setName(std::string First, std::string Middle, std::string Last)
{
name.first_name = First;
name.middle_name = Middle;
name.last_name = Last;
}
Name customer::getName()
{
return name;
}
void customer::setSaving(float setSav)
{
initial_saving = setSav;
}
void customer::setChecking(float setCheck)
{
initial_checking = setCheck;
}
Main
#include "customer.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//Function prototypes
void withdraw (customer[], int);
void deposit (customer[], int);
void checkBal (customer[], int);
void newAccountFile (customer[], int);
int main ()
{
customer custInfo[49]; //Creates the customer array for info to be stored in
int customerAmt = 0; //Keeps track of the amount of customers in the array
ifstream customerfile ("account.dat"); //Open existing customer file
while (customerAmt < 2) //Loop to read the data from the existing file
{
int D, M, Y; // Birth day, month, year
string FN, MN, LN; //First name, middle name, last name
float Save, Check; //Savings & Checking initial amount
customerfile >> D;
customerfile >> M;
customerfile >> Y;
customerfile >> FN;
customerfile >> MN;
customerfile >> LN;
customerfile >> Save;
customerfile >> Check;
custInfo[customerAmt].setDate(D, M, Y);
custInfo[customerAmt].setName(FN, MN, LN);
custInfo[customerAmt].setSaving(Save);
custInfo[customerAmt].setChecking(Check);
customerAmt++;
}
customerfile.close(); //Close customer file and end file reading
//Variables for the User menu
int accountNumber, option, D1, M1, Y1;
string FN1, MN1, LN1;
float Save1, Check1;
//Loop used for User menu
do
{
cout << "1. withdraw" << endl;
cout << endl;
cout << "2. Deposit" << endl;
cout << endl;
cout << "3. Check Balances" << endl;
cout << endl;
cout << "4. Create a New Account" << endl;
cout << endl;
cout << "0. Exit" << endl;
cin >> option;
if (option == 1) // Uses withdraw function
{
cout << "Enter the account number: ";
cin >> accountNumber;
withdraw(custInfo, accountNumber);
}
else if (option == 2) // Uses deposit function
{
cout << "Enter the account number: " << endl;
cin >> accountNumber;
deposit(custInfo, accountNumber);
}
else if (option == 3) // Uses check balance function
{
cout << "Enter the account number: " << endl;
cin >> accountNumber;
checkBal(custInfo, accountNumber);
}
else if (option == 4) //Option to handle creating a new account
{
cout << "Enter the new account information, starting with birth info: " << endl;
cin >> D1 >> M1 >> Y1;
custInfo[customerAmt].setDate(D1, M1, Y1);
cout << endl;
cout << "Enter the new customer's full name: " << endl;
cin >> FN1 >> MN1 >> LN1;
custInfo[customerAmt].setName(FN1, MN1, LN1);
cout << endl;
cout << "Enter the inital savings balance: " << endl;
cin >> Save1;
custInfo[customerAmt].setSaving(Save1);
cout << endl;
cout << "Enter the initial checking balance: " << endl;
cin >> Check1;
custInfo[customerAmt].setChecking(Check1);
customerAmt++;
}
}while (option != 0);
newAccountFile(custInfo, customerAmt); // Creates new updated account info file
system ("pause");
return 0;
}
void withdraw(customer custInfo[], int number)
{
float withdrawAmount;
int optionWit;
float tempSave;
float tempCheck;
cout << "1. Withdraw from savings" << endl;
cout << endl;
cout << "2. Withdraw from checking" << endl;
cin >> optionWit;
cout << "Enter the amount to be withdrawn: ";
cin >> withdrawAmount;
if (optionWit == 1)
{
tempSave = custInfo[number].check_balance_saving() - withdrawAmount;
custInfo[number].setSaving(tempSave);
}
else if (optionWit == 2)
{
tempCheck = custInfo[number].check_balance_checking() - withdrawAmount;
custInfo[number].setChecking(tempCheck);
}
}
void desposit(customer custInfo[], int number)
{
float depositAmount;
int optionDep;
float tempSave;
float tempCheck;
cout << "1. Deposit to savings" << endl;
cout << endl;
cout << "2. Deposit to checking" << endl;
cin >> optionDep;
cout << "Enter the amount to be deposited: ";
cin >> depositAmount;
if (optionDep == 1)
{
tempSave = custInfo[number].check_balance_saving() + depositAmount;
custInfo[number].setSaving(tempSave);
}
else if (optionDep == 2)
{
tempCheck = custInfo[number].check_balance_checking() + depositAmount;
custInfo[number].setChecking(tempCheck);
}
}
void checkBal(customer custInfo[], int number)
{
int optionBal;
cout << "1. Check savings balance" << endl;
cout << endl;
cout << "2. Check checking balance" << endl;
cin >> optionBal;
if (optionBal == 1)
{
cout << "Current savings balance is: " << custInfo[number].check_balance_saving() << endl;
}
else if (optionBal == 2)
{
cout << "Current checking balance is: " << custInfo[number].check_balance_checking() << endl;
}
}
void newAccountFile(customer custInfo[], int size)
{
int i;
Date tempDate;
Name tempName;
ofstream newAccountStorage;
newAccountStorage.open ("updated_account.dat"); //Opens new file for updated account info
for (i = 0; i < size; i++);
{
tempDate = custInfo[i].getDate();
tempName = custInfo[i].getName();
newAccountStorage << tempDate.Day << endl;
newAccountStorage << tempDate.Month << endl;
newAccountStorage << tempDate.Year << endl;
newAccountStorage << tempName.first_name << endl;
newAccountStorage << tempName.middle_name << endl;
newAccountStorage << tempName.last_name << endl;
newAccountStorage << custInfo[i].check_balance_saving() << endl;
newAccountStorage << custInfo[i].check_balance_checking() << endl;
}
newAccountStorage.close(); //Closes the file after writing new information
}
You have misprint in your function definition
void desposit(customer custInfo[], int number)
^^^^