c++ - Adding a do/while loop -
i need add do/while loop code using != n || != n. trouble absolutely horrid do/while loops , not quite sure should place everything. before switch itself? or before entire input module?
//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 year in range 400 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<<" not leap year\n"; break; case 2: cout<<"the year "<<year<<" leap year\n"; break; case 3: cout<<"the year "<<year<<" century year not leap year\n"; break; case 4: cout<<"the year "<<year<<" leap century year"; break; } //end switch } else cout<<"the year out of range\n"; system("pause"); return 0; }
do/while loops used whenever need loop code block 0 n number of times, meaning need run given code block @ least once until exit condition gets satisfied.
in case you'd have place do
/while
loop before leap
, year
variables declaration , close right after system("pause")
line, of main()
method. once have determine result of processing within switch block, update "another" variable (or whatever other variable you'd use in do
/while
condition).
by doing program go do
/while
loop first time , print output string, processing user input, once reaches end of loop, evaluate loop condition inside while
clause determine if code block should executed again, if so, program ask input user , process it.
//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; //declare "another" variable //input module cout << "please enter year in range 400 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 << " not leap year\n"; //update "another" variable break; case 2: cout << "the year " << year << " leap year\n"; //update "another" variable break; case 3: cout << "the year " << year << " century year not leap year\n"; //update "another" variable break; case 4: cout << "the year " << year << " leap century year"; //update "another" variable break; } //end switch } else cout << "the year out of range\n"; //update "another" variable system("pause"); } while (another != "n" || != "n"); return 0; }
Comments
Post a Comment