looping doesn't stop

Hi guys! Could you help me? So the logic here why did i use two loop syntax which is do while and while, so i wanted after i got the answer or to what am i looking i have a choice to back either visual or to search again. So now, i can't get out of the program. I'm stuck! Help me please. TYIA!

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
  int main()
{
//int choice=0;

do
{

//visual

char again='Y';
while(again=='Y' || again== 'y')
{

//search 
//cin >> choice;

//switch(variable)
{

//case

}

gotoxy(14,21);
cout<<"Press Y to go back search... Press N to exit...";
cin >> again;
getchar();

if(again=='N' || again == 'n')
{
break;
}
}
}
while(choice!=22);

return 0;
}
*with choice commented out, it won't compile.
try this: move again declare out of the loops entirely.
char again = 'Y';
do
{ ...


also why the convoluted logic (breaks seem excessive)? It isnt really clear what you want, but at a guess..
do

stuff (do you NEED to check for y / Y here?)

while again isnt n or N


@jonnin,

Hi sir! to what i understand from your comment. Is it like this?

EDIT: Btw sir, is it possible that while in the search mode i could go back from the visual? Just in case the end-user forgot something that it's looking for?


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
 
int main()
{
char again='Y';
do
{

//visual

//What loop should i put here to get back here?

//int choice=0;

//search 
//cin >> choice;

//switch(choice)
{

//case 


}

}
gotoxy(14,21);
cout<<"Press Y to go back search... Press N to exit...";
cin >> again;
getchar();

while(again != 'N' || again != 'n')

return 0;
}
Last edited on
this version will repeat until it gets N or n, whether they typed Y or Z or Q or anything else. Which is one way to do things, as long as you understand what you are getting with the code you wrote and whether that is acceptable.

it is possible to control your code in many ways, so yes.

But let me ask, how do you get back to 'visual'?
the do-while will go back to visual every time the user presses something that is not N or n. If you need to, you can make this more fine-grained. consider

do

visual(); //a function?
othercode(); //do some stuff
c = getchar()
if(c == 'r') //r_epeat visual?
{
visual(); //repeat it, they forgot and need to see it again
c = getchar(); // get more input if necessary?
}
if(c == 'y') //it could be y because they typed y, or y because they typed r, then y, see?
{ //what to do if they put in y
stuff();
}
while

this sort of stuff can QUICKLY lead to unmanageable code, though. If I had a clear idea of what exactly you wanted to do, we can avoid that problem by designing something better. This works if your logic is fairly simple, and you only have a couple of states to manage.

A better design for example is this pattern

loop
display a menu
get a choice
perform the action of choice
end loop when choice = quit

such a system MAY have a sub-menu of the same pattern under one of the choices; in that case one of the choices could be 'return to previous menu' in which case you just exit the inner menu doing nothing and the outer one will re-display its options naturally via the loop. Do you understand this?
Last edited on
Hi sir @jonnin,

i've got the idea or the plot you discuss. The thing i didn't get is about the c==getchar thing but i understand the concept i just don't know what syntax i am going to use or how to apply it. But in you concept i decided to use goto. so thing i did is:

same concept with this:
loop
display a menu
get a choice
perform the action of choice
end loop when choice = quit

int main()
{
char again='y';
while(again=='Y' || again == 'y')
{
char beginning=0;
beginning:

//visual

//search
//cin>> variable

if(beginning='s') //for example in the condition is equal to 's'
{
goto beginning;
}

//switch(variable)

// case

}
//asking

return 0;
}

now, it works well for now i guess.. it quits if i press any character, but it loops back when i press Y/y. Now, my problem is solve. Thank you sir jonnin for the time and effort!
I can't remember how getchar works so just take that part as pseudocode.

goto ... works, but it is better if you avoid using it too much. Almost everything you can do with goto can be done with a better design; using it is a warning that you probably need to re-think something.

Since it works, there is no need to rewrite it this time to do the same thing another way, but in the future if you find yourself using a goto, stop and try to think through a better design.
@jonnin,

Thank you for advice and tips. Based on my research, they said that it is bad to use it but as long as using this properly, no problem. But i am going to use this for temporary or just in case. So for now, i am going to master the use of if and looping. Well, i am going to sleep now. Good luck to me, i hope i get a nice grade for this shitty project, program. Bye!
Topic archived. No new replies allowed.