Structure
A structure is a collection of variables referenced under one name. Variables of different datatype are defined in the structure and can be used using a common name.
Syntax :
struct structure_name {
datatype
variable1;
datatype
variable2;
}
Write a program to take a number , string , and float
value from user using structure.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct mydata {
int number ;
char mystr[100] ;
float dec_value ;
} ;
void main() {
clrscr();
mydata collection ;
/* here, collection is the variable of structure */
/* Now, taking input to structure variables */
cout<<"\n Enter a number = " ;
cin>>collection.number ;
cout<<"\n Enter a string = " ;
gets(collection.mystr) ;
cout<<"\n Enter a decimal number = " ;
cin>>collection.dec_value ;
/* Now printing the values of structure variables */
cout<<"\n The number is = "<<collection.number ;
cout<<"\n The string is = "<<collection.mystr ;
cout<<"\n The decimal value is = "<<collection.dec_value ;
getch();
}
0 comments:
Post a Comment
Please give your valuable suggestions