converting a bool in an array of records to a string name?

Hello everyone,
I have a file that looks like the following:
222-222-2222 1250 1
111-111-1111 700 0
This file is put into an array of records. The last number on each line is a bool, which indicates "1" is rented and "0" is available. When I go to display the contents on the array, it needs to display "rented" for 1 and "available" for 0. The program complies with no errors but only displays 1's and 0's.This is what I have so far. Any help would be appreciated. Thank you.

Filename is "RENTALS.txt"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  #include <iostream>                  // for user I/O
#include <fstream>                   // for file streams
#include <iomanip>                   // for fixed, setw and setprecision
#include <cctype>                    // for toupper()
#include <cmath>                     // for absolute functions
#include <string>

using namespace std;

// global constants
const int MAX_ARRAY = 500;            // max number of rentals
const int NOT_FOUND = -1;             // index when not found
const string IN_RENTAL_FILE = "RENTALS.txt";
 
// global structure definitions
struct apartmentInfo 						// apartment information
{                      
        string phoneNum;                    // phone number  
        float rent;                         // rental price
        bool rentalStatus;                  // apartment rented = 1; vacant apartment = 0
};

// function prototypes
void header();
void readRentals (ifstream&, apartmentInfo [], int&);
bool loadDataPrompt (ifstream&, apartmentInfo [], int&, bool&);
char menuChoice();
void pickingOptions(char, ifstream&, apartmentInfo[], int&);
void showWord (char, apartmentInfo[], int&);
void displayRental(apartmentInfo[], int&);
//**************************************************************************
//  FUNCTION:       main
//  DESCRIPTION:    Driver for program
//                  Calls function to get data from client data file
//                  Calls function to matching client to possible dates          
//  OUTPUT:         Return Value:  0 for success
///  CALLS TO:	    readClients, matchClients
//**************************************************************************
int main()
{	
  	char anwser;					//user input for 'Y' or 'N'
  	//string inputtedFileName;
  	int rentalCount = 0;
  	//int targetIndex;
  	//char choice;
  	//char proceedOrNot;
  	bool arrayFull = false;
  	bool returnValue = true;
  	bool loadDataResults; 
  	bool exitLoop = false;
  	
  	// opening the text file CREDIT.txt
  	ifstream inRentalData;
  	inRentalData.open(IN_RENTAL_FILE.c_str());
  	//declare an array for 
  	apartmentInfo addApartment [MAX_ARRAY];
  	//display the description of the program
  	header();
  	cout << endl;
  	
  	// function to ask user to load data or not
  	loadDataResults = loadDataPrompt (inRentalData, addApartment, rentalCount, returnValue);
  	// if first prompt for "Load data" is exit
  	if (loadDataResults == false)
  	{
  		cout << "Goodbye!" << endl;
		return 5;	
	}
	// while user input is 'S', 'A', or 'D', loop will continue
	while (!exitLoop && loadDataResults == true)
	{
		anwser = menuChoice();
		if (anwser == 'E')
		{
			cout << "Goodbye!" << endl;
			exitLoop = true;
		}
		pickingOptions(anwser, inRentalData, addApartment, rentalCount);
		cout << endl;
	}// end while loop
  	
  		
	cin.ignore();
	return 0;
}
  	
 //**************************************************************************
void header()
{
	cout << "**************************************************************" << endl;
	cout << "*\tApartment Rental Services                            *" << endl;
	cout << "*                                                            *" << endl;
	cout << "*  This program will help the use and management of the file *" << endl;
	cout << "*  of an apartment rental company.                           *" << endl;
	cout << "**************************************************************" << endl << endl << endl;
	return;
} 	
//**************************************************************************
void readRentals (ifstream& inRentalData, apartmentInfo addApartment[], int& cnt)
{
	bool arrayFull = false;
	string temp;
	int tempStatus;
	
	inRentalData >> temp;		// prime read;
	while ( inRentalData && !arrayFull ) 								// while data read and more room
	{        
     //	inRentalData >> temp;		// prime read;
        if (cnt < MAX_ARRAY) 
		{
        	addApartment[cnt].phoneNum = temp;
        
	        // read rest of data for this client
	        inRentalData >> addApartment[cnt].rent 
	                     >> tempStatus; 
			tempStatus = static_cast<bool> (tempStatus);
			addApartment[cnt].rentalStatus = tempStatus;    
	        cnt++;
	        inRentalData >> temp;		// try to read next client's code
        }
        else 
		{
        	arrayFull = true;
        	cout << "Too many rentals in data file." << endl
        	     << "Only first " << MAX_ARRAY << " rentals will be used" 
				 << endl << endl;
        }
    }
    inRentalData.close(); 
	return;   
}  	
//**************************************************************************
bool loadDataPrompt (ifstream& inRentalData, apartmentInfo addApartment[], int& cnt, bool& results)
{
	string fileName;		//user input for phone number
	char choice;
	char anwser;
	
	//user prompt to load existing data
  	cout << "Load existing data from a file (Y/N) ? ";
  	cin >> anwser;
  	anwser = toupper(anwser);
		  	
	if(anwser == 'N')		
		results = true;		
	else
	{	
		// function to search for the student ID
		cout << endl;
		cout << "Please input file name to load data file (must add .txt to end): " ;
		cin >> fileName;
		if (fileName == IN_RENTAL_FILE)	
		{
			cout << "File opened successfully! Reading file into array." << endl << endl;
			readRentals (inRentalData, addApartment, cnt);
			results = true;	
		}
		else if (fileName != IN_RENTAL_FILE)
		{
			cout << endl;
			cout << "ERROR! File does not exist. Do you wish to exit "
			 	<< "or proceed with no data\n "
			 	<< "('E' for exit and 'P' to proceed): ";
			cin >> choice;
			choice = toupper(choice);
		
			if (choice == 'E')
				results = false;
			else
				results = true;	
		}		
	}
	return results;
}  
//*****************************************************************
char menuChoice()			// menu will show rentals, add and delete rentals
{
	char choice;
	cout << endl;	
	cout << "\tMENU CHOICE" << endl<< endl;
	cout << "  S -	Displays the rentals on file.\n"
		 << "  A -   Adds new rental information to file.\n" 
		 << "  D -	Delete a rental on file.\n" 
		 << "  E -	Exit the program." << endl << endl;
	
	cout << " Please enter menu choice (S, A, D, E): ";
	cin >> choice;
	choice = toupper(choice);
		
	return choice;
}
//*************************************************************
void pickingOptions(char selection, ifstream& inRentalData, apartmentInfo addApartment[], int& cnt)
{
	// variable
	char display;
	char add;
	char delet;
	string statusWord;
	
	switch (selection)
	{
		case 'S':
			// Display all the apartment information on file
			showWord (selection, addApartment, cnt);
			displayRental(addApartment, cnt);
			break;
		case 'A':
			//additionalApartment();
			break;
		case 'D':
			//deleteApartment();
			break;				
	}
	return;
}  
//*******************************************
void showWord (char status, apartmentInfo addApartment[], int& cnt)
{
	string tempStatus;
	for (int count = 0; count < cnt; count++)
	{
		if (addApartment[count].rentalStatus == 1)	
			addApartment[count].rentalStatus = "Rented";
		else 
			addApartment[count].rentalStatus = "Available";
	}
	return;
}
//*******************************************

void displayRental(apartmentInfo addApartment[], int& cnt)
{
	cout << endl << endl;
	cout << " Phone Number     Monthly Rent      Status" << endl;
	cout << " -------------    ------------      ---------" << endl;
	
	for (int count = 0; count < cnt; count++)
	{
		cout << fixed << showpoint << setprecision(2);
		cout << setw(13) << addApartment[count].phoneNum
			 << setw(17) << addApartment[count].rent
			 << setw(12) << addApartment[count].rentalStatus;
		cout << endl;
	}
	return;
} 

Not actually tested,

1
2
3
4
5
6
7
8
9
10
11
12
	for (int count = 0; count < cnt; count++)
	{
		cout << fixed << showpoint << setprecision(2);
		cout << setw(13) << addApartment[count].phoneNum
			 << setw(17) << addApartment[count].rent;
                if (addApartment[count].rentalStatus == true)
                    cout << setw(12) << "RENTED";
                else
                    cout << setw(12) << "AVAILABLE";
		cout << endl;
	}
	return;

Thank you CodeWriter,
That works! I wonder why the way I had it set up didn't work. Can you not convert bool to a string character?
this is probaby better:

cout<< (addApartment[count].rentalStatus ? "RENTED" : "AVAILABLE")<< endl;
Thanks for the reply. I am fairly new to programming, so I'm not too sure what the ? means. My guess is that it's using a bool and rented means true and available means false. Can you please clarify this?
C++ provides the conditional operator (?:) that is closely related to the if/else structure.
The conditional operator is C++'s only ternary operator - takes three operands.
They form a conditional expression.
http://mathbits.com/MathBits/CompSci/looping/operator2.htm
Last edited on
That makes sense. Thank you for the link.
What I suggested was a ternary conditional operator. ( http://en.cppreference.com/w/cpp/language/operator_other )

The expression is: (a_bool ? eval_if_true : eval_else)

From www.cppreference.com:
The first operand of the conditional operator is evaluated and contextually converted to bool. After both the value evaluation and all side effects of the first operand are completed, if the result was true, the second operand is evaluated. If the result was false, the third operand is evaluated.
Last edited on
Topic archived. No new replies allowed.