-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Sunday, 7 June 2015

Using loops in structures


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.

Source code

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string>
  4. using namespace std;
  5. struct movies
  6. {
  7. string movie;
  8. };
  9. main()
  10. {
  11. movies m[3];
  12. for(int i=0;i<=2;i++)
  13. {
  14. cout<<"Enter the movie of your choice:";
  15. getline(cin,m[i].movie);
  16. }
  17. cout<<endl;
  18. cout<<"\t"<<"\t"<<"List of your favourite movies"<<"\t"<<"\t"<<endl<<endl;
  19. for(int i=0;i<=2;i++)
  20. {
  21. cout<<i+1<<"."<<m[i].movie<<endl<<endl;
  22. }
  23. }

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