Write a C/C++ program for displaying today's date by using structure.
80Thnigs 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
- rundll32.exe - Application Error – The memory could not be "read". Click on OK to terminate the program.
Windows operating system is corrupted due to several virus infections. One such type of common infection is rundll32.exe - Application Error. Due to virus infection, rundll32.exe or such type of... - 4 days ago
- PPV or PPC?: "Pay Per View or Pay Per Click?" - Which One is Better?
Pay per view (PPV) and pay per click (PPC) are two types of marketing advertising solutions which is used by the advertisers to reach targeted group of visitors. A basic introduction will be... - 4 days ago
|
UML and C++: A Practical Guide to Object-Oriented Development (2nd Edition)
Price: $10.45
List Price: $63.00 |
|
Learn Objective–C on the Mac (Learn Series)
Price: $18.98
List Price: $39.99 |
|
Programming in Objective-C 2.0 (2nd Edition)
Price: $24.97
List Price: $44.99 |
|
OOP with Microsoft Visual Basic .NET and Microsoft Visual C#(TM) .NET Step by Step (Step By Step (Microsoft))
Price: $7.50
List Price: $39.99 |
PrintShare it! — Rate it: up down flag this hub











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