Write a C/C++ program for displaying today's date by using structure.
79Thnigs you need
- computer
- programming basic
- compiler
It's a simple program to start learning structure in C/C++. This program will display today's date. Suppose today's date is 30.10.2008. So, the output of the program will be 30.10.2008.
This program is done into two ways. First program is an static initialization. In the second part (name as "Extra") the program is done by collecting values from the user.
Code of the program
#include<iostream.h>
// declaration of structure
struct date{
int day;
int month;
int year;
};
int main(){
// object definition
date today;
// initialization of members today.day = 30; today.month = 10; today.year = 2008;
// printing
cout << "A program for displaying today's information";
cout << endl << endl;
cout << "Today's date: ";
cout << today.day << ".";
cout << today.month << ".";
cout << today.year << endl;
cin.get();
return 0;
}
Output of the program
Discussion of the above code segment
Here 3 data (day, month and year) can be grouped together to form a structure. This group is named as "date". Definition of the "date" structure is done by the following lines:
// declaration of structure
struct date{
int day;
int month;
int year;
};
Day, month and year - all of them are the members of this structure.
In the "main()" function, the structure object is defined. It looks:
date today;
Later, the members are initialized. Initialization looks:
today.day = 30; today.month = 10; today.year = 2008;
At last, the information is displayed by using the "cout". There is "return 0" at the end of the program because here main is integer type.
Extra
Members in an structure can be initialized without statically. The above program is implemented in anther way. Here, all the values are initialized by the user. No static initialization. The user will provide all the information and then it will display the entered information.
Modified version of the above program
#include<iostream.h>
// declearation of structure
struct date{
int day;
int month;
int year;
};
int main(){
// object defination
date today;
cout << "A program for displaying today's information"; cout << endl;
// collecting values from the user cout << "Day :"; cin >> today.day; cout << "Month:"; cin >> today.month; cout << "Year :"; cin >> today.year;
cin.get(); return 0; }
Output of the above code segment
Related readings on "Structue in C/C++"
Put your opinion
Is this simple program easy to understand?
See results without votingMy recent activities
- Fresh and unique contents for Search Engine Optimization: The secret talks of SEO and generating site contents
Millions of websites with billions of webpages are available in the cyberspace. But only a few of the pages are getting real popularity everyday. Google, Yahoo and Bing are the major search engine.... - 2 weeks ago
- Free malware protection with Portable Anti Trojan Elite: The best malware blocker and malware remover for your compter
Recently malware is spreading very rapidly. Malware is a type of software which is designed specifically to damage or disrupt a system, such as a trojan horse or a spyware or a keylogger. If your PC... - 2 weeks ago
|
Java for C/C++ Programmers
Price: $67.23
List Price: $39.95 |
|
Programming in Objective-C 2.0 (2nd Edition)
Price: $25.00
List Price: $44.99 |
|
UML and C++: A Practical Guide to Object-Oriented Development (2nd Edition)
Price: $9.00
List Price: $63.00 |
|
Learn Objective-C for Java Developers
Price: $21.95
List Price: $39.99 |
PrintShare it! — Rate it: up down flag this hub











quicksand says:
8 months ago
You've got some great hubs. Very useful too. Thanks. :)