i have problem where the address is not save to my file after i input a new address and exit it
#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
typedef struct date {
int day;
int month;
int year;
}Date;
typedef struct add {
string building;
string street;
string city;
string state;
string zip;
}Address;
typedef struct entry {
string firstName;
string lastName;
Address address;
string phNo;
Date birthDate;
struct entry* next;
}AddressEntry;
class AddressBook {
public:
AddressBook();
AddressEntry * getFirst();
void addEntry(AddressEntry *entry);
void print(AddressEntry *entry);
void search(int);
bool deleteentry(int);
void writeToFile();
private:
AddressEntry * firstEntry;
};
AddressBook::AddressBook() {
firstEntry = nullptr;
}
int newEntry(AddressBook *book);
int loadData(AddressBook *book);
int main() {
AddressBook *aBook = new AddressBook;
int noOfEnties = loadData(aBook);
int choice;
while (true) {
cout << "1 New Entry"
<< endl;
cout << "2 Search Entry by
lastName" << endl;
cout << "3 Search Entry by
phone number" << endl;
cout << "4 Delete Entry by
last name" << endl;
cout << "5 Delete entry by
phone number" << endl;
cout << "6 Exit" <<
endl;
cout << "Enter your
choice(1-6) :";
cin >> choice;
switch (choice) {
case 1:
newEntry(aBook);
noOfEnties++;
break;
case 2:
aBook->search(1);
break;
case 3:
aBook->search(2);
break;
case 4:
if
(aBook->deleteentry(1))
noOfEnties--;
break;
case 5:
if
(aBook->deleteentry(2))
noOfEnties--;
break;
case 6:
aBook->writeToFile();
exit(0);
default:
cout <<
"Enter valid choice" << endl;
}
}
}
void AddressBook::addEntry(AddressEntry* entry) {
if (firstEntry == nullptr)
firstEntry = entry;
else {
entry->next = firstEntry;
firstEntry = entry;
}
}
void AddressBook::print(AddressEntry* entry) {
cout << "Name : " << entry->firstName
<< " " << entry->lastName << endl;
cout << "Address: " <<
entry->address.building << " " <<
entry->address.street << " " <<
entry->address.city << " " <<
entry->address.state << " " << entry->address.zip
<< endl;
cout << "Phone number :" << entry->phNo
<< " ";
cout << "Date of Birth : ";
cout << entry->birthDate.month << "
";
cout << entry->birthDate.day << "
";
cout << entry->birthDate.year <<
endl;
}
bool AddressBook::deleteentry(int option) {
string searchString;
if (option == 1)
cout << "Enter last name
:";
else
cout << "Enter phone number
:";
cin >> searchString;
AddressEntry *current = firstEntry;
AddressEntry *previous = nullptr;
while (current != nullptr) {
if (option == 1)
if
(searchString.compare(current->lastName) == 0)
break;
if (option == 2)
if
(searchString.compare(current->phNo) == 0)
break;
previous = current;
current = current->next;
}
if (current != nullptr) {
if (previous == nullptr)
firstEntry =
firstEntry->next;
else
previous->next = current->next;
return true;
}
else
{
cout << "Entry not found"
<< endl;
return false;
}
}
void AddressBook::search(int option) {
string searchString;
if (option == 1)
cout << "Enter last name
:";
else
cout << "Enter phone number
:";
cin >> searchString;
AddressEntry *current = firstEntry;
while (current != nullptr) {
if (option == 1)
if
(searchString.compare(current->lastName) == 0)
break;
if (option == 2)
if
(searchString.compare(current->phNo) == 0)
break;
current = current->next;
}
if (current != nullptr)
print(current);
else
cout << "Entry not found"
<< endl;
}
void AddressBook::writeToFile() {
ofstream out;
out.open("C:\\Users.txt");
if (out.fail())
cout << "Writing to file
failed" << endl;
else {
AddressEntry *current =
firstEntry;
while (current != nullptr) {
out <<
current->firstName << " ";
out <<
current->lastName << " ";
out <<
current->address.building << " ";
out <<
current->address.street << " ";
out <<
current->address.city << " ";
out <<
current->address.state << " ";
out <<
current->address.zip << " ";
out <<
current->phNo << " ";
out <<
current->birthDate.month << " ";
out <<
current->birthDate.day << " ";
out <<
current->birthDate.year << endl;
current =
current->next;
}
}
out.close();
}
int newEntry(AddressBook *book) {
AddressEntry *newentry = new AddressEntry;
cout << "Enter details for new entry :" <<
endl;
cout << "First Name :";
cin >> newentry->firstName;
cout << "Last Name :";
cin >> newentry->lastName;
cout << "Building :";
cin >> newentry->address.building;
cout << "Street :";
cin >> newentry->address.street;
cout << "City :";
cin >> newentry->address.city;
cout << "State :";
cin >> newentry->address.state;
cout << "Zip :";
cin >> newentry->address.zip;
cout << "Phone number :";
cin >> newentry->phNo;
cout << "DOB(MM DD YYYY) :";
cin >> newentry->birthDate.month;
cin >> newentry->birthDate.day;
cin >> newentry->birthDate.year;
book->addEntry(newentry);
return 0;
}
int loadData(AddressBook *book) {
ifstream in;
int count = 0;
in.open("Address.txt");
string record;
if (in.fail())
return 0;
else {
while (getline(in, record)) {
istringstream
ss(record);
AddressEntry
*newEntry = new AddressEntry;
ss >>
newEntry->firstName;
ss >>
newEntry->lastName;
ss >>
newEntry->address.building;
ss >>
newEntry->address.street;
ss >>
newEntry->address.city;
ss >>
newEntry->address.state;
ss >>
newEntry->address.zip;
ss >>
newEntry->phNo;
ss >>
newEntry->birthDate.month;
ss >>
newEntry->birthDate.day;
ss >>
newEntry->birthDate.year;
newEntry->next = nullptr;
book->addEntry(newEntry);
count++;
}
}
in.close();
return count;
}
Ok so you are doing file handling in c++ , you have to take care that your writing file name and reading file name should be same.
as in your program you are writing your data in the file "C:\\Users.txt" in the writeToFile() function
and reading the data from the file "Address.txt" in the loadData() function
so just choose only one file , you have to open the same file for the reading and writing the data. so i;ve changed this for you in the below code.
I hope it'll help you so please give positive rating :)
#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
typedef struct date {
int day;
int month;
int year;
}Date;
typedef struct add {
string building;
string street;
string city;
string state;
string zip;
}Address;
typedef struct entry {
string firstName;
string lastName;
Address address;
string phNo;
Date birthDate;
struct entry* next;
}AddressEntry;
class AddressBook {
public:
AddressBook();
AddressEntry * getFirst();
void addEntry(AddressEntry *entry);
void print(AddressEntry *entry);
void search(int);
bool deleteentry(int);
void writeToFile();
private:
AddressEntry * firstEntry;
};
AddressBook::AddressBook() {
firstEntry = nullptr;
}
int newEntry(AddressBook *book);
int loadData(AddressBook *book);
int main() {
AddressBook *aBook = new AddressBook;
int noOfEnties = loadData(aBook);
int choice;
while (true) {
cout << "1 New Entry" << endl;
cout << "2 Search Entry by lastName" << endl;
cout << "3 Search Entry by phone number" << endl;
cout << "4 Delete Entry by last name" << endl;
cout << "5 Delete entry by phone number" << endl;
cout << "6 Exit" << endl;
cout << "Enter your choice(1-6) :";
cin >> choice;
switch (choice) {
case 1:
newEntry(aBook);
noOfEnties++;
break;
case 2:
aBook->search(1);
break;
case 3:
aBook->search(2);
break;
case 4:
if (aBook->deleteentry(1))
noOfEnties--;
break;
case 5:
if (aBook->deleteentry(2))
noOfEnties--;
break;
case 6:
aBook->writeToFile();
exit(0);
default:
cout << "Enter valid choice" << endl;
}
}
}
void AddressBook::addEntry(AddressEntry* entry) {
if (firstEntry == nullptr)
firstEntry = entry;
else {
entry->next = firstEntry;
firstEntry = entry;
}
}
void AddressBook::print(AddressEntry* entry) {
cout << "Name : " << entry->firstName << " "
<< entry->lastName << endl;
cout << "Address: " << entry->address.building
<< " " << entry->address.street << " "
<< entry->address.city << " " <<
entry->address.state << " " << entry->address.zip
<< endl;
cout << "Phone number :" << entry->phNo << "
";
cout << "Date of Birth : ";
cout << entry->birthDate.month << " ";
cout << entry->birthDate.day << " ";
cout << entry->birthDate.year << endl;
}
bool AddressBook::deleteentry(int option) {
string searchString;
if (option == 1)
cout << "Enter last name :";
else
cout << "Enter phone number :";
cin >> searchString;
AddressEntry *current = firstEntry;
AddressEntry *previous = nullptr;
while (current != nullptr) {
if (option == 1)
if (searchString.compare(current->lastName) == 0)
break;
if (option == 2)
if (searchString.compare(current->phNo) == 0)
break;
previous = current;
current = current->next;
}
if (current != nullptr) {
if (previous == nullptr)
firstEntry = firstEntry->next;
else
previous->next = current->next;
return true;
}
else
{
cout << "Entry not found" << endl;
return false;
}
}
void AddressBook::search(int option) {
string searchString;
if (option == 1)
cout << "Enter last name :";
else
cout << "Enter phone number :";
cin >> searchString;
AddressEntry *current = firstEntry;
while (current != nullptr) {
if (option == 1)
if (searchString.compare(current->lastName) == 0)
break;
if (option == 2)
if (searchString.compare(current->phNo) == 0)
break;
current = current->next;
}
if (current != nullptr)
print(current);
else
cout << "Entry not found" << endl;
}
void AddressBook::writeToFile() {
ofstream out;
out.open("C:\\Users.txt");
if (out.fail())
cout << "Writing to file failed" << endl;
else {
AddressEntry *current = firstEntry;
while (current != nullptr) {
out << current->firstName << " ";
out << current->lastName << " ";
out << current->address.building << " ";
out << current->address.street << " ";
out << current->address.city << " ";
out << current->address.state << " ";
out << current->address.zip << " ";
out << current->phNo << " ";
out << current->birthDate.month << " ";
out << current->birthDate.day << " ";
out << current->birthDate.year << endl;
current = current->next;
}
}
out.close();
}
int newEntry(AddressBook *book) {
AddressEntry *newentry = new AddressEntry;
cout << "Enter details for new entry :" << endl;
cout << "First Name :";
cin >> newentry->firstName;
cout << "Last Name :";
cin >> newentry->lastName;
cout << "Building :";
cin >> newentry->address.building;
cout << "Street :";
cin >> newentry->address.street;
cout << "City :";
cin >> newentry->address.city;
cout << "State :";
cin >> newentry->address.state;
cout << "Zip :";
cin >> newentry->address.zip;
cout << "Phone number :";
cin >> newentry->phNo;
cout << "DOB(MM DD YYYY) :";
cin >> newentry->birthDate.month;
cin >> newentry->birthDate.day;
cin >> newentry->birthDate.year;
book->addEntry(newentry);
return 0;
}
int loadData(AddressBook *book) {
ifstream in;
int count = 0;
in.open("C:\\Users.txt"); // the file location is changed
here
string record;
if (in.fail())
return 0;
else {
while (getline(in, record)) {
istringstream ss(record);
AddressEntry *newEntry = new AddressEntry;
ss >> newEntry->firstName;
ss >> newEntry->lastName;
ss >> newEntry->address.building;
ss >> newEntry->address.street;
ss >> newEntry->address.city;
ss >> newEntry->address.state;
ss >> newEntry->address.zip;
ss >> newEntry->phNo;
ss >> newEntry->birthDate.month;
ss >> newEntry->birthDate.day;
ss >> newEntry->birthDate.year;
newEntry->next = nullptr;
book->addEntry(newEntry);
count++;
}
}
in.close();
return count;
}
Here is the output:
As you can see after new execution i'm able to search a record which is previously entered . you can now test it.
i have problem where the address is not save to my file after i input a...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...
"Function does not take 0 arguments". I keep getting this error for my last line of code: cout << "First Name: " << employee1.getfirstName() << endl; I do not understand why. I am trying to put the name "Mike" into the firstName string. Why is my constructor not working? In main it should be set to string firstName, string lastName, int salary. Yet nothing is being put into the string. #include<iostream> #include<string> using namespace std; class Employee { int...
Hello, I have some errors in my C++ code when I try to debug it.
I tried to follow the requirements stated below:
Code:
// Linked.h
#ifndef INTLINKEDQUEUE
#define INTLINKEDQUEUE
#include <iostream>
usingnamespace std;
class IntLinkedQueue
{
private: struct Node {
int data;
Node *next;
};
Node *front; // -> first item
Node *rear; // -> last item
Node *p; // traversal position
Node *pp ; // previous position
int size; // number of elements in the queue
public:
IntLinkedQueue();...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use switch case to do this but it has not worked I need help with this. Thank you! #include <iostream> #include <string> #include <iomanip> using namespace std; const int...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...