Time of Day C++
Posted: Sat Mar 27, 2010 7:38 pm
Hey me again! Yes another C++ quick snip and tutorial. Today we are looking at switch cases wahooo;
With his program we are going to be telling our program what time of day it is. (Afternoon. morning, evening) cooll;
Let's get started shall we? As usual I will present a script and then explain it afterwards! wahooo;
you should also remember cout is our output and cin is the input functions.
Now integer's are used for WHOLE numbers only, so we will not be typing in 'morning' or anything instead we use the numbers to the left of the line 1, 2, and 3. which tells the script the integer, and switches it to the correct corresponding case. So for example if I were to type 1, the script would go to case 1. And so fourth.
As for the 'break' this is used to leave or break from the function as well if we did not have the break then the script would just simply continue on to the next cases. default is used in case they enter an invalid number such as 5 or 4 or a letter.
This completes our switch case tutorial. Check out my next tutorial when I go over the If-then-else functions. ;) Thanks for reading!
-Duggypker cooll;
With his program we are going to be telling our program what time of day it is. (Afternoon. morning, evening) cooll;
Let's get started shall we? As usual I will present a script and then explain it afterwards! wahooo;
Code: Select all
As you remember from our last tutorial int a; is integer a, which is a new open data space that we can either set or fill. which we have using cin #include <iostream>
using namespace std;
int main(){
int a;
cout << "What time of day is it?\n";
cout << "1) Morning\n";
cout << "2) Afternoon\n";
cout << "3) Evening\n";
cout << "Enter a choice: ";
cin >> a;
switch (a){
case 1:
cout << "Good Morning!";
break;
case 2:
cout << "Good Afternoon!";
break;
case 3:
cout << "Good Evening!";
break;
default:
cout << "Not a valid entry...";
break;
}
}
you should also remember cout is our output and cin is the input functions.
Now integer's are used for WHOLE numbers only, so we will not be typing in 'morning' or anything instead we use the numbers to the left of the line 1, 2, and 3. which tells the script the integer, and switches it to the correct corresponding case. So for example if I were to type 1, the script would go to case 1. And so fourth.
As for the 'break' this is used to leave or break from the function as well if we did not have the break then the script would just simply continue on to the next cases. default is used in case they enter an invalid number such as 5 or 4 or a letter.
This completes our switch case tutorial. Check out my next tutorial when I go over the If-then-else functions. ;) Thanks for reading!
-Duggypker cooll;