Page 1 of 1

Time of Day C++

Posted: Sat Mar 27, 2010 7:38 pm
by 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;

Re: Time of Day C++

Posted: Sat Mar 27, 2010 10:18 pm
by 35000vr
Thanks!!!

Re: Time of Day C++

Posted: Sun May 30, 2010 3:45 am
by zachman61
thank you

Re: Time of Day C++

Posted: Sun May 30, 2010 2:14 pm
by 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;
}

Re: Time of Day C++

Posted: Thu Jun 24, 2010 7:50 pm
by Duggypker
Haha yea when I made this tut I was able to comprehend those xD