C++ LogIn.exe crashes in terminal - c++

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.

Related

(C++) Read username and password from a file output problem

my program of registration and login encountered a few problems during output :
I have to register a new user and pass first(even if i alrd have previous usernames and passwords stored in the text file im trying to retrieve it from), after that only i can login using previous username and passwords and this repeats after i close the debug window and start debugging again (if i directly choose to login upon running the program, it will output "invalid username or password")
when logging out from a newly registered username, the program jumps to the
int main() AND DISPLAY "1. Register...."
but logging out from previous usernames, it jumps to
void login() and display "Username:"
*note: the last function isn't complete yet but i think it doesn't affect it (?) (the program worked fine before i added the void accountPage()tho)
*i am not supposed to use pointers plus i'm very new to c++
the code is a bit long but its just a lot of simple functions, i would rly appreciate it if someone can point out my mistake anywhere
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
//Global Variables
int Choice1;
int mobile, ic;
string user, pass, name, inUser, inPass;
//Function Prototypes
void register_user();
void login();
void bookRoom();
bool CheckCredentials(string, string);
void accountPage();
int main()
{
cout << "Welcome to Cozy Homes!\n";
cout << "Operating hours: 11am - 10pm Tuesday - Sunday\n\n\n\n";
do {
cout << "\n1. Register\n";
cout << "2. Log In\n";
cout << "3. Exit\n";
cout << "Please enter a number:";
cin >> Choice1;
if (Choice1 == 1)
{
register_user();
}
else if (Choice1 == 2)
{
login();
}
else if (Choice1 == 3)
{
cout << "Exiting now...\n";
return 0;
}
else if (Choice1 < 1 || Choice1 > 3)
{
cout << "Please choose a number from the menu!" << endl;
}
} while (Choice1 != 3);
system("pause");
return 0;
}
//Register page
void register_user()
{
cin.ignore();
cout << "\n\n\n" << "New Username: ";
getline(cin, user);
cout << endl;
cout << "New Password: ";
getline(cin, pass);
cout << endl;
cout << "Full name: ";
getline(cin, name);
cout << endl;
cout << "Mobile Number: ";
cin >> mobile;
cout << endl;
cout << "Ic Number (without \" - \"): ";
cin >> ic;
cout << endl;
cout << "Registered Successfully!" << endl;
cout << endl;
//Store username and password in login file
ofstream l("login.txt", ios::app);
if (!l.is_open()) {
cout << "could not open file \n";
}
l << user << " " << pass << endl;
l << endl;
l.close();
//Store other details in customer file
ofstream c("customer.txt", ios::app);
if (!c.is_open()) {
cout << "could not open file \n";
}
c << user << endl;
c << pass << endl;
c << name << endl;
c << mobile << endl;
c << ic << endl;
c << '\n';
c.close();
}
//Log in page
void login()
{
do
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
} while (CheckCredentials(inUser, inPass) != true);
}
//Validate their username and password
bool CheckCredentials(string inUser, string inPass)
{
string u;
string p;
bool status = false;
ifstream f;
f.open("login.txt");
if (!f.is_open())
{
cout << "Unable to open file!\n";
}
else if (f)
{
while (!f.eof())
{
f >> u >> p;
if (inUser == u && inPass == p)
{
status = true;
}
else
{
status = false;
}
}
}
f.close();
return status;
}
//Account Page
void accountPage()
{
int Choice2;
do
{
cout << "1. Profile\n";
cout << "2. Book a Room\n";
cout << "3. Cancel Booking\n";
cout << "4. Logout\n";
cout << "Please enter a number: ";
cin >> Choice2;
if (Choice2 == 1)
{
}
else if (Choice2 == 2)
{
}
else if (Choice2 == 3)
{
}
else if (Choice2 == 4)
{
cout << "Logging out.....\n\n\n\n";
cout << endl;
}
} while (Choice2 != 4);
}
//Booking page
void bookRoom() {
cout << " ";
}
```
Like this :
void login()
{
bool loggedin = false;
while(!loggedin)
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
loggedin = true;
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
}
}
or like this:
void login()
{
bool loggedin = false;
do
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
loggedin = true;
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
}
while(!loggedin)
}
As this is a school assignment I did not bother to test the code myself.
It is just meant to get you futher and I did only minimal changes.
The point of your error is that the faulty function calls checkcredentials before a user and pasword is entered in the system.
If this solves your problem please mark as solution.
l << user << " " << pass << endl;
l << endl; <---- creates an empty line in your file. Is this intentional ?
l.close();
Clearly there is more bugs in your program. But this will get you further.
Have to do my own job now ;-)

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

Run Time Error When Program is Running C++

I am getting a runtime error when the program is running it takes the username but then when it comes for password it shows me: Debug Error Run Time Check Failure #3-T.
#include <iostream>
using namespace std;
int main()
{
int choice;
float username, password; //login
int name, age, gender, dob, address, workinfo;
cout << "Welcome To HDFC Bank" << endl;
//Menu Option
cout << "Choose an option: " << endl;
cout << "===========================" << endl;
cout << "1. Login" << endl;
cout << "2. Register" << endl;
cout << "===========================" << endl;
cin >> choice;
if (choice == 1) {
cout << "Please Enter Your Username: " << endl;
cin >> username;
cout << "Please Enter your Password: " << endl;
cin >> password;
if (choice == 1 || password = 2) {
cout << "Welcome To The Program!!!" << endl;
}
else {
cout << "Wrong Details!!" << endl;
}
}
else if (choice == 2) {
cout << "Enter Your Full Name: " << endl;
cin >> name;
cout << "Enter Your Age" << endl;
cin >> age;
cout << "Enter Your Date of Birth(dd/mm/yyyy): " << endl;
cin >> dob;
cout << "Enter Your Gender(M/F)" << endl;
cin >> gender;
cout << "Enter Your Address: " << endl;
cin >> address;
cout << "Enter Your Work Details: " << endl;
cin >> workinfo;
}
if (age < 21) {
cout << "Sorry You cannot Register as you are below 21 years. Please try later." << endl;
}
else {
cout << "You have succesfully registered. Please check your email." << endl;
}
return 0;
}
You should definitely learn more about Types and STL Streams.
But assuming you're experimenting here is little bit more meaningful version of your code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int choice = 0;
std::string username, password; //login
int age = 0;
std::string name, gender, dob, address, workinfo;
cout << "Welcome To HDFC Bank" << endl;
//Menu Option
cout << "Choose an option: " << endl;
cout << "===========================" << endl;
cout << "1. Login" << endl;
cout << "2. Register" << endl;
cout << "===========================" << endl;
cin >> choice;
cin.ignore();
if (choice == 1) {
cout << "Please Enter Your Username: " << endl;
getline(cin, username);
cout << "Please Enter your Password: " << endl;
getline(cin, password);
if (password == "1" || password == "2") {
cout << "Welcome To The Program!!!" << endl;
}
else {
cout << "Wrong Details!!" << endl;
return 0;
}
}
else if (choice == 2) {
cout << "Enter Your Full Name: " << endl;
getline(cin, name);
cout << "Enter Your Age: " << endl;
cin >> age;
cin.ignore();
cout << "Enter Your Date of Birth(dd/mm/yyyy): " << endl;
getline(cin, dob);
cout << "Enter Your Gender(M/F)" << endl;
getline(cin, gender);
cout << "Enter Your Address: " << endl;
getline(cin, address);
cout << "Enter Your Work Details: " << endl;
getline(cin, workinfo);
}
if (age < 21) {
cout << "Sorry You cannot Register as you are below 21 years. Please try later." << endl;
}
else {
cout << "You have successfully registered. Please check your email." << endl;
}
return 0;
}
Note that we use cin.ignore() after reading int as described here

Is it a good idea if I put all this in a different function?

So I'm working on this endterm project. Which the teacher said that we should use functions now. Which he recently introduced to us. My question is if it's a good idea to put every option into a function? Functions are for organizing and for code reuse right? Or I'm missing the point of functions. XD
What I mean is like making a function for option 1, which is add account. Instead of putting it in the main function. So far I only made a function for options 3,4 and 5. Which are search, view and delete functions
#include <iostream>
#include <string>
using namespace std;
bool accountSearch(int searchParameter);
void viewList();
void deleteFromList(int delParameter);
int option, numberOfAccounts = 0, accountNumSearch, index, deleteAccount;
struct personAccount
{
int currentBalance, accountNumber, pin;
string lastname, firstname, middlename;
};
personAccount account[20];
int main()
{
do{
cout << "[1] Add Account" << endl
<< "[2] Edit Account" << endl
<< "[3] Search Account" << endl
<< "[4] View Account" << endl
<< "[5] Delete Account" << endl
<< "[6] Inquire" << endl
<< "[7] Change Pin Number" << endl
<< "[8] Withdraw" << endl
<< "[9] Deposit" << endl
<< "[10] View Transactions" << endl
<< "[11] Exit" << endl << endl
<< "Option [1-11]: ";
cin >> option; cout << endl;
if(option == 1)
{
if(numberOfAccounts != 20)
{
cout << "Account Number: ";
cin >> account[numberOfAccounts].accountNumber;
cout << "PIN: ";
cin >> account[numberOfAccounts].pin;
cout << "Lastname: ";
cin >> account[numberOfAccounts].lastname;
cout << "Firstname: ";
cin >> account[numberOfAccounts].firstname;
cout << "Middlename: ";
cin >> account[numberOfAccounts].middlename;
account[numberOfAccounts].currentBalance = 0;
++numberOfAccounts;
cout << endl;
}
else
{
cout << "The list is full!\n\n";
}
}
else if(option == 2)
{
if(numberOfAccounts != 0)
{
cout << "Account Number: ";
cin >> accountNumSearch;
if(accountSearch(accountNumSearch))
{
cout << "PIN: ";
cin >> account[index].pin;
cout << "Lastname: ";
cin >> account[index].lastname;
cout << "First name: ";
cin >> account[index].firstname;
cout << "Middlename: ";
cin >> account[index].middlename;
cout << endl;
}
else
{
cout << "Account not found!\n\n";
}
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 3)
{
if(numberOfAccounts != 0)
{
cout << "Enter account number to search: ";
cin >> accountNumSearch;
if(accountSearch(accountNumSearch))
{
cout << "Found at index " << index << "\n\n";
}
else
{
cout << "Not found!\n\n";
}
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 4)
{
if(numberOfAccounts != 0)
{
viewList();
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 5)
{
if(numberOfAccounts != 0)
{
cout << "Account Number: ";
cin >> deleteAccount;
deleteFromList(deleteAccount);
}
}
}while(option != 11);
}
bool accountSearch(int searchParameter)
{
bool found = 0;
for(int i = 0; i < numberOfAccounts; i++)
{
index = i;
if (account[i].accountNumber == searchParameter)
{
found = 1;
break;
}
}
if(found)
{
return 1;
}
else
{
return 0;
}
}
void viewList()
{
for(int i = 0; i < numberOfAccounts; i++)
{
cout << "Account Number: " << account[i].accountNumber << endl
<< "Lastname: " << account[i].lastname << endl
<< "Firstname: " << account[i].firstname << endl
<< "Middlename: " << account[i].middlename << endl
<< "Current Balance: " << account[i].currentBalance << "\n\n";
}
}
void deleteFromList(int delParameter)
{
if(accountSearch(deleteAccount))
{
for(int i = index; i < numberOfAccounts; i++)
{
account[i] = account[i+1];
}
--numberOfAccounts;
cout << "Deleted Done\n";
}
else
{
cout << "Account not found!\n";
}
}
It's not done yet, but is there anything you would like to mention or suggest?
Yes, you should write functions separately, it's common good practice as a programmer, It will be easier for you(and others) to read, follow, and understand your code.
So, if your options do different things, they should have their own functions. (More like it would be desirable, in the end it's up to you)

Display Data From Text Files

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