Hi
A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier. In other words, a structure, let's say St1, combines two or more kinds of variables under the same umbrella, and then whenever some other variable, x, is declared using St1 x; one needs to assign as much values to x as there are in the St1. A structure is an 'umbrella' variable declaration. Please correct me.
These points should be remembered:
1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
4: A structure can be nested within another structure.
Please review the above material and please make corrections if necessary. Thanks a lot.
And if you run or check out the output of the below given code you will notice that the second tab character "\t" produces more space than the first. Notice the distance between "1" and "2", and between "2" and "3".
Sample code:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
struct date
{
int day; int month; int year;
};
int main ()
{
date D1, D2;
{
cout << "Enter the data for Day No. 1" << endl;
cout << "Enter Day: "; cin >> D1.day;
cout << "Enter Month: "; cin >> D1.month;
cout << "Enter Year: "; cin >> D1.year;
}
cout << " " << endl;
{
cout << "Enter the data for Day No. 2" << endl;
cout << "Enter Day: "; cin >> D2.day;
cout << "Enter Month: "; cin >> D2.month;
cout << "Enter Year: "; cin >> D2.year;
}
cout << " " << endl;
cout << "Day No. 1 Detail: " << D1.day << "\t" << D1.month << "\t" << D1.year << endl;
cout << "Day No. 2 Detail: " << D2.day << "\t" << D2.month << "\t" << D2.year << endl;
system("pause");
}
Output:
Code:
Enter the data for Day No. 1
Enter Day: 1
Enter Month: 2
Enter Year: 3
Enter the data for Day No. 2
Enter Day: 4
Enter Month: 5
Enter Year: 6
Day No. 1 Detail: 1 2 3
Day No. 2 Detail: 4 5 6
Press any key to continue . . .