Can do while can be used for twice in the same program?

May 20, 2019 at 5:02pm
Let's say I'm making a program for tuition class, I wanted to ask "Do you want to add another registration?"(the do while will be placed at the very outer of the whole program) and "Do you want to add individual subject" (do while will be placed at the outer of if/else statement, is it possible to use it twice?

do
{
cout<<"\nPlease enter category ( PT3 ) : ";
cin>>category;

if (strcmp(category ,"PT3")==0 || strcmp(category ,"pt3")==0)
{
cout<<"HELLO PHYSICS, CHEMISTRY!";
}
else
{
cout<<"ERROR!";
}
cout<<"\n\nDo you want to add another registration? (Y-YES / N-NO) : ";
cin>>registration ;
} while (registration =='Y');
May 20, 2019 at 5:21pm
If you're asking if you can have a do-while loop inside a do-while loop, the answer is yes.

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
// Example program
#include <iostream>
#include <string>

using namespace std;

int main()
{
    char registration;
    do
    {
        std::string category;
        do
        {
            cout<<"category : ";
            cin >> category;
            
            if (category != "PT3" && category != "pt3")
                cout << "Error, try again. ";
        }
        while (category != "PT3" && category != "pt3");

        cout<<"Another registration? (Y-YES / N-NO) : ";
        cin >> registration ;
    } while (registration =='Y');
}
May 21, 2019 at 4:37am
I would also use toupper or tolower for registration. Also, make sure to validate the registration answer because what if the user types in a letter other than Y || N? i.e. other letters or lowercase Y||N
Last edited on May 21, 2019 at 4:41am
Topic archived. No new replies allowed.