2017-05-01 4 views
0

私の学校の仕事は「1月から12月の月を受け入れ、希望の月を入力するときに対応する誕生石を表示するプログラムを書くことです。月の名前を間違えたときにメッセージが表示され、別のメッセージボックスが表示されますテキストボックスに月が入力されていない場合。入力を押しても答えが出ないときにエラーメッセージを表示する方法は?

これは私が作成したコードですが、実行するたびにすべての結果は で終了します。 '(1月12日入力):3 エラー!月は与えられていない。 注文に関する何かが間違っていることを知っていますが、修正方法はわかりません。誰も助けることができますか?ありがとう!

P.S さんについてあなたはnを変更できます

#include <iostream> 
#include <sstream> 

using std::cin; 
using std::cout; 
using std::string; 

int main() 
{ 
string in = ""; 
int n = 0; 
while (true) 
{ 
    cout << "Enter a month (1-12): "; 
    getline(cin, in); 
    std::stringstream (in); 
    if (n) 
     break; 
    cout << "Error! No month given."<<std::endl; 
} 

int month; 
    cout << "Enter a month (1-12): "; 
    cin >>month; 
switch (month) 
{ 
    case 1: 
     cout <<"Corresponding Birthstone= Garnet"; 
     break; 
    case 2: 
     cout <<"Corresponding Birthstone= Amethyst"; 
     break; 
    case 3: 
     cout <<"Corresponding Birthstone= Aquamarine"; 
     break; 
    case 4: 
     cout <<"Corresponding Birthstone= Diamond"; 
     break; 
    case 5: 
     cout <<"Corresponding Birthstone= Emerald"; 
     break; 
    case 6: 
     cout <<"Corresponding Birthstone= Pearl"; 
     break; 
    case 7: 
     cout <<"Corresponding Birthstone= Ruby"; 
     break; 
    case 8: 
     cout <<"Corresponding Birthstone= Peridot"; 
     break; 
    case 9: 
     cout <<"Corresponding Birthstone= Sapphire"; 
     break; 
    case 10: 
     cout <<"Corresponding Birthstone= Opal"; 
     break; 
    case 11: 
     cout <<"Corresponding Birthstone= Topaz"; 
     break; 
    case 12: 
     cout <<"Corresponding Birthstone= Turquoise"; 
     break; 
    default: 
     cout << "Error! Month is misspelled/does not exist."; 
     break; 
} 

return 0; 
} 
+0

最初のループが終了する唯一の条件は、「n」がゼロでない場合です。 'n'はループの前にゼロであり、決して変更されません。 'cin'から読み込まれる文字列は決してチェックされません。 – Peter

答えて

0

または1等しくするためにswitch文を使用する必要がありますプログラム?限り、私はあなたがそれを変更することはできませんので、エラーメッセージが常に表示されることがわかります。

0

私はあなたの問題を修正しました。あなたを助けるかもしれません。

#include <iostream> 
#include <sstream> 

using std::cin; 
using std::cout; 
using std::string; 

int main() 
{ 
    string in = ""; 
    int n = 0; 

    while (true) 
    { 
     int month; 
     cout << "Enter a month (1-12): "; 
     cin >>month; 

     if(n) 
     { 
      cout << "Error! No month given."<<std::endl; 
      break; 
     } 

     switch (month) 
     { 
      case 1: 
       cout <<"Corresponding Birthstone= Garnet"; 
       break; 
      case 2: 
       cout <<"Corresponding Birthstone= Amethyst"; 
       break; 
      case 3: 
       cout <<"Corresponding Birthstone= Aquamarine"; 
       break; 
      case 4: 
       cout <<"Corresponding Birthstone= Diamond"; 
       break; 
      case 5: 
       cout <<"Corresponding Birthstone= Emerald"; 
       break; 
      case 6: 
       cout <<"Corresponding Birthstone= Pearl"; 
       break; 
      case 7: 
       cout <<"Corresponding Birthstone= Ruby"; 
       break; 
      case 8: 
       cout <<"Corresponding Birthstone= Peridot"; 
       break; 
      case 9: 
       cout <<"Corresponding Birthstone= Sapphire"; 
       break; 
      case 10: 
       cout <<"Corresponding Birthstone= Opal"; 
       break; 
      case 11: 
       cout <<"Corresponding Birthstone= Topaz"; 
       break; 
      case 12: 
       cout <<"Corresponding Birthstone= Turquoise"; 
       break; 
      default: 
       cout << "Error! Month is misspelled/does not exist."; 
       n = 1; 
       break; 
     } 
     cout<<std::endl; 
     return 0; 
    } 
} 
+0

これは大いに役立ちましたが、私がenterを押すと、エラーメッセージが表示されません。私は何かが間違っていると思うが、助けてくれてありがとう! –

関連する問題