Time of Day C++
All tutorials created in C++ to be posted in here.
5 posts
Page 1 of 1
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;
Thanks!!!
‼ <----- Copy it,it is together and if you backspace it it will both erase
☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"<-----Some Cool symbols.
♂<-----Boy Symbol
²ƽ<--------Mini 2 and 5!
ð<----Not sure what it is.
☺☻<-----Smiles
♪♫<----Music Notes
Others:ß┬ƒ○║■ã¿┼↑
☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"<-----Some Cool symbols.
♂<-----Boy Symbol

²ƽ<--------Mini 2 and 5!
ð<----Not sure what it is.
☺☻<-----Smiles
♪♫<----Music Notes
Others:ß┬ƒ○║■ã¿┼↑
thank you
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that 

I thought this would actually get the time of day from the current time.
Like
Like
Code: Select all
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using std::cout;
int _tmain(int argc, _TCHAR* argv[])
{
SYSTEMTIME st;
GetLocalTime(&st);
cout << "Hour is: " << st.wHour << "\r\n";
if (st.wHour > 0 && st.wHour < 12)
{
cout << "Good morning.";
}
else if (st.wHour > 12 && st.wHour < 18)
{
cout << "Good afternoon.";
}
else
{
cout << "Good evening.";
}
return 0;
}
Haha yea when I made this tut I was able to comprehend those xD
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023