2016-11-10 18 views
-4

別のコードを使用してこのコードにdo/whileループを追加する必要があります。別の!= n。問題は、私はdo/whileループには大変恐ろしいです。どこにすべてを置くべきか分かりません。それはスイッチ自体の前にあるでしょうか?または、入力モジュール全体の前に?do/whileループを追加する

//Compiler Directives 

#include<iostream> 
#include<cstdlib> 

using namespace std; 

//Constant Declarations 

const int LOWERBOUND = 400; 
const int UPPERBOUND = 2400; 

//Main Body 

int main(){ 
    //Local Identifiers 

    int year, leapCode; 

    //Input Module 

    cout<<"Please enter a year in the range 400 to 2400\t"; 
    cin>>year; 

    //Process Module 

    if ((year >= LOWERBOUND)&& (year <= UPPERBOUND)){ 
     if (year%4!=0) 
      leapCode = 1; 
     else 
      if (year%100!=0) 
       leapCode = 2; 
     else 
      if (year%400!=0) 
       leapCode = 3; 
     else 
       leapCode = 4; 
     switch(leapCode){ 
       case 1: 
       cout<<"The year "<<year<<" is not a leap year\n"; 
       break; 
      case 2: 
       cout<<"The year "<<year<<" is a leap year\n"; 
       break; 
      case 3: 
       cout<<"The year "<<year<<" is a century year but not a leap year\n"; 
       break; 
      case 4: 
       cout<<"The year "<<year<<" is a leap century year"; 
       break; 
     } //end switch 
    } 
    else 
    cout<<"The year is out of range\n"; 
    system("PAUSE"); 
    return 0; 
} 
+0

少なくとも何かを試してください – pm100

+0

SO上の人は通常あなたの宿題をしません。 –

+0

あなたが何をしなければならないのかを一度、そして多分何度もあなた自身に尋ねてください。それはあなたにヒントを与えるはずです。 –

答えて

0

DO /あなたが終了条件が満たさされるまでに少なくとも一度与えられたコードブロックを実行する必要があることを意味し、N回まで0からループのコードブロックをする必要がある場合は、必ずループが使用されていますが。あなたのケースでは

あなたは自分のleapyear変数宣言の前do/whileループを配置し、右のあなたのsystem("PAUSE")行の後にそれを閉じ、あなたのmain()方法の基本的に最も必要があるだろう。スイッチブロック内の処理結果を特定したら、「別の」変数(またはdo/whileの条件で使用する他の変数)を更新します。

これを実行すると、プログラムは最初にdo/whileループに入り、出力文字列を出力し、ユーザー入力で処理します。ループの最後に達すると、 while節内のループ状態を調べて、コードブロックが再度実行されるかどうかを判断します。そうであれば、プログラムはユーザーに別の入力を要求して処理します。

//Compiler Directives 

#include <iostream> 
#include <cstdlib> 

using namespace std; 

//Constant Declarations 

const int LOWERBOUND = 400; 
const int UPPERBOUND = 2400; 

//Main Body 

int main() 
{ 
    //Local Identifiers 

    do { 
     int year, leapCode; 
     //declare "another" variable 

     //Input Module 
     cout << "Please enter a year in the range 400 to 2400\t"; 
     cin >> year; 

     //Process Module 
     if ((year >= LOWERBOUND) && (year <= UPPERBOUND)) { 
      if (year % 4 != 0) 
       leapCode = 1; 
      else 
       if (year % 100 != 0) 
        leapCode = 2; 
       else 
        if (year % 400 != 0) 
         leapCode = 3; 
        else 
         leapCode = 4; 
      switch (leapCode) { 
       case 1: 
        cout << "The year " << year << " is not a leap year\n"; 
        //update "another" variable 
        break; 
       case 2: 
        cout << "The year " << year << " is a leap year\n"; 
        //update "another" variable 
        break; 
       case 3: 
        cout << "The year " << year << " is a century year but not a leap year\n"; 
        //update "another" variable 
        break; 
       case 4: 
        cout << "The year " << year << " is a leap century year"; 
        //update "another" variable 
        break; 
      } //end switch 
     } 
     else 
      cout << "The year is out of range\n"; 
      //update "another" variable 

     system("PAUSE"); 
    } while (another != "N" || another != "n"); 

    return 0; 
} 
関連する問題