0
#include <iostream> 

using namespace std ;
 int main() {
//Variables
char country;
int taxed ;
float pay ;
char uk_tax = 'a'; //40%
char us_tax ='b';//mulitple by number devide answer by 100

//Inputs
cout << "Enter your total earnings: ";
cin  >> pay ;
cout << "Are you within the UK or USA?\n ";
cout << "a) For Uk b) For USA \n  ";
cin >> country ;


switch(country)
{
case 1:
        //chosen if input is a
        if (country == uk_tax )
        taxed = 40 * pay / 100;
        cout<< "Here are your earnings after Tax £" << taxed  ;
        break;

    case 2:
        //chosen if input is b
        if (country == us_tax)
         taxed = 28 * pay / 100 ;
        cout << "Here are your earning after Tax £" << taxed ;
        break;
          } return 0 }

hello i was just wondering if anyone could help see the problem i'm having with this code. The code does not execute the switch statements.The code its self it just a tax calculator for two country (usa,uk) the user picks the country by entering 'a' or 'b' but i can't seem to get it to execute the switch statements after

8
  • 7
    Maybe because country isn't 1 or 2?
    – user253751
    Dec 3, 2015 at 20:58
  • 1
    the cases are 'a' and 'b' not 1 and 2. Dec 3, 2015 at 21:01
  • 2
    @NathanOliver: 'a' and 'b', actually. Dec 3, 2015 at 21:03
  • You are also using redundant if cases within the switch itself.
    – ssell
    Dec 3, 2015 at 21:03
  • 1
    In addition to the other problems, it looks like you're confusing the amount of tax with the amount remaining after tax.
    – Ben Voigt
    Dec 3, 2015 at 21:07

5 Answers 5

3

If your variable country is not 1 or 2 then the switch won't do anything. Also your cases would probably be better if they were:

case 'a':

case 'b':
1

Your comments indicate you want to switch on inputs 'a' and 'b', yet you did not write any code to do that. Your code switches on inputs for which country is the ASCII code 1 or 2, which you are going to struggle to type on your keyboard.

Write this:

case 'a':

and this:

case 'b':

instead.

1

country is a char. You probably want to switch over '1' and '2' or 'a' and 'b'.

1

You're asking the user to enter a or b for the countries, your switch cases should be case 'a': and case 'b': to match.

0

Since country is of type char, the switch will look try to match each case. In this instance, you want to use case 'a' and case 'b', not case 1 and case 2

Not the answer you're looking for? Browse other questions tagged or ask your own question.