How do I stop do while in if statement from keep looping

How do I stop do while in if statement from keep looping?
do
{
cout<<"\nPlease enter category ( PT3/ SPM ) : ";
cin>>category;

if (strcmp(category ,"PT3")==0 || strcmp(category ,"pt3")==0)
{
cout<<"HELLO PHYSICS, CHEMISTRY!";
cout<<"Do you love physics?";
cin>>love;
do
{
cout<<"I love physics";
}while(love== 'Y');
}
else if (strcmp(category ,"SPM")==0 || strcmp(category ,"SPM")==0)
{
cout<<"BIOLOGY!";
}
else
{
cout<<"error!";
}
cout<<"\n\nDo you want to add another registration? (Y-YES / N-NO) : ";
cin>>registration ;
} while (registration =='Y');
It's kind of a strange question. You must see WHY it's looping forever. If the user enters Y then it will loop forever. That's how you wrote it. So to make it NOT loop forever you should write it in such a way that it doesn't loop forever. There are many ways that might be done. Here's one:

1
2
3
4
5
6
7
if (love == 'Y')
{
    for (int i = 0; i < 10; ++i)
    {
        cout<<"I love physics";
    }
}

(BTW, learn to use [code] [/code] tags so your code is readable like mine above.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
simple loops can be broken with the break statement:

do
{
   error = things();  
   if(error)
       break; //
}
while(true);

you can use a goto as well.  goto is generally frowned upon but to get out of loops it is accepted.  

do
{
   error = things();  
   if(error)
    goto fubar;
}
while(true);
fubar:
Like so:
1
2
3
4
5
6
cin>>love;
do
{
cin>>love;
cout<<"I love physics";
}while(love== 'Y');
Topic archived. No new replies allowed.