Hi,
Can any one please describe that if we can use || and && operator in C++ as following ?

if (choice == 'e' || 'E' && cur == 'd' || 'D' )
  { cout << " Enter amount in Euro " ;
     cin >> amount ;
    }

Recommended Answers

All 8 Replies

We can but not the way you showed it.

if ( (choice == 'e' || choice ==  'E') && (cur == 'd' || cur == 'D' ) ){
 //...
}

You should use a switch statement

char letter;
while (cin>>letter)
  {
    switch (letter)
      {
        case 'd':
        case 'D':
        case 'e':
        case 'E':
         //code here
        break;
       }
   }

You should use a switch statement

char letter;
while (cin>>letter)
  {
    switch (letter)
      {
        case 'd':
        case 'D':
        case 'e':
        case 'E':
         //code here
        break;
       }
   }

There are two different variables in the OP's if statement, your approach will not work.

:) yeah i omitted the "&&"

char letter;
char currency;
while (cin >> letter)
  {    
    switch (letter)
       {
         case 'd':
         case 'D':
           switch (currency)
             {
               case 'E':
               case 'e':
                 // bankruptcy
               break;
             }
        }
   }

anyway :) an if statement would suit better so ignore this

A switch case inside another switch case is strictly a no no. It goes to show that your program is not modular enough. You'll have a hard time maintaining it. Others will have a hard time understanding the logic.

:) yeah i omitted the "&&"

char letter;
char currency;
while (cin >> letter)
  {    
    switch (letter)
       {
         case 'd':
         case 'D':
           switch (currency)
             {
               case 'E':
               case 'e':
                 // bankruptcy
               break;
             }
        }
   }

anyway :) an if statement would suit better so ignore this

We can but not the way you showed it.

if ( (choice == 'e' || choice ==  'E') && (cur == 'd' || cur == 'D' ) ){
 //...
}

Thanks bro :) , that's what I was looking for .course restriction doesn't permit me to use " switch " .thanks again .

Thanks every one for your time and advices/suggestions.

Please mark the thread as solved if you feel that you have found your answer here. Happy coding!

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.