2015-12-03 261 views
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 } 

你好我只是想知道是否有人可以幫助看到我與這段代碼有問題。代碼不執行開關語句。代碼它自己它只是兩個國家(美國,英國)的稅計算器用戶通過輸入'a'或'b'選擇國家,但我似乎無法得到它執行switch語句後執行switch語句?

+7

也許是因爲'country'不是1或2? – immibis

+1

案件是「a」和「b」不是「1」和「2」。 – NathanOliver

+2

@NathanOliver:實際上是''a''和''b''。 –

回答

3

如果您的變量country不是1或2,那麼switch將不會執行任何操作。 此外,您的情況可能會更好,如果他們是:

case 'a':

case 'b': 
1

您的意見指出要對投入'a''b'切換,但你沒有寫任何代碼來做到這一點。您的代碼會打開country是ASCII代碼12的輸入,您將很難在鍵盤上輸入。

這樣寫:

case 'a': 

這:

case 'b': 

代替。

1

countrychar。您可能需要切換'1''2''a''b'

1

您要求用戶輸入ab爲國家,您的交換機應該是case 'a':case 'b':匹配。

0

由於countrychar類型,因此交換機會嘗試匹配每種情況。在這種情況下,您想要使用case 'a'case 'b',而不是case 1case 2