Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically, i think i have problem with switch case when i press 1 the case begins but when i press 2 it should but it doesn't what is the problem !!

What I have tried:

	char answer;
		int choice;
		do
		{
			system("cls");
			system("Color F ");
			cout << "\t|  --------------------------------- |" << endl;
			cout << "\t|  Press 1 for Palying Game.    |" << endl;
			cout << "\t|  Press 2 Exit				   |" << endl;
			cout << "\t|  Enter Your Choice            |" << endl;
			cout << "\t|  -----------------------------|" << endl;
			cout << "\t   \t";
			cin >> choice;    //this 
			switch (choice)
			{
			case 1:
			{
				system("cls");
				system("color E");
				t.org();
			}
			break;
			case 2:
				default:
				break;
			}
		} while (choice == 2);
	}
	return 0;
}
Posted
Updated 8-Jan-17 23:40pm
v2
Comments
Richard MacCutchan 9-Jan-17 5:04am    
case 2 does not do anything.
Ayodeji Oluwatomiwo 9-Jan-17 5:11am    
the break after the first case will terminate further checkings
Try this and post back if it works so i can repost as Solution or there is any error for correction.

char answer;
      int choice;
       do
        {
         system("cls");
            system("Color F ");
           cout << "\t|  --------------------------------- |" << endl;
           cout << "\t|  Press 1 for Palying Game.    |" << endl;
            cout << "\t|  Press 2 Exit                   |" << endl;
          cout << "\t|  Enter Your Choice            |" << endl;
            cout << "\t|  -----------------------------|" << endl;
            cout << "\t   \t";
          cin >> choice;    //this 
           switch (choice)
           {
         case 1:
           {
             system("cls");
                system("color E");
                t.org();
                break;
          }
         case 2:
         {
              //your code for exit
              break;
         }
         default:
          //your code if no case is found also optional
     } while (choice == 2);
    }
 return 0;
}

Um...that's some odd code - and badly indented! You should try to get the indentation right, it makes reading the code a lot, lot easier. And if you are going to use curly brackets around a case code, then put it round the whole code, not just a bit of it - that just looks wrong and is confusing.
char answer;
int choice;
C++
do
    {
    system("cls");
    system("Color F ");
    cout << "\t|  --------------------------------- |" << endl;
    cout << "\t|  Press 1 for Playing Game.         |" << endl;
    cout << "\t|  Press 2 Exit                      |" << endl;
    cout << "\t|  Enter Your Choice                 |" << endl;
    cout << "\t|  --------------------------------- |" << endl;
    cout << "\t   \t";
    cin >> choice;    //this 
    switch (choice)
        {
        case 1:
            {
            system("cls");
            system("color E");
            t.org();
            break;
            }
        case 2:
        default:
            break;
        }
    } while (choice == 2);
See how much clearer it is what is going on?
I'd also suggest that the code inside a case should be as short as possible - so move the "play game" code into a separate function, and just call that inside the case. It makes the function smaller and easier to read, understand, and maintain.

Now...about the actual code...
The loop will execute once, unless the user presses 2 to exit, in which case it will go round again. Probably, you want:
C++
} while (choice != 2);
That may fix your problem!
 
Share this answer
 
Comments
mayashah 9-Jan-17 5:17am    
Fixed !! Thanks Ya All !!! but some other problem encounters
mayashah 9-Jan-17 5:17am    
When i run that part of the code it don't backspace the code !!

ch = _getch();//read a character from screen
while (ch != 13)
{//character 13 is enter

if (ch == 8)
{

pasword = pasword.substr(0, (pasword.length()) - 1);
//for(int i=0;i<pasword.length();i++)
//cout << '*';
}
else
{
pasword.push_back(ch);
cout << '*';
}
ch = _getch();
}
mayashah 9-Jan-17 5:18am    
why is so i mean when i enter the password it only types the password it doesn't backspace the password if i want to delete it ??
OriginalGriff 9-Jan-17 5:30am    
Try here:
https://www.google.co.uk/search?q=getch+and+passwords&oq=getch+and+passwords&aqs=chrome..69i57.5043j0j7&sourceid=chrome&ie=UTF-8
mayashah 9-Jan-17 5:42am    
there are so many links can you follow up with the one that works with me
The problem is that your do while loop continues while choice is 2 and breaks if it is anything else.

So it should be probably:
C++
do
{
    // ...
} while (choice != 2);
 
Share this answer
 
Quote:
when i press 2 it should but it doesn't
It should but it don't what ?

You should learn to use the debugger as soon as possible, it is an incredible learning tool. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900