• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2026 Replies
  • 331 Replies
    1928 Replies

Time of Day C++

User avatar
Duggypker
New Member
Posts: 10

Time of Day C++

Sat Mar 27, 2010 8: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;

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
Posts: 176

Re: Time of Day C++

Sat Mar 27, 2010 11:18 pm

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
Posts: 1890

Re: Time of Day C++

Sun May 30, 2010 4:45 am

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
Posts: 2585

Re: Time of Day C++

Sun May 30, 2010 3:14 pm

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
Posts: 10

Re: Time of Day C++

Thu Jun 24, 2010 8:50 pm

Haha yea when I made this tut I was able to comprehend those xD
Image
Current Project: From the Ashes RTS

Post Reply

Return to “C++ Tutorials”