The do loop

A common use of a do loop is to repeat an input until a valid response is entered. Here, the loop continues unit the user enters a valid month, a number from 1 to 12. When a valid month is entered, the loop ends and the season is printed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Get month with do loop until valid, switch/case to print season
#include<iostream>
using namespace std;
int main() {
	int month;
	do {
		cout << "Enter the number of the month:";
		cin >> month;
	} while (month < 1 || month>12);
	switch (month) {
	  case 3: case 4: case 5: cout << "Spring"; break;
	  case 6: case 7: case 8: cout << "Summer"; break;
	  case 9: case 10: case 11: cout << "Fall"; break;
	  case 12: case 1: case 2: cout << "Winter"; break;
	  //no default needed because do loop forces a valid month
	} //switch month
	system("pause");
	return 0;
}
CODE

NEXT: Files