Getting timelimit exceed error in Basic Calculator problem using switch case approach

For this question, I am using switch case but getting this error:

runguard: warning: timelimit exceeded (wall time): aborting command
runguard: warning: command terminated with signal 15

my code:
#include
using namespace std;

int main() {
char ch;
cin.get(ch);

while((ch != 'X') || (ch != 'x'))
{
	switch(ch)
	{
		case '+' : 
		{
			int a,b;
			cin>>a>>b;
			long long ans = a+b;
			cout<<ans<<"\n";
			break;
		}
		case '-' : 
		{
			int a,b;
			cin>>a>>b;
			cout<<(a-b)<<"\n";
			break;
		}
		case '*' : 
		{
			int a,b;
			cin>>a>>b;
			long long ans = a*b;
			cout<<ans<<"\n";
			break;
		}
		case '/' : 
		{
			int a,b;
			cin>>a>>b;
			cout<<(a/b)<<"\n";
			break;
		}
		case '%' : 
		{
			int a,b;
			cin>>a>>b;
			cout<<(a%b)<<"\n";
			break;
		}
		default : 
			cout<<"Invalid operation. Try again.\n";
			break;
	}

	cin.get(ch);
}
return 0;

}

hi @swastikswarupmeher please share your code using CB IDE.
Try changing the condition to while((ch != 'X') && (ch != 'x')) instead, because ch cant be x AND ch cant be X, both these conditions must be satisfied

Hi @Ishitagambhir

Thanks! changing from while((ch != ‘X’) || (ch != ‘x’)) to while((ch != ‘X’) && (ch != ‘x’)) worked for me.

BTW can you explain that why this worked? and can you explain the error -->

runguard: warning: timelimit exceeded (wall time): aborting command
runguard: warning: command terminated with signal 15

Thanks! again

1 Like

@swastikswarupmeher this error is given in case of TLE, ie if your code is going in an infinite loop.
The terminal condition for this loop was such that it would never be false, so the while loop would never stop and go in an infinite loop.

1 Like

@swastikswarupmeher dont forget to mark your doubt as resolved!

Ok I 'll look into it

1 Like