I want to translate following c++ code into c#. But I do not know how to loop through "FILM" (like film [n] in c++), instead of each one calling separately.
Can someone also make the suggestion for better translation of this code?
C++ Code
// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} films [3];
void printmovie (movies_t movie);
int main ()
{
string mystr;
int n;
for (n=0; n<3; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
cout << "\nYou have entered these movies:\n";
for (n=0; n<3; n++)
printmovie (films[n]);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
My c# attemp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAKEGROUP {
class Program {
struct movies_t {
public string title;
public int year;
public void printmovie(movies_t movie) {
Console.Write(movie.title);
Console.Write(" (");
Console.Write(movie.year);
Console.Write(")\n");
}
}
static void Main(string[] args) {
movies_t FILM = new movies_t();
movies_t FILM1 = new movies_t();
FILM1.title = "Hero";
FILM1.year = 1990;
movies_t FILM2 = new movies_t();
FILM2.title = "Titanic";
FILM2.year = 1997;
movies_t FILM3 = new movies_t();
FILM3.title = "Mission impossible";
FILM3.year = 1996;
// How can I use for loop
// for the following code
FILM.printmovie(FILM1);
FILM.printmovie(FILM2);
FILM.printmovie(FILM3);
Console.ReadKey();
}
}
}