HELP with do-while loop

here is the question im sorry it might be silly but it is because i have just started learning c++
My question is that im trying to get information from the user as long as what i get from the user is not H,P,or U i want it to ask the user to enter the iformation again and display a warning message


1
2
3
4
5
6
7
8
//Buildings data entry section
	for (int i = 0; i < numOfBuildings; i++)
	{
		cout << "Enter information for building  " << i + 1 << ":" << endl;
		cout << "---------------------------------" << endl;
		cout << "Select Building type(H: for Hospital, P: for Pharmacy, U: for University): ";
		cin >> buildingType[i];
 


my cin will be buildingType[i] whenever i try to do do-while loop im not successful i need to ask the user to enter the buildingType until they enter H, U, or P help please
Check the input before you put it in your array. If it isn't what you want, tell the user and ask them to re-enter the info.
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
#include <iostream>
using namespace std;

int main()
{
    int numBuildings = 10;
    char* buildingType = new char[numBuildings];

    for (int i = 0; i < numBuildings; ++i)
    {
        char usrInput;
        while (true)
        {
            cout << "Enter information for building  " << i + 1 << ":" << endl;
            cout << "---------------------------------" << endl;
            cout << "Select Building type(H: for Hospital, P: for Pharmacy, U: for University): ";
            cin >> usrInput;

            if (usrInput == 'H' || usrInput == 'P' || usrInput == 'U')
            {
                buildingType[i] = usrInput;
                break; //break out of while(true)
            }
            else
            {
                cout << "Building type not recognized. Try again." << endl << endl;
            }
        }
    }

    delete[] buildingType;
    return 0;
}
Topic archived. No new replies allowed.