return to last loop

Can i somehow return to first loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (true) {
	//*first loop*//		while (true) {
				cout << "U svojoj sobi si, odaberi sta zelis uraditi \n(izaci, ostati)\n";
				getline(cin, odgovor);
				if (odgovor == "izaci")
					break;
				if (odgovor == "ostati")
					continue;
				else
					cout << "Odgovor nije validan! \n";
			}
		cout << "(javiti se, vratiti se u sobu, prekinuti poziv\n";
//*second loop *//			while (true) {
				getline(cin, odgovor);
				if (odgovor=="vratiti se u sobu")
				//* return to first loop*//	
			}
yes
put the first loop in a function and call it again when needed.
OR put the first loop inside an outer loop that takes you back up

you can use label
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int main(){
	int t=0, k=0;
	while(++t<10){
		label:
		cout << "t= " << t << endl;
	}	
	cout << "\n\n";
	
	while(++k<10){
		cout << "k= " << k << endl;
		if(k==3){
			goto label;
		}
	}
}
closed account (z05DSL3A)
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
#include <iostream>
#include <string>


int main ()
{
    std::string reply{};
    while (true)
    {
        // * first loop * // 
        while (true)
        {
            std::cout << "You are in your room, choose what you want to do \n (get out, stay) \n";
            std::getline (std::cin, reply);
            if (reply == "get out")
                break;
            if (reply == "stay")
                continue;
            else
                std::cout << "The answer is not valid! \n";
        }
        std::cout << "(please login, return to the room, cancel the call \n";
        // * second loop * // 
        while (true)
        {
            std::getline (std::cin, reply);
            if (reply == "go back")
                break;
        }
    }
}
Last edited on
Topic archived. No new replies allowed.