//*****************************************************************************
//CODE FILENAME: Taylor-wk8-prog1
//DESCRIPTION: This program reads a file that contains data about apartments for
//rent. It then allows the user to add/delete listing from that file.
//CLASS/TERM: CS362/Summer-Term 2
//DESIGNER: Cody Taylor
//FUNCTIONS: readFile - reads the file
// newFile - ask user to load existing file
// userOption - user chooses what to do
// addNew - function is user wants to add new listing
// deleteList - function if user wants to delete a listing
// writeData- function to write data the user enters back to the file
// main - calls the functions
//*****************************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
#include <iomanip>
#include <cstdlib>
usingnamespace std;
struct houses{ //creates a struct for the listing
string phoneNum;
double price;
string vacancy;
houses* next;
};
houses *housesPtr,
*listTop = NULL;
constint NOT_FOUND = -1;
constint MAX = 50000; //max listings
const string APT_INFO = "RENTALS.TXT";
bool ShowRentals(houses* &listTop);
char CheckChoice(char pick);
bool ReadFile(char& , houses* &listTop);//reads the file
char newFile();//ask user to load existing file
void userOption(int& count);//user chooses what to do
void addNew(int& count);//function is user wants to add new listing
void deleteList(int& count);//function if user wants to delete a listing
void writeData(int count);//function to write data the user enters back to the
//file
//*********************************************************************
// FUNCTION: main
// DESCRIPTION: Calls the functions, gives description to user
//**********************************************************************
int main()
{
char choiceOne;
char choice;
char useFile; // read in data from existing file
bool valid = true; // bool flags for successful functions
bool flag = true;
bool okay = true;
bool firstTry = true; // for checking whether to skip newline
bool content = true; // there's data in the file
bool correct = true; // phone is correct
string userPhone; // entered at keyboard
int found = NOT_FOUND;
string fileName; // user entered filename
string phoneNumber; // user entered phone number
char useData;
int count = 0;
cout << "This program will give a list of apartments to the user. It will ""provide the user with the phone number, price, and whether or not ""the apartment is rented."
<< endl << endl;
cout << "Load existing data from a file? (Y/N) ";
cin >> choiceOne;
choiceOne = toupper(choiceOne);
if (choiceOne == 'Y') // only if read file in
{
flag = ReadFile (choiceOne, listTop);
}
// do // whether file read or not
// {
cout << endl << endl;
cout << "Main Menu: " << endl;
cout << " S - Show Rentals" << endl;
cout << " M - Modify Monthly Rent" << endl;
cout << " A - Add Rental" << endl;
cout << " D - Delete Rental" << endl;
cout << " E - Exit Program" << endl;
cout << "Please enter selection from choices above: ";
cin >> choice;
choice = toupper(choice);
choice = CheckChoice (choice);
return 0;
}
bool ReadFile (char& useFile, houses* &listTop)
{
ifstream rentalFile;
char answer;
char pauseChar;
string phoneNum;
double price;
string phone;
double rent;
bool vacancy;
string fileName;
bool rented;
bool flag;
houses *newRental;
listTop = NULL;
houses *last = NULL;
do
{
// if user wants to use existing data
if (useFile == 'Y')
{
cout << "Enter the filename: ";
cin >> fileName;
rentalFile.open(fileName.c_str());
if (!rentalFile)
{
cout << "Error! File not found.";
cin.clear();
cout << endl << "Enter new filename (Y) or go directly ";
cout << "to action menu (N)? ";
cin >> useFile;
useFile = toupper (useFile);
flag = false;
}
}
} while ((!rentalFile) && (useFile == 'Y'));
if (useFile == 'Y')
{
rentalFile >> phone; // prime read
newRental = new (nothrow) houses;
newRental->next = NULL;
do
{
if (rentalFile)
{
rentalFile >> rent >> vacancy;
if ( newRental == NULL)
{
cout << "Cannot allocate space." << endl << endl;
cin >> pauseChar;
flag = true;
}
else
{
if (listTop == NULL)
{
last = newRental->next;
listTop = newRental;
}
else
{
newRental->next = listTop;
listTop = newRental;
}
newRental -> phoneNum = phone;
newRental -> price = rent;
newRental -> vacancy = vacancy;
rentalFile >> phoneNum;
newRental = new (nothrow) houses;
newRental->next = NULL;
}
}
} while ((newRental != NULL) && (rentalFile));
rentalFile.close();
flag = true;
}
return flag;
}
char CheckChoice(char pick)
{
while ((pick != 'S') && (pick != 'M') && (pick != 'A') && (pick != 'D') && (pick != 'E'))
{
cout << endl;
cout << "Invalid entry, please enter a valid choice from the list below.\n";
cout << endl << endl;
cout << "Main Menu: " << endl;
cout << " S - Show Rentals" << endl;
cout << " M - Modify Monthly Rent" << endl;
cout << " A - Add Rental" << endl;
cout << " D - Delete Rental" << endl;
cout << " E - Exit Program" << endl;
cout << "Please enter selection from choices above: ";
cin >> pick;
pick = toupper(pick);
}
return pick;
}
bool ShowRentals(houses* &listTop)
{
}
void ShowRentals(houses* &listTop)
{
//copy the Node into temporary one
houses * walker = listTop ;
while(walker) //This loop will stop when it reaches the end of the list.. ( when walker == null)
{
cout << walker->phonenum <<" " << walker-> price << " " << walker->vacancy << '\n' ;
walker = walker->next ;
}
}
I don't know if this is what you want exactly .. But I give it a try ...
and why it is return bool in your code ??
and why the parameter is a reference to pointer and not a pointer only ??