2017-06-09 13 views
0

C++でcase文を使用し、その式を持つことは可能ですか?私はif文を使うべきですか?スイッチ式?どの言語でも可能ですか?

#include <iostream> 

using namespace std; 

int main() 
{ 

    double balance; 
    int amount; 
    const double TWENTY_AMOUNT = .10; 
    const double THIRTY_AMOUNT = .08; 
    const double FOURTY_AMOUNT = .06; 
    const double SIXTY_AMOUNT = .04; 

    cout << "What is your beginning balance?" << endl; 
    cin >> balance; 

    cout << "How many checks did you write?" << endl; 
    cin >> amount; 

    if (balance < 400) 
    { 
     balance = balance - 15; 
     cout << "Your account went below $400, $15 taken off" << endl; 
    } 


    switch (amount) 
    { 
     case (<= 20): 
     balance = balance + (TWENTY_AMOUNT * amount); 
     break; 

     case (> 20 && <= 39): 
     balance = balance + (THIRTY_AMOUNT * amount); 
     break; 
     case (> 40 && <= 59): 
     balance = balance + (FOURTY_AMOUNT * amount); 
     break; 
     case (> 60): 
     balance = balance + (SIXTY_AMOUNT * amount); 
     break; 
     case (< 0): 
     cout << "Account overdrawn!"; 
     default: 
     cout << "Incorrect amount!"; 
    } 

    cout << "There is an additional $10 fee this month" << endl; 
    balance = balance - 10; 

    cout << "You currently have: $" << balance << "This month " << endl; 

    return 0; 
} 

EDIT:表現:私はorginallyた場合の金額の変数を持っていた私の主な質問は私が一緒に仕事しようとしています次のcase文として示されています。それはあまりにもエラーを投げた。

ありがとうございます!

+0

https://stackoverflow.com/questions/2094776/is-it-possible-to-do-the-following-in-a-switch-statement-cの複製のようです – Ben

答えて

0

短い答えはいいえです。case文は、整数と文字の大文字小文字の区別を純粋にチェックするためのものです(C++で)。この種の条件付きフローを探しているなら、if文を使うべきです。

関連する問題