Hello I am newbie to C++ and I am testing my first loop using a switch statement. I want the loop to run nonstop until the user quits the program.

#include <iostream>

using namespace std;

int main() 
{
	char answ;

	cout << "Yes or No(y/n)" << endl;
	cin >> answ;

	switch(answ)
	{
	
	case1:
		do 
		{
			cout << "Test" << endl;
		}

			while((answ == 'y') || (answ =='Y'));

			break;

	case0:
		do 
		{
			cout << "OK" << endl;
		}

			while((answ =='n') || (answ == 'N'));

			break;

	}

	system("PAUSE");
	return 0;
}

The code compiles and runs but when I enter "Yes" it doesn't print out "Test" like I want it to. Neither when I enter "No". Can anyone help?

This should work

#include <iostream>

using namespace std;

int main()
{
	char answ;

	cout << "Yes or No(y/n)" << endl;
	cin >> answ;

	switch(answ)
	{
	case 'y': cout << "Test" << endl;break;
	case 'Y':cout << "Test" << endl;break;
    case 'N':cout << "Ok" << endl;break;
    case 'n':cout << "Ok" << endl;break;
    default : cout<<"Wrong Order"<<endl;break;
	}

	system("PAUSE");
}

Your program is backwards. You want to
1) start a loop
2) ask for input
3) use the switch
4) continue with loop until exit command entered

Your program is backwards. You want to
1) start a loop
2) ask for input
3) use the switch
4) continue with loop until exit command entered

Please forgive me, but I don't seem to follow... Could you care to explain more?

Please forgive me, but I don't seem to follow... Could you care to explain more?

1) start a loop
2) ask for input
3) use the switch
4) continue with loop until exit command entered

>>start a loop

while( true ){...}

>>ask for input
ask for input inside while loop
>>use a switch

switch(input) { ... }

4) continue with loop until exit command entered
ask user to continue or not :

cin >> loopAgain; if(loopAgain == 'n') break; else continue;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.