C++ can't update one of my variables - c++

I had this school assignment to build an atm machine. I got it to work but if I deposit or withdrawal money and view my account balance later its still not updated. Can someone tell me what I am doing wrong and any suggestion on how to make my code better, in general, would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
void WelcomeMenu();
void Menu();
void userChoiceDeposite(double accountbalance);
void userChoiceWithdraw(double accountbalance);
void userChoiceView(double accountbalance);
void printReceipt();
int main ()
{
//clear receipt.txt from previous transactions
ofstream clearFile("Receipt.txt");
clearFile.close();
//Declaring variables
string userInput;
char userAction;
//prompt user for input, start the program or quit
cout <<"Enter start to continue, or Q/q to quit ";
getline (cin, userInput);
cout <<endl;
//check the input
if ((userInput == "q") || (userInput == "Q"))
{
cout <<"Have a nice day!" <<endl;
return 0;
}
else if (userInput == "start")
{
const int sizeLimit = 50;
string firstName[sizeLimit], lastName[sizeLimit], passWord[sizeLimit];
double accountBalance[sizeLimit];
int count = 0;
ifstream readFile("Accounts.txt");
while (!readFile.eof())
{
readFile >> firstName[count];
readFile >> lastName[count];
readFile >> passWord[count];
readFile >> accountBalance[count];
count++;
}
//ask for username/pw
string fName, lName, pwd;
double accountbalance;
bool exists = false;
do
{
cout <<"Enter First Name: ";
cin >> fName;
cout <<"Enter Last Name: ";
cin >>lName;
cout <<"Enter Password: ";
cin >> pwd;
for (int i=0; i<sizeLimit; i++)
{
if (firstName[i] == fName)
{
if (lastName[i] == lName)
{
if (passWord[i] == pwd)
{
exists = true;
accountbalance = accountBalance[i];
}
}
}
}
}
while (exists == false);
//if correct display menu
WelcomeMenu();
cin >> userAction;
//while login is valid (true)
while (userAction != 'q')
{
if (userAction == 'D' || userAction == 'd')
{
userChoiceDeposite(accountbalance);
}
else if (userAction == 'W' || userAction == 'w')
{
userChoiceWithdraw(accountbalance);
}
else if (userAction == 'V' || userAction == 'v')
{
userChoiceView(accountbalance);
}
else if (userAction == 'P' || userAction == 'p')
{
printReceipt();
return 0;
}
else
{
cout <<"Have a nice Day" <<endl;
return 0;
}
Menu();
cin >> userAction;
}
}
else
{
cout <<"Input Error..." <<endl;
}
return 0;
}
//welcome menu display function
void WelcomeMenu()
{
cout <<"Welcome to your account " <<endl;
cout <<endl;
cout <<"[D/d] Deposit Money" <<endl;
cout <<"[W/w] Withdraw Money" <<endl;
cout <<"[V/v] View Account Balance" <<endl;
cout <<"[Q/q] Quit" <<endl;
}
//function after action was taken (add print option)
void Menu()
{
cout <<endl;
cout <<"[D/d] Deposit Money" <<endl;
cout <<"[W/w] Withdraw Money" <<endl;
cout <<"[V/v] View Account Balance" <<endl;
cout <<"[P/p] Print Receipt and quit" <<endl;
cout <<"[Q/q] Quit" <<endl;
}
//deposit function
void userChoiceDeposite(double accountbalance)
{
int depositMoney;
cout <<"Enter the ammount of money you want to deposit. Max $10000" <<endl;
cin >> depositMoney;
if ((depositMoney > 0) && (depositMoney <= 10000))
{
accountbalance = accountbalance + depositMoney;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You made a deposit of $" <<depositMoney <<endl;
saveAction.close();
}
else
{
cout <<"Incorrect ammount" <<endl;
return userChoiceDeposite(1);
}
}
//withdraw function
void userChoiceWithdraw(double accountbalance)
{
int withdrawMoney;
cout <<"Enter the ammount of $ you want to withdraw:";
cin >> withdrawMoney;
if (accountbalance < withdrawMoney)
{
cout <<"You dont have that much money"<<endl;
return userChoiceWithdraw(1);
}
else
{
accountbalance = accountbalance - withdrawMoney;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You withdrew $" <<withdrawMoney <<endl;
saveAction.close();
}
}
//view function
void userChoiceView(double accountbalance)
{
cout <<"Your account balance is $" <<accountbalance <<endl;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You viewed your account balance" <<endl;
saveAction.close();
}
//print receipt function
void printReceipt()
{
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "" <<endl;
saveAction << "" <<endl;
saveAction << "" <<endl;
saveAction << "Thank you! Come again!" <<endl;
saveAction.close();
}

You are only changing the value that you pass into the function (thus not changing the accountbalance outside of the function). For your methods where you need to update the accountbalance you either need to:
Pass by reference
Create a global variable
use a return value to update the account balance
Try one of these and see how it goes.

Related

Program won't read the inputted name to edit other info

When I input the info needed in addstudent(), it works. But when I try to edit the info, using the name I input to search for the info to edit, it doesn't work.
Here is the sample:
#include<iostream>
#include<cstring>
#include<cctype>
#include<vector>
using namespace std;
struct studmem{
char name[50];
int age;
char course[50];
int yrlvl;
char spcl[50];
}s[10];
void addstudent(studmem s[],int lngth, int &val){
for (int i = 0; i < lngth; i += 1) {
int CheckCharacter = 0;
int counter = 0;
int CheckCharacters = 0;
do{
int chkchr=1;
cout<<"Enter name: ";
cin.getline(s[val].name,50);
for(int i=0;i<strlen(s[val].name);i++){
if(!(isalpha(s[val].name[i])||isspace(s[val].name[i]))){
cout<<"Invalit input! Please try again!\n";
chkchr=0;
break;
}
}
if(chkchr==1){
break;
}
}while(1);
cout << "Enter your age: ";
cin >> s[val].age;
cin.ignore();
while (s[val].age >= 35 || s[val].age <= 17) {
cout << "Invalid age. Please enter your age again: ";
cin >> s[val].age;
}
while (CheckCharacter == 0) {
cout << "Enter Course: ";
cin.getline(s[val].course, 30);
if (strcmp(s[val].course, "BSIT") == 0)
{
CheckCharacter++;
}
else if (strcmp(s[val].course, "BSCS") == 0) {
CheckCharacter++;
}
else {
cout << "Invalid Input! Please try again!" << endl;
}
}
do{
CheckCharacter = 0;
cout << "Enter Year Level: ";
cin >> s[val].yrlvl;
if (s[val].yrlvl >= 1 && s[val].yrlvl <= 4){
CheckCharacter++;
}
else {
cout << "Invalid Input. Please Enter Year Level Again!\n";
}
}while (CheckCharacter == 0);
CheckCharacter = 0;
cin.ignore();
while (CheckCharacter == 0) {
cout << "Enter Specialization: ";
cin.getline(s[val].spcl, 50);
if (strcmp(s[val].course, "BSIT") == 0)
{
if (strcmp(s[val].spcl, "AGD") == 0)
CheckCharacter++;
else if (strcmp(s[val].spcl, "DA") == 0)
CheckCharacter++;
else if (strcmp(s[val].spcl, "WMA") == 0)
CheckCharacter++;
else if (strcmp(s[val].spcl, "SMBA") == 0)
CheckCharacter++;
else
{
cout << "Invalid Input. Please Enter Specialization Again";
}
}
else if (strcmp(s[val].course, "BSCS") == 0) {
if (strcmp(s[val].spcl, "SE") == 0)
CheckCharacter++;
else if (strcmp(s[val].spcl, "DS") == 0)
CheckCharacter++;
else
{
cout << "Invalid Input. Please Enter Specialization Again";
}
}
else {
cout << "Invalid Input. Please Enter Specialization Again(For BSIT: WMA, DA, AGD, SMBA For BSCS: DS, SE)";
}
}
val += 1;
}
}
void edstudent(studmem s[]){
int CheckCharacter=0;
do{
char studname[50];
cout<<"Search Name of Student: ";
cin.ignore();
cin.getline(studname,50);
for(int i=0;i<strlen(s[i].name);i++){
if((strcmp(studname,s[i].name))==0){
CheckCharacter++;
}
else{
cout<<"Student not found, please try again!\n";
break;
}
}
}while(CheckCharacter=0);
}
int main(){
bool Code = 1;
int rec=0;
int list = 0;
int value = 0;
do {
int ch;
cout << "Student Record" << endl;
cout << "1) Add" << endl;
cout << "2) View" << endl;
cout << "3) Edit" << endl;
cout << "4) Exit" << endl;
cout << "Maximum of 10 Students" << endl;
cout << "No. of Students: " << rec << endl;
cout << "\nChoice: ";
cin >> ch;
cin.ignore();
switch (ch) {
case 1: {
rec += 1;
addstudent(s, 1, value);
break;
}
case 2:{
for(int i=0;i<rec;i++){
cout<<"Student #"<<i+1;
cout<<"\nName: "<<s[i].name<<endl;
cout<<"Age: "<<s[i].age<<endl;
cout<<"Course: "<<s[i].course<<endl;
cout<<"Year Level: "<<s[i].yrlvl<<endl;
cout<<"Specialization: "<<s[i].spcl<<endl;
cout<<endl;
}
break;
}
case 3:{
edstudent(s);
break;
}
default:
cout<<"Invalid Input! Please try again!\n";
main();
}
} while (1);
}
I tried changing (int i=0;i<strlen(s[i].name);i++) because they say it's the problem, but it just doesn't work.
The main problem is here, I think:
void edstudent(studmem s[]){
int CheckCharacter=0;
do{
char studname[50];
cout<<"Search Name of Student: ";
cin.ignore();
cin.getline(studname,50);
for(int i=0;i<strlen(s[i].name);i++){
if((strcmp(studname,s[i].name))==0){
CheckCharacter++;
}
else{
cout<<"Student not found, please try again!\n";
break;
}
}
}while(CheckCharacter=0);
}
Hoping you can help me, since I'm new to C++.

C++ Array Object Checking Values

I am supposed to write a program with two classes, Employee and Department. When the main() function runs, it asks the user to choose one of the six numbered options that are displayed and creates arrays for Employee and Department objects. I'm disregarding every other option except option 1, the Create Department Option, so the focus of this issue will be the Department class and the Department department[3] array.
There is a while loop in the main() function that continuously runs until the user decides to exit. If the user enters option 1, a Department array object is created, and then the user also enters the departmentID, departmentName, and departmentHeadName for that object. The while loop notifies the user if the array has three Employee objects. However, I am having difficulties because each departmentID needs to be unique. For example, I cannot enter 1 for the first array object's departmentID, and then enter 1 again for the second array object's departmentID. How do I check if the user's departmentID input already exists in a previous object?
#include <iostream>
#include <string>
using namespace std;
// Employee Class
class Employee
{
private:
string employeeID;
string employeeName;
string employeeDepartmentID;
double employeeSalary;
int employeeAge;
public:
void createEmployee()
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> employeeID;
cout << "Employee Name :";
cin >> employeeName;
cout << "Salary: $";
cin >> employeeSalary;
cout << "Age : ";
cin >> employeeAge;
cout << "Department ID : ";
cin >> employeeDepartmentID;
}
};
// Department Class
class Department
{
private:
string departmentID;
string departmentName;
string departmentHeadName;
public:
void createDepartment()
{
cout << "Please Enter Department Details: \n";
cout << "Department ID : ";
cin >> departmentID;
cout << "Department Name : ";
cin >> departmentName;
cout << "Head of Department : ";
cin >> departmentHeadName;
}
};
// Function prototype
void displayMenu();
// Client main function
int main()
{
Employee employee[5];
Department department[3];
int choice;
int departmentCount = 0;
int employeeCount = 0;
while (true)
{
displayMenu();
cin >> choice;
if (choice == 1 && departmentCount < 3)
{
department[departmentCount].createDepartment();
departmentCount = departmentCount + 1;
}
else if (choice == 1 && departmentCount >= 3)
{
cout << "\nThe array is full, you can not add any more Departments." << endl;
}
else if (choice == 2 && employeeCount < 5)
{
employee[employeeCount].createEmployee();
employeeCount = employeeCount + 1;
}
else if (choice == 2 && employeeCount >= 5)
{
cout << "The array is full, you can not add any more Employees." << endl;
}
else if (choice == 6)
{
cout << "Thank you, goodbye." << endl;
break;
}
}
return 0;
}
// Display menu function
void displayMenu()
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
cout << "Please make a selection : ";
}
try this one
#include <iostream>
#include <string>
using namespace std;
// Employee Class
class Employee
{
private:
string employeeID;
string employeeName;
string employeeDepartmentID;
double employeeSalary;
int employeeAge;
public:
void createEmployee()
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> employeeID;
cout << "Employee Name :";
cin >> employeeName;
cout << "Salary: $";
cin >> employeeSalary;
cout << "Age : ";
cin >> employeeAge;
cout << "Department ID : ";
cin >> employeeDepartmentID;
}
};
// Department Class
class Department
{
private:
string departmentID;
string departmentName;
string departmentHeadName;
public:
void createDepartment()
{
cout << "Please Enter Department Details: \n";
cout << "Department ID : ";
cin >> departmentID;
cout << "Department Name : ";
cin >> departmentName;
cout << "Head of Department : ";
cin >> departmentHeadName;
}
public:
string getDepartmentID(){
return departmentID;
}
};
// Function prototype
void displayMenu();
// Client main function
int main()
{
Employee employee[5];
Department department[3];
int choice;
int departmentCount = 0;
int employeeCount = 0;
while (true)
{
displayMenu();
cin >> choice;
if (choice == 1 && departmentCount < 3)
{
department[departmentCount].createDepartment();
for(int i=0;i<departmentCount;i++)
{
if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
{
cout<<"already exists......................... \n";
}
}
departmentCount = departmentCount + 1;
}
else if (choice == 1 && departmentCount >= 3)
{
cout << "\nThe array is full, you can not add any more Departments." << endl;
}
else if (choice == 2 && employeeCount < 5)
{
employee[employeeCount].createEmployee();
employeeCount = employeeCount + 1;
}
else if (choice == 2 && employeeCount >= 5)
{
cout << "The array is full, you can not add any more Employees." << endl;
}
else if (choice == 6)
{
cout << "Thank you, goodbye." << endl;
break;
}
}
return 0;
}
// Display menu function
void displayMenu()
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
cout << "Please make a selection : ";
}
I used getDepartmentID function in Department Class to get department id from each department object
public:
string getDepartmentID(){
return departmentID;
}
there should be return type is string.because you have created departmentID as a string.
and I used For Loop in main function to compare relevant department id already exists or not
for(int i=0;i<departmentCount;i++)
{
if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
{
cout<<"already exists......................... \n";
}
}

C++ map, switch, and main menu trouble

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;
}

Program stuck in an infinite loop

I'm trying to get this program to run properly. it should do as the pseudo code do as described although when I execute the program and try to open a new account if I put more than one character in the customer name field the program just goes into an infinite loop and I have clue how to fix this issue.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
using namespace std;
int choice, account_number;
long acc_entry;
long acc_no = 112280;
double balance, deposit, withdrawal;
int interest = 1.67;
string customer_name;
void display_menu();
void get_choice();
void menu_selection(int selection);
void open_account();
void make_withdrawal();
void make_deposit();
void add_interest();
void display_transaction();
void main()
{
get_choice();
}
void display_menu()
{
system("CLS");
cout << "\n\n\t\t\t\tACCOUNT MENU";
cout << "\n\t\t\t\t============\n";
cout << "\n\t\t\t\t1. Open Account";
cout << "\n\t\t\t\t2. Make Withdrawal";
cout << "\n\t\t\t\t3. Make Deposit";
cout << "\n\t\t\t\t4. Add Interest";
cout << "\n\n\t\t\t\t5. Exit";
}
void open_account()
{
system("CLS");
cout << "\n\n\t\t\t\tOPEN ACCOUNT";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter your name\n\n\t";
cin >> customer_name;
cout << "\n\n\tPlease enter initial despoit\n\n\t";
cin >> deposit;
balance = balance + deposit;
account_number = acc_no + 1;
cout << "\n\n\tYour new account number\n\n\t" << setfill('0') << setw(8) << account_number;
get_choice();
}
void make_withdrawal()
{
system("CLS");
cout << "\n\n\t\t\t\tMAKE WITHDRAWAL";
cout << "\n\t\t\t\t===============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tPlease enter amount to withdraw\n\n\t";
cin >> withdrawal;
if (withdrawal > balance)
{
cout << "\n\n\tYou are exceeding your limit";
cin.ignore();
cin.get();
}
else
{
balance = balance - withdrawal;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void make_deposit()
{
system("CLS");
cout << "\n\n\t\t\t\tMAKE DEPOSIT";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tPlease enter amount to deposit\n\n\t";
cin >> deposit;
balance = balance + deposit;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void add_interest()
{
string yn;
system("CLS");
cout << "\n\n\t\t\t\tADD INTEREST";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tDo you wish to add interest [Y/N]\n\n\t";
getline(cin, yn);
if (yn == "Y" || yn == "y")
{
balance = balance * interest;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void display_transaction()
{
system("CLS");
cout << "\n\n\t\t\t\tCLOSED";
cout << "\n\t\t\t\t======\n\n";
if (account_number != 112280)
{
cout << "\tCustomer Name : - " << customer_name;
cout << "\n\n\tAccount Number : - " << setfill('0') << setw(8) << account_number;
cout << "\n\n\tBalance : - " << fixed << setprecision(2) << (char)156 << balance << "\n\n";
}
cin.get();
}
void get_choice()
{
display_menu();
do
{
cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
cin >> choice;
menu_selection(choice);
} while
(choice << 1 || choice >> 5);
cin.ignore();
}
void menu_selection(int a)
{
switch (a)
{
case 1:
{
open_account();
break;
}
case 2:
{
make_deposit();
break;
}
case 3:
{
make_withdrawal();
break;
}
case 4:
{
add_interest();
break;
}
case 5:
{
display_transaction();
break;
}
default:
{
cout << "hello";
}
}
}
void get_choice()
{
display_menu();
do
{
cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
cin >> choice;
menu_selection(choice);
} while
(choice < 1 || choice > 5); // << and >> aren't for comparison
cin.ignore();
}
In your get_choice function, you have a do-while loop with the following condition:
while (choice << 1 || choice >> 5); // this is going to run for a bit
<< and >> are not comparison operators; rather, they are bit shift operators.
Because your choice can be shifted either left or right, it goes into an infinite loop. It's a simple fix, really - change them to comparison operators!
As for the customer name, if there are spaces or newlines, std::cin will stop reading at the first one. Be sure to use std::getline for reading strings from stdin.
In open_account, replace this:
cin >> customer_name;
with this:
std::getline(cin, customer_name);
Also, fix this line:
int interest = 1.67; // This should be a double
You shouldn't be using globals really, but that's a whole different story.

Customer Account banking system error with variable input

Im building a banking system. in my create_account it asks for account address and phone number as well as other questions. When I go to my Show account info (balance inquiry) I notice its not getting the right address as well as phone number. its showing "garbage". Im not sure whats going on If you can point me in the right direction and help out that would be appreciated.
/********************************************************************
* Vincent Dotts 09/29/2014 ch11.cpp *
* This program serves as a customer banking system *
*****************************HISTORY*********************************
* WHO DATE Discription *
*********************************************************************
* VD 09/30/2013 Created program *
********************************************************************/
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
using namespace std;
class account
{
int accno;
double phone;
char name[30], address[40];
int deposit;
char type;
public:
void create_account(); //function to get data from user
void show_account() const; //function to show data on screen
void modify(); //function to add new data
void dep(int); //function to accept amount and add to balance amount
void draw(int); //function to accept amount and subtract from balance amount
void report() const; //function to show data in tabular format
int retacno() const; //function to return account number
int retdeposit() const; //function to return balance amount
char rettype() const; //function to return type of account
//int validate(int*); //prototype for function for validation
};
void account::create_account()
{
cout << "\nEnter The account # (5 digits): ";
cin >> accno;
while (accno < 10000 || accno > 99999)
{
cout << "Invalid Account # Please reenter";
cin >> accno;
}
cout << "\n\nEnter The Name of The account Holder : ";
cin.ignore();
cin.getline(name, 30);
cout << "\n\nEnter Address of The account Holder: "<<endl;
cout << "Example: 18 First St, Oakland, CA 97836" <<endl;
cin.ignore();
cin.getline(address, 40);
cout << "\nEnter Phone Number: ";
cin >> phone;
while (phone<1000000000 || phone>9999999999)
{
cout << "Invalid Phone # Please reenter";
cin >> phone;
}
cout << "\nEnter Type of The account (C/S) : ";
cin >> type;
type = toupper(type);
cout << "\nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : ";
cin >> deposit;
cout << "\n\n\nAccount Created..";
}
void account::show_account() const
{
cout << "\nAccount No. : " << accno;
cout << "\nAccount Holder Name: " << name;
cout << "\nAccount Holder Address:" << address;
cout << "\nAccount Holder Phone #:" << phone;
cout << "\nType of Account : " << type;
cout << "\nBalance amount : " << deposit;
}
void account::modify()
{
cout << "\nAccount No. : " << accno;
cout << "\nModify Account Holder Name: ";
cin.ignore();
cin.getline(name, 30);
cout << "\nModify Account Holder Address: " << endl;
cout << "Example: 18 First St, Oakland, CA 97836";
cin.ignore();
cin.getline(address, 60);
cout << "\nModify Account Holder Phone Number: ";
cin >> phone;
cout << "\nModify Type of Account: ";
cin >> type;
type = toupper(type);
cout << "\nModify Balance amount: ";
cin >> deposit;
}
void account::dep(int x)
{
deposit += x;
}
void account::draw(int x)
{
deposit -= x;
}
void account::report() const
{
cout << accno <<" " << name << " " << address<<" "<< phone << " " << type << " " << deposit << endl;
}
int account::retacno() const
{
return accno;
}
int account::retdeposit() const
{
return deposit;
}
char account::rettype() const
{
return type;
}
// function declaration
void write_account(); //function to write record in binary file
void display_sp(int); //function to display account details given by user
void modify_account(int); //function to modify record of file
void delete_account(int); //function to delete record of file
void display_all(); //function to display all account details
void deposit_withdraw(int, int); // function to desposit/withdraw amount for given account
void intro(); //introductory screen function
// THE MAIN FUNCTION OF PROGRAM
int main()
{
char ch;
int num;
intro();
do
{
system("cls");
cout << "\n\n\n\tMAIN MENU";
cout << "\n\n\t01. NEW ACCOUNT";
cout << "\n\n\t02. DEPOSIT AMOUNT";
cout << "\n\n\t03. WITHDRAW AMOUNT";
cout << "\n\n\t04. BALANCE ENQUIRY";
cout << "\n\n\t05. ALL ACCOUNT HOLDER LIST";
cout << "\n\n\t06. CLOSE AN ACCOUNT";
cout << "\n\n\t07. MODIFY AN ACCOUNT";
cout << "\n\n\t08. EXIT";
cout << "\n\n\tSelect Your Option (1-8) ";
cin >> ch;
system("cls");
switch (ch)
{
case '1':
write_account();
break;
case '2':
cout << "\n\n\tEnter The account No. : "; cin >> num;
deposit_withdraw(num, 1);
break;
case '3':
cout << "\n\n\tEnter The account No. : "; cin >> num;
deposit_withdraw(num, 2);
break;
case '4':
cout << "\n\n\tEnter The account No. : "; cin >> num;
display_sp(num);
break;
case '5':
display_all();
break;
case '6':
cout << "\n\n\tEnter The account No. : "; cin >> num;
delete_account(num);
break;
case '7':
cout << "\n\n\tEnter The account No. : "; cin >> num;
modify_account(num);
break;
case '8':
cout << "\n\n\tThank you Come Again";
break;
default:cout << "\a";
}
cin.ignore();
cin.get();
} while (ch != '8');
return 0;
}
// function to write in file
void write_account()
{
account ac;
ofstream outFile;
outFile.open("account.dat", ios::binary | ios::app);
ac.create_account();
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
outFile.close();
}
// function to read specific record from file
void display_sp(int n)
{
account ac;
bool flag = false;
ifstream inFile;
inFile.open("account.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
return;
}
cout << "\nBALANCE DETAILS\n";
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
if (ac.retacno() == n)
{
ac.show_account();
flag = true;
}
}
inFile.close();
if (flag == false)
cout << "\n\nAccount number does not exist";
}
// function to modify record of file
void modify_account(int n)
{
bool found = false;
account ac;
fstream File;
File.open("account.dat", ios::binary | ios::in | ios::out);
if (!File)
{
cout << "File could not be open !! Press any Key...";
return;
}
while (!File.eof() && found == false)
{
File.read(reinterpret_cast<char *> (&ac), sizeof(account));
if (ac.retacno() == n)
{
ac.show_account();
cout << "\n\nEnter The New Details of account" << endl;
ac.modify();
int pos = (-1)*static_cast<int>(sizeof(account));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char *> (&ac), sizeof(account));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
// function to delete record of file
void delete_account(int n)
{
account ac;
ifstream inFile;
ofstream outFile;
inFile.open("account.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
return;
}
outFile.open("Temp.dat", ios::binary);
inFile.seekg(0, ios::beg);
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
if (ac.retacno() != n)
{
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
}
}
inFile.close();
outFile.close();
remove("account.dat");
rename("Temp.dat", "account.dat");
cout << "\n\n\tRecord Deleted ..";
}
// function to display all accounts deposit list
void display_all()
{
account ac;
ifstream inFile;
inFile.open("account.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
return;
}
cout << "\n\n\t\tACCOUNT HOLDER LIST\n\n";
cout << "*********************************************************************\n";
cout << "A/c# NAME Address Phone # Type Balance\n";
cout << "*********************************************************************\n";
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
ac.report();
}
inFile.close();
}
// function to deposit and withdraw amounts
void deposit_withdraw(int n, int option)
{
int amt;
bool found = false;
account ac;
fstream File;
File.open("account.dat", ios::binary | ios::in | ios::out);
if (!File)
{
cout << "File could not be open !! Press any Key...";
return;
}
while (!File.eof() && found == false)
{
File.read(reinterpret_cast<char *> (&ac), sizeof(account));
if (ac.retacno() == n)
{
ac.show_account();
if (option == 1)
{
cout << "\n\n\tTO DEPOSITE AMOUNT ";
cout << "\n\nEnter The amount to be deposited";
cin >> amt;
ac.dep(amt);
}
if (option == 2)
{
cout << "\n\n\tTO WITHDRAW AMOUNT ";
cout << "\n\nEnter The amount to be withdraw";
cin >> amt;
int bal = ac.retdeposit() - amt;
if ((bal<500 && ac.rettype() == 'S') || (bal<1000 && ac.rettype() == 'C'))
cout << "Insufficience balance";
else
ac.draw(amt);
}
int pos = (-1)*static_cast<int>(sizeof(ac));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char *> (&ac), sizeof(account));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
// INTRODUCTION FUNCTION
void intro()
{
cout << "\n\n\n\t CUSTOMER";
cout << "\n\n\t ACCOUNT";
cout << "\n\n\t SYSTEM";
cin.get();
}
http://imgur.com/kV8w5KB
a screen shot of my input