2017-11-01 7 views
-4

私たちの任務は、forループプログラムとwhileループプログラムをswitch文のプログラムから外すことです。なぜ私のforループプログラムが動作していないのかを知る必要があります。すばらしいです。なぜ私の閏年プログラムのforループが機能していないのですか?

whileループ:ループの

/* 
* File: main.cpp 
* Author: Jorge Elias 
* 
* Created on October 22, 2017, 11:53 PM 
*/ 

#include<cstdlib> 
#include<iostream> 
#include<string> 
#include<iomanip> 
using namespace std; 

/* 
* 
*/ 
int main() { 
    //Assigning the Integers 
    int month,days,year; 
    //Asking User for inputs 
    cout<<"Please enter the Year: ";cin>>year; 
    cout<<"Please enter the month (1-12): ";cin>>month; 
    //Switch statement to determine how many days there are in a month 
    switch(month) { 
     case 1: 
     case 3: 
     case 4: 
     case 5: 
     case 6: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      cout<<"31 days"<<endl; 
      break;    
    case 9: 
    case 11: 
     cout<<"30 days"<<endl; 
     break; 
while(month<0){ 
     cout<<"INVALID INPUT! Please enter month number between 1-12"<<endl; 
     break; 
     } 

//Determine if the Year inputed by the User is a Leap Year or not 
     case 2: 

     while(year>0){ 
     if(year % 100 == 0 && year % 400 == 0) 
     { 
      cout<<"This is a leap year, 29 days"<<endl; 
     } 
      else if(year % 100 !=0 && year % 4 == 0) 
      { 
       cout<<"This is a leap year, 29 days"<<endl; 
      } 
      else 
       cout<<"there are 28 days"; 
     break; 
} 
}   
return 0; 
} 

: * *ファイル:main.cppに *著者:ホルヘ・エリアス * * 2017年10月22日に作成され、午前11時53分PM */

#include<cstdlib> 
#include<iostream> 
#include<string> 
#include<iomanip> 
using namespace std; 

/* 
    * 
    */ 
int main() { 
    //Assigning the Integers 
    int month,days,year; 
    //Asking User for inputs 
    cout<<"Please enter the Year: ";cin>>year; 
    cout<<"Please enter the month (1-12): ";cin>>month; 
    //Switch statement to determine how many days there are in a month 
    for(month>1; month<12;){ 
     switch(month) { 
     case 1: 
     case 3: 
     case 4: 
     case 5: 
     case 6: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      cout<<"31 days"<<endl; 
     break; 
     case 9: 
     case 11: 
      cout<<"30 days"<<endl; 
     break; 
     for(month<1;month>12;){ 
      cout<<"INVALID INPUT! Please enter month number between 1-12"<<endl; 
      break; 
     } 
     //Determine if the Year inputed by the User is a Leap Year or not 
     case 2: 
     for(year % 100 == 0;year % 400 == 0;) 
     { 
      cout<<"This is a leap year, 29 days"<<endl; 
      for(year % 100 !=0; year % 4 == 0;) 
      { 
       cout<<"This is a leap year, 29 days"<<endl; 
      } 
      for(year >0;) 
      { 
       cout<<"there are 28 days"; 
       break; 
      } 
     } 
    }  
    } 
    return 0; 
    } 
+0

に結果の

for(i = 0; i < 10; ++i) { printf("Count is %d\n", i); } 

現在の失敗振る舞いを記述してください。 – Aaron

+0

それは私に括弧の前に一次式が必要だと言うエラーメッセージを与える – JE3

+0

とその行はどのような行にはエラーがあります – pm100

答えて

2

これらは、あなたのためのループです:

for(month>1; month<12;) 
for(year % 100 == 0;year % 400 == 0;) 
for(year >0;) 

通常のfor-loopsのように見えるものはありません。

for([init]; [test]; [mod]) { [body] } 

これらの3つの部分である:

  • [init]が初期化式である、などi = 0
  • [test]である


forループ通常は3つの別々の部分を有しますループが続くかどうかを判断する述語。例えば、i < 11

  • [mod]は、++i
  • として、ループを毎回更新するための修正式であるあなたのforループは、1つのまたは2の部品を有し、それらはイニシャライザ/述語/修飾子ありません。

    Count is 0 
    Count is 1 
    Count is 2 
    Count is 3 
    Count is 4 
    [...] 
    Count is 9 
    
    +1

    悲しいことに、最初の2つのループは「有効」で、コンパイルするのに十分です。おそらく 'for(;;){/ * infinite loop * /}'のようなステートメントが有効であることに言及する価値はあるでしょう。そして使用された(予防措置と一緒に)。 –

    関連する問題