Hello and Asalam o Alikum. Today I am here with a new blog that will take demonstate the use of loops in structures.
In the program,I will take input from the user of his/her favourite movies and finally display the list of his/her favourite movies using structures in C++.I will use a loop to input 3 movies from the user.
So,below is the source code of this program.
In the program,I will take input from the user of his/her favourite movies and finally display the list of his/her favourite movies using structures in C++.I will use a loop to input 3 movies from the user.
So,below is the source code of this program.
Source code
- #include<iostream>
- #include<conio.h>
- #include<string>
- using namespace std;
- struct movies
- {
- string movie;
- };
- main()
- {
- movies m[3];
- for(int i=0;i<=2;i++)
- {
- cout<<"Enter the movie of your choice:";
- getline(cin,m[i].movie);
- }
- cout<<endl;
- cout<<"\t"<<"\t"<<"List of your favourite movies"<<"\t"<<"\t"<<endl<<endl;
- for(int i=0;i<=2;i++)
- {
- cout<<i+1<<"."<<m[i].movie<<endl<<endl;
- }
- }
Output
Explanation
I have used <string> header file in line # 3 because I have "string" data type in line # 7.
In line # 11, I have declared an array of size 3 and data type movies.
In line #12, a "for" loop is used for 3 iterations.
Remember that if we do not use the header file <string>, then we cannot use getline( ) function as used in line # 16.
No comments:
Post a Comment