ADDING WHILE LOOP !

how do i add a while loop so that after they are done with a function they can choose a function again ?

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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

void VIEWAPPOINTMENTS();
void ADDBOOKINGS();
void VIEWPATIENTSRECORD();
void CALCULATEBMI();
void VIEWDATA();

using namespace std;

int main()
{
	char ch;
	int choice;
	string userID, password;
	string pwdId, pwd;
	bool found = false;
	int i = 0;
	cout << "----------------Login Screen--------------------" << endl;

	
		cout << "Please enter your username : ";
		cin >> userID;
		cout << "Please enter your password : ";
		cin >> password;
		
		ifstream inFile;
		inFile.open("patientandstaff.txt.txt");
		found = false;
		if (!inFile)
			cout << "Unable to open file" << endl;
		else 
			{
				if (userID == "staff" && password == "0006")
				{
					cout << " Welcome Staff ! " << endl;
		
					cout<<"Please choose from the following: \n";
					cout<<"\t1. View patient's records. \n";
					cout<<"\t2. View appointments . \n";
					cout<<"\t3. Quit. \n";
					cout<<endl;

					cout<<"Your choice: ";
					cin>>choice;
	
          
					if (choice == 1)
						VIEWPATIENTSRECORD();
                  
					else if (choice == 2)
						VIEWAPPOINTMENTS();
                   
					else 
						cout<<"\tBYE <33"<<endl;
				}
				else
				{
						cout << "Welcome Patient!" << endl;
						cout << "-------Welcome-------" << endl;
						cout << " \t1Add bookings  " << endl;
						cout << " \t2Calculate BMI  " << endl;
						cout << " \t3View your own data  " << endl;
						cout << " \t4Exit " << endl;
						cout << endl;
						cout <<  "Your Choice : " ;
						cin >> choice;

		                if (choice == 1)
                            ADDBOOKINGS();
                  
                        else if (choice == 2)
                            CALCULATEBMI();

			            else if (choice == 3)
				            VIEWDATA();

						else 
							cout << "Bye" << endl;
				}

		

//				cout<<"\nWould you like to try again (y/n): " << endl;
//				cin>>ch; 
		}
	system("pause");
	return 0;
}




  void VIEWAPPOINTMENTS()
{

    string STRING;
	ifstream infile;
	infile.open ("BOOKINGS.txt.txt");
        while(!infile.eof()) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
	        cout << STRING; // Prints our STRING.
        }
	
	infile.close();
	return ;
	
}
	
   
   void VIEWPATIENTSRECORD()
{
    ifstream input;
    input.open("textfile2.txt.txt");
	
    string firstname,
     lastname,
         contact,
         Datetime;

	bool found = false;
	string fname, lname;
	cout << "Enter first name ";
	cin >> fname;
	cout << "Enter last name ";
	cin >> lname;

		

    while(!input.eof() && !found)
	{   
		input>>firstname>>lastname>>contact>>Datetime;
		if (fname.compare(firstname) == 0 && lname.compare(lastname) == 0)
		{
			found = true;
			cout << "The other info: " << contact << "  " << Datetime << endl;
		}

	}
   

  
	if (!found)
		cout << "No match at all" << endl;
    cout<<"Reading Completed. "<<endl;
    input.close();
    return ;
}
   // STAFF OPTIONS

		
	
		
void ADDBOOKINGS()
{
    ofstream outfile;
    char firstname[20],
         lastname[20],
         contact[20],
         Datetime[20];
    
	
    outfile.open("BOOKINGS.txt.txt",ios::app);

    cout<<"Enter your First Name: ";
    cin>>firstname;
    cin.ignore();
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Contact: ";
    cin>>contact;
    cin.ignore();
    cout<<"Enter your Date and Time: ";
    cin>>Datetime;
    cin.ignore();
   

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<contact<<setw(2)<<Datetime<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return ;
}

void CALCULATEBMI()
	{
		double bmi,bmass,height;
		int hr,min,day,month,year;		
		cout<<"BMI Calculation"<<endl;
			cout<<"Please enter the time at which you are taking the option"<<endl;
			cout<<"(format:hr,min,day,mth,yr=20 02 06 08 2014):";
			cin>>hr;
			cin>>min;
			cin>>day;
			cin>>month;
			cin>>year;
		    cout<<"Please enter your height(in metric units,eg:1.75m):";
			cin>>height;
			cout<<"Please enter your body mass(eg:55kg):";
			cin>>bmass;
			bmi=bmass/(height*height);
			cout<<"Your bmi is:"<<bmi<<endl;
			if(bmi<18.5)
			cout<<"You are underweight."<<endl;
			else if(bmi>=18.5 && bmi <=24.9)
			cout<<"You have an acceptable body mass index."<<endl;
			else if(bmi>24.9 && bmi <=29.9)
			cout<<"You are overweight."<<endl;
			else if(bmi>29.9)
			cout<<"You are obese."<<endl;
		
		}



   void VIEWDATA()
{
    ifstream input;
    input.open("textfile2.txt.txt");
	
    string firstname,
     lastname,
         contact,
         Datetime;

	bool found = false;
	string fname, lname;
	cout << "Enter first name ";
	cin >> fname;
	cout << "Enter last name ";
	cin >> lname;

		
    
    while(!input.eof() && !found)
	{
        input>>firstname>>lastname>>contact>>Datetime;
		if (fname.compare(firstname) == 0 && lname.compare(lastname) == 0)
		{
			found = true;
			cout << "The other info: " << contact << "  " << Datetime << endl;
		}

	}

  
	if (!found)
	cout << "No match at all" << endl;
       
    
    cout<<"Reading Completed. "<<endl;
    input.close();
    return ;
}

	
		
			

		
		
	
	
Last edited on
1. What are the statements that you want to repeat?
2. You can break; from a loop instead of changing the continuation condition false.
i want to after the staff or patient finishes a function it will ask Do you want to continue y/n then the person can choose a function again .
You want a do/while loop around lines 38-85.

I would strongly suggest moving lines 39-60 into a new function, e.g. do_staff_function and also moving lines 62-85 to a a new function. e.g. do_patient_function.

Your do/while loop becomes:
1
2
3
4
5
6
7
8
    do
    {  if (userID == "staff" && password == "0006")
            do_staff_function ()
        else
            do_patient_funtion (); 
        cout<<"\nWould you like to try again (y/n): " << endl;
        cin>>ch; 
    } while (ch == 'y');


Topic archived. No new replies allowed.