vendredi 24 avril 2015

c++ stucts cant be accessed passed the if statement

The goal is to have the user input some movie information for two movies.
Then two functions output the movies data
The problem is when I try to use the functions outside the if statements they cant be accssed anymore.

#include <iostream>
#include <string>
using namespace std;

struct MovieData // the stuct
{
    string Title,  // required varibles
        Director;
    int yearReleased,
        runTime;


    MovieData(string TITLE, string  DIRECTOR, int YearReleased, int RunTime)  // constructors 
    {
        Title = TITLE;
        Director = DIRECTOR;
        yearReleased = YearReleased;
        runTime = RunTime;

    }
    string getTitle() // getters
    {
        return Title;
    }
    string getDirector()
    {
        return Director;
    }
    int getYear()
    {
        return yearReleased;
    }
    int getLength()
    {
        return runTime;
    }
};

void printMovie1(MovieData movieprint1) // fucntion that outputs movie 1
{
    cout << "Title of movie " << movieprint1.getTitle() << endl;
    cout << "Director is " << movieprint1.getDirector()<< endl;
    cout << "Movie release year " << movieprint1.getYear()<< endl;
    cout << "Movie length " << movieprint1.getLength() << endl;
}

void printMovie2(MovieData movieprint2) // function that outputs movie 2
{
    cout << "Title of movie " << movieprint2.getTitle() << endl;
    cout << "Director is " << movieprint2.getDirector() << endl;
    cout << "Movie release year " << movieprint2.getYear() << endl;
    cout << "Movie length " << movieprint2.getLength() << endl;
}



int main()
{
    string Title, // local varibles
        Director;
    int yearReleased,
        runTime;

    for (int i = 0; i < 2; i++) // forward loop to enter movie info
    {


        cout << "Please enter the movie " << (i + 1) << " title ";
        getline(cin, Title);

        cout << "Now the director ";
        getline(cin, Director);

        cout << "What year was " << Title << " releasted ";
        cin >> yearReleased;

        cout << "Lastly the length ";
        cin >> runTime;
        cin.ignore();

        if (i == 0)
        {
            MovieData movie1(Title, Director, yearReleased, runTime);
            // I know function works when placed here
            // printMovie1(movie1); 
        }
        else if (i == 1)
        {
          MovieData  movie2(Title, Director, yearReleased, runTime);
         // I know function works when placed here
          // printMovie2(movie2);

        }


    }

    // these functions now dont work
    enter code hereprintMovie1(movie1);
    printMovie2(movie2);



    return 0;
}

Aucun commentaire:

Enregistrer un commentaire