Time of Day C++

All tutorials created in C++ to be posted in here.
5 posts Page 1 of 1
Contributors
User avatar
Duggypker
New Member
New Member
Posts: 10
Joined: Tue Mar 23, 2010 3:10 am

Time of Day C++
Duggypker
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;
Code: Select all
#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;

	}


}
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

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;
Image
Current Project: From the Ashes RTS
User avatar
35000vr
Top Poster
Top Poster
Posts: 176
Joined: Sat Mar 06, 2010 5:09 pm

Re: Time of Day C++
35000vr
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:ß┬ƒ○║■ã¿┼↑
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

Re: Time of Day C++
zachman61
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 :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Time of Day C++
mandai
I thought this would actually get the time of day from the current time.

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;
}
User avatar
Duggypker
New Member
New Member
Posts: 10
Joined: Tue Mar 23, 2010 3:10 am

Re: Time of Day C++
Duggypker
Haha yea when I made this tut I was able to comprehend those xD
Image
Current Project: From the Ashes RTS
5 posts Page 1 of 1
Return to “C++ Tutorials”