Display Data From Text Files - c++

I'm working on a system in which a set of data has been asked and acquired from user, stored on the text file, to be displayed, searched and eddied later. So far I could write all the functions, and data's are stored on text files, but however when I tend to read from files, I end up having an error saying "Unhandled exception at 0x0F797EA6 (msvcp120d.dll) in Assignment.exe: 0xC0000005: Access violation reading location 0x00000000". I'm guessing it has to do something with my pointers, but I can't really figure it out , here are the code snippets ;
function to acquire data:
void get_apartment_details(){
cout << "Apartment ID:" << apartment_id() << endl << endl;
cin >> auto_id;
while (true) {
cout << "Apartment Addres:" << endl;
cin >> apartment_address;
if (apartment_address.empty()) {
cout << "Please Do Enter a Name, Blank Data's Are Not Allowed!" << endl;
cin.clear();
}
else if (validate_string(apartment_address) == false) {
cout << "Please Enter Only Alphabetic Elements, Digits Are Not Allowd!" << endl;
cin.clear();
}
else {
cout << "Number Of Bedrooms: (Limited To 2,3, and 4 Only)" << endl;
if (isNumber(number_of_bedrooms) == false ) {
cout << "Please Do Enter Numbers Only" << endl;
cin.clear();
}
else if (number_of_bedrooms < 2 || number_of_bedrooms > 4) {
cout << "Numbers Are Only Limited To 2,3, And 4" << endl;
cin.clear();
}
else {
cout << "Number Of Bathrooms:" << endl;
if (isNumber(number_of_bathrooms) == false) {
cout << "Please Do Enter Numbers Only!" << endl;
cin.clear();
}
else if (number_of_bathrooms > 10) {
cout << "The Maximum Number Of Bathrooms Defined Is 10" << endl;
cin.clear();
}
else {
cout << "Maximum Capacity:" << endl;
if (isNumber(maximum_capacity) == false) {
cout << "Please Do Enter Numbers Only!" << endl;
cin.clear();
}
else if (maximum_capacity > 10) {
cout << "The Maximum Capacity Defined For The System Is 10!" << endl;
cin.clear();
}
else {
cout << "Size (sq feet): " << endl;
if (isNumber(size)==false) {
cout << "Please Do Enter Only Numbers!" << endl;
cin.clear();
}
else if (size > 929) {
cout << "Size Of The House Can't Be More 929 Sq Feet, Or 1000 Meters" << endl;
cin.clear();
}
else {
cout << "Daily Rental:" << endl;
if (isNumber(daily_rental) == false) {
cout << "Please Do Enter Only Numbers!" << endl;
cin.clear();
}
else if (daily_rental > 1000) {
cout << "The Maximum For Daily Rental Is 1000" << endl;
cin.clear();
}
else {
cout << "Weekly Rental:" << endl;
if (isNumber(weekly_rental) == false) {
cout << "Please Do Enter Only Numbers!" << endl;
cin.clear();
}
else if (weekly_rental > 1000) {
cout << "The Maximum For Weekly Rental Is 7000" << endl;
cin.clear();
}
else {
cout << "Monthly Rental:" << endl;
if (isNumber(monthly_rental) == false) {
cout << "Please Do Enter Only Numbers!" << endl;
cin.clear();
}
else if (monthly_rental > 1000) {
cout << "The Maximum For Weekly Rental Is 7000" << endl;
cin.clear();
}
else {
cout << "Please Do Choose The Available Facilities: ( Choose 4 )" << endl;
cout << "1.Local Restaurant Chargeback" << endl;
cout << "2.On-Site Reception" << endl;
cout << "3.Broadband Internet Access" << endl;
cout << "4.Housekeeping" << endl;
cout << "5.Business Administration Services" << endl;
cout << "6.Baby Sitting Booking Service" << endl << endl;
cout << "First Item:" << endl;
cin >> opt2;
if ( 0 > opt2 > 6) {
cout << "Please Dn Only Enter In Digits From 1 To 6" << endl;
cin.clear();
}
else {
cout << "Second Item:" << endl;
cin >> opt3;
if (0 > opt2 > 6) {
cout << "Please Dn Only Enter In Digits From 1 To 6" << endl;
cin.clear();
}
else {
cout << "Third Item:" << endl;
cin >> opt4;
if (0 > opt2 > 6) {
cout << "Please Dn Only Enter In Digits From 1 To 6" << endl;
cin.clear();
}
else {
cout << "Fourth Item:" << endl;
cin >> opt5;
if (0 > opt2 > 6) {
cout << "Please Dn Only Enter In Digits From 1 To 6" << endl << endl;
cin.clear();
}
break;
}
}
}
}
}
}
}
}
}
}
}
}
}
Function to store data's in to the text file :
void add_apartment(){
system("cls");
staff staffobj;
staffobj.get_apartment_details();
progress();
ofstream outFile;
outFile.open("apartment.txt", ios::binary | ios::app);
outFile.write(reinterpret_cast<char *> (&staffobj), sizeof(staff));
outFile.close();
cout << endl << "\n\n Details Of New Apartments Has Been Succesfully Added";
cin.ignore();
cin.get();
staff_menu();
}
Function to display data from text file :
void display_all()
{
staff st;
ifstream inFile;
inFile.open("apartment.txt", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
cin.ignore();
cin.get();
staff_menu();
}
cout << "\n\n\n\t\t\tRegistered Apartments Records !!!\n\n";
while (inFile.read(reinterpret_cast<char *> (&st), sizeof(staff)))
{
st.ShowData_Apartment();
cout << "\n\n====================================\n";
}
inFile.close();
cin.ignore();
cin.get();
system("pause");
staff_menu();
}
showdata :
void ShowData_Apartment(){
cout << "Apartment Address:" << apartment_address << endl;
cout << "Number Of Bedrooms:" << number_of_bedrooms << endl;
cout << "Number Of Bathrooms:" << number_of_bathrooms << endl;
cout << "Maximum Capacity:" << maximum_capacity << endl;
cout << "Size (sq feet):" << size << endl;
cout << "Daily Rental::" << daily_rental << endl;
cout << "Weekly Rental:" << weekly_rental << endl;
cout << "Monthly Rental:" << monthly_rental << endl;
cout << "Facilities Availabale:" << endl;
cout << opt2 << endl;
cout << opt3 << endl;
cout << opt4 << endl;
cout << opt5 << endl;
}

Related

different option throw an unexpected error

when I'm executing this program, it works fine just for one option, but if I try to choose an other option when executing, it will throw an error in this line:
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
here's my program:
void avail_art(void)
{
char c,con;
int quantity,disp=MAX;
bool b = true;
map<string, unordered_set<int>>m{
{"Mouse",{120,disp}},
{"Keyboard",{75,disp}},
{"Monitor",{750,disp}},
{"GPU",{1200,disp}},
{"CPU",{1000,disp}}
};
auto it = m.find("CPU");
cout << "M - for mouse." << endl;
cout << "K - for keyboard." << endl;
cout << "R - for monitor." << endl;
cout << "G - for GPU." << endl;
cout << "C - for CPU." << endl;
cout << "Q - for quit." << endl;
cout << "======================" << endl;
while (b)
{
cout << "Enter your choice: ";
cin >> c;
switch (c)
{
case 'M':
it = m.find("Mouse");
cout << "You've choosen the Mouse!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
l:
cout << "Enter the quantity: ";
cin >> quantity;
if (quantity > * (it->second.find(disp)))
{
cout << "Out of Stock! the Stock has only :" << *(it->second.find(disp)) <<" Piece."<< endl;
goto l;
}
cout << "Confirm? y/n: ";
cin >> con;
if (con == 'y')
{
auto its=it->second.find(disp);
it->second.insert(disp=(*its - quantity));
it->second.erase(its);
}
break;
case 'K':
it = m.find("Keyboard");
cout << "You've choosen the Keyboard!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
cout << "Enter the quantity: ";
cin >> quantity;
cout << "Confirm? y/n :";
cin >> con;
if (con == 'y')
{
auto its = it->second.find(disp);
it->second.insert(disp = (*its - quantity));
it->second.erase(its);
}
break;
}
}
}
can someone help me please

Calling a class array inside of a class array C++

so I have a school project where I'm trying to create a bank account. So I have an array of class customer containing the customer name etc, this customer can have multiple accounts, so I have created an array of class accounts inside of customer. My problem is, I can't call a getter function inside of the account array, I have tried using customerArray[curr_id]->accountArray[curr_acc_num]->get_balance() however I am getting an error telling my that it is unaccessable.
Here is the code, any input would be apprechiated as it is still a work in progress.
#include <iostream>.
#include <windows.h>
#include "account.h"
#include "customer.h"
using namespace std;
bool quit;
int amount;
double bal;
bool valid_pin;
customer* customerArray[200];
account* accountArray[5];
int curr_id;
int curr_acc_num;
//1st name, 2nd name, ID
customer cust1("Paul", "Smith", 2233);
//ID, Deposit, Pin
account Paul(112, 222, 6716);
//Welcome screen function
void welcome_screen() {
cout << endl << endl << endl;
cout << "\tAbertay Banking" << endl;
cout << "\t System " << endl << endl << endl;
Sleep(1800);
}
//Display account balance function
void account_balance() {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << " " << customerArray[curr_id]->get_name() << endl;
cout << " ID: " << customerArray[curr_id]->get_custid() << endl << endl;
cout << " Balance: " << char(156) << customerArray[curr_id]->accountArray[curr_acc_num]->get_balance() << endl;
//Display any outstanding loans
if (Paul.get_loan() > 0) {
cout << endl << endl;
cout << " Outstanding loan: -" << char(156) << Paul.get_loan();
Sleep(3000);
}
//User has no loan on this account
else {
Sleep(3000);
}
}
//Withdrawl function
void withdrawl() {
bal = Paul.get_balance();
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter the amount you wish to withdraw: ";
cin >> amount;
//customer has sufficent funds, withdrawl accepted
if (amount <= bal + Paul.get_overdraft()) {
bal -= amount;
Paul.set_balance(bal);
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl; cout << "You have withdrawn: " << char(156) << amount << endl;
cout << "You now have a balance off: " << char(156) << bal << endl;
cout << "Do you wish to carry out another transaction?"; //do you wish to carry out another transaction.
Sleep(2000);
}
//customer doesn't have enough funds to carry out withdrawl
else {
char choice;
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "You have insufficient funds in your account" << endl;
}
}
//Deposit function
void deposit() {
//balance from user account
bal = Paul.get_balance();
char check;
double ln;
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter the amount you wish to deposit: ";
cin >> amount;
//Option to pay off loan, if applicable
if (Paul.get_loan() > 0) {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Do you want to pay off your loan? (Y/N)" << endl;
cin >> check;
system("cls");
cout << "\tBank of Abertay" << endl << endl;
//User selected to pay off loan
if (check == 'y') {
//deposit paid into loan
if (amount < Paul.get_loan()) {
ln = Paul.get_loan() - amount;
Paul.set_loan(ln);
cout << "Your loan is now at: -" << char(156) << Paul.get_loan() << endl;
}
//loan is payed of, any extra money is deposited into balance
else {
ln = amount - Paul.get_loan() + Paul.get_balance();
Paul.set_loan(0);
Paul.set_balance(ln);
cout << "You have payed your loan off!" << endl;
}
}
}
//user has no loan or doesn't wish to pay off loan
else {
bal += amount;
Paul.set_balance(bal);
}
cout << "You have deposited: " << char(156) << amount << endl;
cout << "You now have a balance off: " << char(156) << bal << endl;
cout << "Would you like to select another option with this account?"; //do you wish to carry out another transaction.
Sleep(1500);
}
//Apply for overdraft
void overdraft() {
system("cls");
char choice;
cout << endl << endl;
cout << "\tTake out an overdraft of " << char(156) << "300? " << endl;
cout << "\t (Y/N)" << endl;
cin >> choice;
if (choice == 'y') {
Paul.set_overdraft(300);
}
}
//Calculating interest rates
double interest_rate(int loan) {
//Up to £3,000 rate of 11%
if (loan < 3000) {
return 1.11;
}
//From £3,000 to £7,000 rate of 9%
else if (loan > 3000 && loan < 7000) {
return 1.09;
}
//From £7,000 to £21,000 rate of 7%
else if (loan > 7000 && loan < 21000) {
return 1.07;
}
//From £21,000 over rate of 5%
else if (loan < 21) {
return 1.05;
}
}
//Loan eligibility funcition
void loan_elgibility() {
double max_loan;
double loan;
//Calculating loan eligibility
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Caculating loan eligibility";
max_loan = (2 * Paul.get_balance());
Sleep(800);
//Loan and rates
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << " You are eligible to borrow up to: " << char(156) << max_loan << endl << endl;
cout << " Interest rates are as follows" << endl << endl;
cout << " - " << char(156) << "3,000 at 11%" << endl;
cout << " " << char(156) << "3,000 - " << char(156) << "7,000 at 9%" << endl;
cout << " " << char(156) << "7,000 - " << char(156) << "21,000 at 7%" << endl;
cout << " " << char(156) << "21,000 - at 5%" << endl;
Sleep(6200);
//User input loan request
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Enter loan request: " << char(156);
cin >> loan;
//Processing loan request
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " We are processing your request..." << endl;
Sleep(800);
system("cls");
//Loan has been approved, loan deposited into account
if (loan <= Paul.get_balance()) {
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "Your loan has been approved" << endl;
bal = Paul.get_balance() + loan;
loan = interest_rate(loan) * loan;
Paul.set_balance(bal);
Paul.set_loan(loan);
Sleep(800);
}
//Loan has not been approved
else {
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "You do not qualify for a loan of this amount";
Sleep(800);
}
}
//Quit function
void quit_screen() {
system("cls");
char choice;
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Do you want to quit? (Y/N) " << endl;
cout << " ";
cin >> choice;
// customer wants to quit, end while loop and program
if (choice == 'y') {
system("cls"); //clear screen
cout << endl << endl << endl;
cout << " Thanks for using," << endl;
cout << " Bank of Abertay!"<< endl;
Sleep(2000);
quit = true;
}
else {
quit = false;
}
}
//Request user pin number
bool pin_request() {
double pin;
int counter = 0;
do {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "\tEnter pin: ";
cin >> pin;
if (pin == Paul.get_pin()) {
return true;
}
else if (counter == 3) {
cout << "You have entered an invalid pin too many times!";
Sleep(800);
return false;
}
else {
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "You have entered an invalid pin" << endl;
Sleep(800);
valid_pin = false;
counter++;
}
} while (valid_pin == false && counter < 3);
}
//Adding a new customer
int counter = 0;
void add_customer(string first_nom, string second_nom, int cust_ID) {
customer* new_customer = new customer(first_nom, second_nom, cust_ID);
customerArray[counter] = new_customer;
counter++;
}
//creating new bank account
void create_account()
{
int input_custid;
double input_balance;
int input_pin;
string first_nom;
string second_nom;
int cust_ID;
char choice;
//Request new customer information
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter first name: ";
cin >> first_nom;
cout << endl;
cout << "Enter surname: ";
cin >> second_nom;
cout << endl;
cout << "Customer ID: ";
cin >> cust_ID;
//create new customer class array
add_customer(first_nom, second_nom, cust_ID);
//creating new account array
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter The account No. :";
cin >> input_custid;
cout << "Initial deposit in new account: ";
cin >> input_balance;
cout << "Create pin: ";
cin >> input_pin;
system("cls");
cout << " We are processing your request..." << endl;
Sleep(800);
cust1.add_account(input_custid, input_balance, input_pin);
system("cls");
cout << "Account Created.." << endl;
cout << "Thank you for joining the Bank of Abertay";
Sleep(800);
}
//find user account
bool find_account() {
int custID;
int counter;
bool found_account;
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Enter your customer ID: ";
cin >> custID;
cout << " - ";
cin >> curr_acc_num;
//not happy about this!!
for (int i = 0; i < 200; i++) {
if (customerArray[i]->get_custid() == custID) {
curr_id = i;
return true;
}
else if (i == 199) {
return false;
}
}
}
//an option to send money to another account?
// need a function that takes user back to option menu if wrong pin enter!
int main() {
//welcome screen
welcome_screen();
//while loop until player decides to quit
do {
int option_choice;
system("cls"); //clear screen, updating board every time function called
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "\t Menu" << endl << endl;
cout << "\t1. View Balance" << endl;
cout << "\t2. Withdraw" << endl;
cout << "\t3. Deposit" << endl;
cout << "\t4. Loan eligibility" << endl;
cout << "\t5. New account" << endl;
cout << "\t6. Overdraft" << endl;
cout << "\t7. Quit" << endl << endl;
cout << "\t\tSelect: ";
cin >> option_choice;
//ensure option screen only runs through once unless an invalid option is chosen
int valid_number_check = 0;
// do {
switch (option_choice) {
//Display account balance
case 1:
if (find_account() == true && pin_request() == true) {
account_balance();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Account withdrawl
case 2:
if ( find_account() == true && pin_request() == true) {
withdrawl();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Account deposit
case 3:
if (find_account() == true && pin_request() == true) {
deposit();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Loan eligibility
case 4:
if (find_account() == true && pin_request() == true) {
loan_elgibility();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Create new account
case 5:
create_account();
break;
//Overdraft
case 6:
if (find_account() == true && pin_request() == true) {
overdraft();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Quit
case 7:
quit_screen();
break;
//Invalid number entered
default:
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "Please enter a valid number";
Sleep(800);
break;
}
} while (quit == false);
}
// main
Account .h
#pragma once
#include <string>
#include "account.h"
using namespace std;
class customer
{
private:
int custid;
string name;
account* accountArray[5];
int counter = 0;
public:
int get_custid();
customer(string, string, int);
string get_name();
void add_account(int, double, int);
~customer();
};
Account.cpp
#include "customer.h"
#include "account.h"
#include <string>
using namespace std;
customer::customer(string first_nom, string second_nom, int cust_ID) {
counter++;
string first_name = first_nom;
string sur_name = second_nom;
//counting the number of accounts
counter++;
//identification of customer array
//custid = counter + 1000;
custid = cust_ID;
}
customer::~customer()
{
}
int customer::get_custid() {
return custid;
}
void customer::add_account(int id, double dep, int pin) {
account* new_account = new account(id, dep, pin);
accountArray[counter] = new_account;
counter++;
}
string customer::get_name() {
return name;
}
customer.h
#pragma once
class account
{
private:
double balance;
int custid;
double loan_amount;
double overdraft;
int pin;
int account_number;
public:
account(int, double, int);
~account();
//getters
double get_balance();
double get_loan();
double get_pin();
double get_overdraft();
//setters
void set_overdraft(double over);
void set_balance(double bal);
void set_loan(double loan);
};
customer.cpp
#include "account.h"
#include <iostream>
#include <windows.h>
using namespace std;
account::account(int input_custid, double input_balance, int input_pin)
{
custid = input_custid;
balance = input_balance;
pin = input_pin;
loan_amount = 0;
overdraft = 0;
}
account::~account()
{
}
double account::get_balance()
{
return balance;
}
double account::get_loan()
{
return loan_amount;
}
double account::get_pin()
{
return pin;
}
double account::get_overdraft()
{
return overdraft;
}
void account::set_balance(double bal)
{
balance = bal;
}
void account::set_loan(double loan)
{
loan_amount = loan;
}
void account::set_overdraft(double over)
{
overdraft = over;
}

Why will my program not re-enter my 2nd while loop? C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
All of my commands and alters to the array work individually. However, when i try to return to the menu the screen just starts jittering and will not continue. I am forced to close the program. How do i fix this.
/* Programmer: Joshua Zuber
Program Desc: This Program will allow the user to select an icon to display around an array message!
Program Name: Array Demonstration
Clean Compile Date:
*/
#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
// Arrays
string Masterfile[] = "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design small C++ programs using a gamming approach. ";
string Studentfile[]= "";
// Variables
int Icon;
char again = 'y';
int i;
int j;
int menu = 'y';
int menu2 = 'n';
int menu3 = 'n';
int menu4 = 'n';
int menu5 = 'n';
int objEdit;
while (toupper(again) == 'Y') // Start Main Loop
{
cout << "Welcome to Array Demonstration!" << "\n\n";
cout << Masterfile[0] << "\n\n";
while(toupper(menu) == 'Y' || toupper(menu2) == 'Y' || toupper(menu3) == 'Y' || toupper(menu4) == 'Y' || toupper(menu5) == 'Y' ) // Start Array Menu Loop
{
cout << "Please select one of the below options to edit this string!" << "\n\n";
Studentfile[0] = Masterfile[0];
cout << "1. Size" << "\n\n";
cout << "2. Replace {all of the C++ students} with {My instructer, Professor Penn}" << "\n\n";
cout << "3. Swap the word {small} with {efficient}" << "\n\n";
cout << "4. Erase the phrase {using a gamming approach}" << "\n\n";
cout << "5. View Final Product" << "\n\n";
cin >> objEdit;
if(objEdit == 1) // Menu Array Size Check
{
cout << "The size of the original string is: " << Masterfile[0].size() << "\n\n";
cout << "\n\n" << "Would you like to return to menu? Y/N " << "\n\n";
cin >> menu2 ;
system("cls");
}
else if(objEdit == 2) // Menu 2nd Option
{
cout << "Changing the phrases!" << "\n\n";
Studentfile[0].replace(16,20,"My instructer, Professor Penn");
cout << Studentfile[0] << "\n\n";
cout << "\n\n" << "Would you like to return to menu? Y/N " << "\n\n";
cin >> menu3;
system("cls");
}
else if(objEdit == 3) // Menu 3rd Option
{
cout << "Changing the phrases!" << "\n\n";
Studentfile[0].replace(131,5,"efficient" );
cout << Studentfile[0] << "\n\n";
cout << "\n\n" << "Would you like to return to menu? Y/N " << "\n\n";
cin >> menu4;
system("cls");
}
else if(objEdit == 4) // Menu 3rd Option
{
cout << "Changing the phrases!" << "\n\n";
Studentfile[0].erase(150);
cout << Studentfile[0] << "\n\n";
cout << "\n\n" << "Would you like to return to menu? Y/N " << "\n\n";
cin >> menu5;
system("cls");
}
else if (objEdit <=0 || objEdit >=6) // Menu Failsafe
{
cout << "Please Select a Valid Number" << "\n\n";
cout << "1. Size" << "\n\n";
cout << "2. Replace {all of the C++ students} with {My instructer, Professor Penn}" << "\n\n";
cout << "3. Swap the word {small} with {efficient}" << "\n\n";
cout << "4. Erase the phrase {using a gamming approach}" << "\n\n";
cout << "5. View Final Product" << "\n\n";
cin >> objEdit;
}
else if(objEdit == 5 || toupper(menu) == 'N') // Menu 5th Option
{
cout << "Please Select one of the following numbers to choose a symbol!" << "\n\n";
cout << "1. *" << "\n\n";
cout << "2. ^" << "\n\n";
cout << "3. #" << "\n\n";
cout << "4. +" << "\n\n";
cin >> Icon;
system("cls");
if(Icon <= 0, Icon >= 5) // User Failsafe
{
cout << "You're Entry Is Not Valid" << "\n\n";
cout << "Please Select one of the following numbers to choose a symbol!" << "\n\n";
cout << "1. *" << "\n\n";
cout << "2. ^" << "\n\n";
cout << "3. #" << "\n\n";
cout << "4. +" << "\n\n";
cin >> Icon;
system("cls");
}
else // Icon Breakdown
{
if(Icon == 1) // Icon Choice 1
{
for (i=1;i<=1;i++)
{
for (j=1;j<=i;j++);
{
cout << "*******************************************************************************" << "\n";
cout << Studentfile[0] << "\n";
cout << "*******************************************************************************" << "\n";
cout << endl;
cout << "\n\n" << "Do you wish to play again? Y/N " << "\n\n";
cin >> again;
system("cls");
}
}
}
else if(Icon == 2) // Icon Choice 2
{
for (i=1;i<=1;i++)
{
for (j=1;j<=i;j++);
{
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << "\n";
cout << Studentfile[0] << "\n";
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << "\n";
cout << endl;
cout << "\n\n" << "Do you wish to play again? Y/N " << "\n\n";
cin >> again;
system("cls");
}
}
}
else if(Icon == 3) // Icon Choice 3
{
for (i=1;i<=1;i++)
{
for (j=1;j<=i;j++);
{
cout << "###############################################################################" << "\n";
cout << Studentfile[0] << "\n";
cout << "###############################################################################" << "\n";
cout << endl;
cout << "\n\n" << "Do you wish to play again? Y/N " << "\n\n";
cin >> again;
system("cls");
}
}
}
else if(Icon == 4) // Icon Choice 4
{
for (i=1;i<=1;i++)
{
for (j=1;j<=i;j++);
{
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << "\n";
cout << Studentfile[0] << "\n";
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << "\n";
cout << endl;
cout << "\n\n" << "Do you wish to play again? Y/N " << "\n\n";
cin >> again;
system("cls");
}
}
}
else // Icon Choice 5
{
cout << "Sorry You Didn't Want to Play" << "\n\n";
cout << "\n\n" << "Do you wish to play again? Y/N " << "\n\n";
cin >> again;
system("cls");
}
}
}
} // End Menu Loop
} // End Main Loop
cout << "Thank you for playing!" << "\n\n";
system("pause");
return 0;
}
Probably because you define menu .. menu5 as int. Try:
char menu = 'y';
char menu2 = 'n';
char menu3 = 'n';
char menu4 = 'n';
char menu5 = 'n';
....

C++ LogIn.exe crashes in terminal

I am trying to create a LogIn screen in c++ in Microsoft Visual Studios 2013
the code works but after this:
cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
p1.getData1();
}
The program will crash and show this:
Unhandled exception at 0x50E1DF58 (msvcp120d.dll) in LogIn.exe:
0xC0000005: Access violation reading location 0x008AD1D4.
Any help will be great... Thank you!!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class account
{
protected:
string username1;
string username2;
string username3;
string username4;
string username5;
string pswd1;
string pswd2;
string pswd3;
string pswd4;
string pswd5;
public:
int menuSelect;
void getData1()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username1;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd1;
cout << endl;
}
void getData2()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username2;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd2;
cout << endl;
}
void getData3()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username3;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd3;
cout << endl;
}
void getData4()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username4;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd4;
cout << endl;
}
void getData5()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username5;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd5;
cout << endl;
}
void showData1()
{
cout << endl;
cout << "Your username is: " << username1 << endl;
cout << endl;
cout << "Your password is: " << pswd1 << endl;
cout << endl;
}
void showData2()
{
cout << endl;
cout << "Your username is: " << username2 << endl;
cout << endl;
cout << "Your password is: " << pswd2 << endl;
cout << endl;
}
void showData3()
{
cout << endl;
cout << "Your username is: " << username3 << endl;
cout << endl;
cout << "Your password is: " << pswd3 << endl;
cout << endl;
}
void showData4()
{
cout << endl;
cout << "Your username is: " << username4 << endl;
cout << endl;
cout << "Your password is: " << pswd4 << endl;
cout << endl;
}
void showData5()
{
cout << endl;
cout << "Your username is: " << username5 << endl;
cout << endl;
cout << "Your password is: " << pswd5 << endl;
cout << endl;
}
string getUsername1()
{
return username1;
}
string getUsername2()
{
return username2;
}
string getUsername3()
{
return username3;
}
string getUsername4()
{
return username4;
}
string getUsername5()
{
return username5;
}
string getPswd1()
{
return pswd1;
}
string getPswd2()
{
return pswd2;
}
string getPswd3()
{
return pswd3;
}
string getPswd4()
{
return pswd4;
}
string getPswd5()
{
return pswd5;
}
};
int accept()
{
int menuSelect;
cout << "Press 1 to do something" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
cout << "Do something 1" << endl;
cout << endl;
}
else
{
cout << "Error: Invalid Choice" << endl;
}
return 0;
}
int main()
{
account p1;
ifstream infile("DATA.DAT", ios::binary);
infile.read(reinterpret_cast<char*>(&p1), sizeof(p1));
char answer;
int menuSelect;
cout << "Have you created an account yet? y/n" << endl;
cin >> answer;
if (answer == 'n' || 'N')
{
cout << endl;
cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
p1.getData1();
}
else if (menuSelect == 2)
{
p1.getData2();
}
else if (menuSelect == 3)
{
p1.getData3();
}
else if (menuSelect == 4)
{
p1.getData4();
}
else if (menuSelect == 5)
{
p1.getData5();
}
else
{
cout << "Error: Invalid Choice" << endl;
}
ofstream outfile("DATA.DAT", ios::binary);
outfile.write(reinterpret_cast<char*>(&p1), sizeof(p1));
}
else if (answer == 'y' || 'Y')
{
string nam;
cout << endl;
cout << "Please enter your username:" << endl;
cin >> nam;
cout << endl;
if (nam == p1.getUsername1() || nam == p1.getUsername2() || nam == p1.getUsername3() || nam == p1.getUsername4() || nam == p1.getUsername5())
{
string pwd;
cout << "Please enter your password:" << endl;
cin >> pwd;
cout << endl;
if (pwd == p1.getPswd1() || pwd == p1.getPswd2() || pwd == p1.getPswd3() || pwd == p1.getPswd4() || pwd == p1.getPswd5())
{
cout << "Access Granted" << endl;
cout << endl;
cout << "Press 1 to start program" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
return accept();
}
}
else
{
cout << "Access Denied" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 0)
{
return main();
}
}
}
else
{
cout << "Access Denied" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 0)
{
return main();
}
}
}
else
{
cout << endl;
cout << "Error: Invalid Choice" << endl;
}
return 0;
}
You can't read p1 from file that way! p1 is a complex class where fields (strings) have non trivial initializations. If you save p1 to disk then you won't have text of string inside the file, but probably (depend on implementation) pointers to actual text content. When you read them you actually initialize the string with pointers to unallocated memory, hence the access violation.

My program will loop regardless of whatever char I enter

I have been working on a very very basic calculation program in C++. It calculates the square root of a number, and also squares it if the user wants. This is what I have so far (I know it's probably rubbish code, but I'm a beginner just experimenting to see how it all works. Any suggestions greatly appreciated though):
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int number; // Global variables to be used in void functions as well as main.
int squaredNumber;
double sqrtResult;
char input;
char useAgain;
void squareNum(); // Prototypes for the void functions
void sqrtNum();
void useAgainQuery();
int main()
{
retry: // Establishing somewhere to send user if their input is invalid.
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Do you want to square a number or find the square root of a number?" << endl;
cout << "Please select 1 or 2 respectively." << endl;
cout << endl;
cin >> input;
if (input == '1')
{
cout << "Please press ENTER to continue." << endl;
cin.ignore().get();
squareNum(); // If the input is 1, run the void to square a number.
}
else if (input == '2')
{
cout << "Please press ENTER to continue." << endl;
cin.ignore().get();
sqrtNum(); // If the input is 2, run the void to sqrt a number.
}
else if (input != '1' || '2')
{
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Your selection was invalid, please enter 1 or 2." << endl;
cin.ignore().get();
goto retry; // If the input isn't either 1 or 2, send back to the start of program.
}
return 0;
}
void squareNum() // function to square the inputted number.
{
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Enter the number you want to square." << endl;
cin >> number;
cout << "You have chosen: " << number << endl;
cout << "Press ENTER to calculate." << endl;
cin.ignore().get();
system("cls");
squaredNumber = number * number; // Simple maths to find the square number
cout << "You have squared " << number << "." << endl;
cout << "The result was " << squaredNumber << "." << endl;
cout << "Press ENTER to continue." << endl;
cin.get();
useAgainQuery();
return;
}
void sqrtNum()
{
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Enter the number you would like the square root of." << endl;
cin >> number;
cout << "You have chosen: " << number << "." << endl;
cout << "Press ENTER to calculate." << endl;
cin.ignore().get();
system("cls");
sqrtResult = sqrt(number);
cout << "You have found the square root of " << number << "." << endl;
cout << "The result was: " << sqrtResult << "." << endl;
cout << "Press ENTER to continue." << endl;
cin.get();
useAgainQuery();
return;
}
void useAgainQuery()
{
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Would you like to make another calculation?" << endl;
cout << "Y for Yes and N for No." << endl;
cout << endl;
cin >> useAgain;
if (useAgain == 'Y' || 'y')
{
retry2: // Establishing somewhere to send user if their input is invalid.
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Do you want to square a number or find the square root of a number?" << endl;
cout << "Please select 1 or 2 respectively." << endl;
cout << endl;
cin >> input;
if (input == '1')
{
cout << "Please press ENTER to continue." << endl;
cin.ignore().get();
squareNum(); // If the input is 1, run the void to square a number.
}
else if (input == '2')
{
cout << "Please press ENTER to continue." << endl;
cin.ignore().get();
sqrtNum(); // If the input is 2, run the void to sqrt a number.
}
else if (input != '1' || '2')
{
system("cls");
cout << "Square Calcualtions" << endl;
cout << "******************" << endl;
cout << endl;
cout << "Your selection was invalid, please enter 1 or 2." << endl;
cin.ignore().get();
goto retry2;
}
}
else if (useAgain != 'Y' || 'y')
return;
return;
}
So yeah, when I go through and it asks "Would you like to play again", it goes through it over and over. It doesn't matter what key I press but it loops. Any help would be greatly appreciated!
Change your condition here:
if (useAgain == 'Y' || 'y')
to
if (useAgain == 'Y' || useAgain=='y')
Also, change this:
else if (useAgain != 'Y' || 'y')
{
return;
}
to this:
else if (useAgain != 'Y' && useAgain!='y')
{
return;
}
Perhaps try creating a bool variable that controls the entire main loop, like this:
#include <InsertLibrariesHere>
int main(){
bool running=true;
while(running){
//calculations here
//continue(Y/N)?
if (input == N || input == n){running = false;}
}
}
Don't use goto, use a while(1) instead.
your last if statement is wrong, it needs to be
if (input != '1' && input != '2')
You can simplify your comparisons to letters:
if (std::toupper(useAgain) == 'Y')
or
if (std::tolower(useAgain) == 'y')
You can also convert the case after you input:
cin >> useAgain;
useAgain = std::toupper(useAgain);
if (useAgain == 'Y')
There is also std::transform is you need to transform a std::string to all lower or all upper case.