Pergunta

class MovieData
{
private:
    string Title;
    string Director;
    int Year;
    int Time;

public:
    MovieData();
    void setTitle(string Title);
    void setDirector(string Director);
    void setYear(int Year);
    void setTime(int Time);
    string getTitle();
    string getDirector();
    int getYear();
    int getTime();
};

MovieData::MovieData()
{
    Title = "";
    Director = "";
    Year = 0;
    Time = 0;
}    

void MovieData::setTitle(string title)
{
    Title = title;
}    

void MovieData::setDirector(string director)
{
    Director = director;
}

void MovieData::setYear(int year)
{
    Year = year;
}

void MovieData::setTime(int time)
{
    Time = time;
}

string MovieData::getTitle()
{
    return Title;
}

string MovieData::getDirector()
{
    return Director;
}

int MovieData::getYear()
{
    return Year;
}

int MovieData::getTime()
{
    return Time;
}

void readMovieData(MovieData *, int);
void MovieDatastats(MovieData *, int);

This is my class, definition and prototype of functions here.... they are all in the same main.cpp Here's my body

int main()
{
    int amount = 0;
    cout << "How many movies did you watch last month? ";
    cin >> amount;

    MovieData *pMovie;
    pMovie = new MovieData[amount];

    cout << "Please enter the information of your Movies!" << endl;
    readMovieData(pMovie, amount);

    cout << "\nHere are the information with your Movies!" << endl;
    MovieDatastats(pMovie, amount);

}

For those who have been helping me before, I fixed it through my prototype function. I had (MovieData *pMovie, int const size) as the parameters when it was unnecessary. Really appreciated the help! I didn't need to call the same object twice, it conflicts with each other. and now, here is for my function that I have been stuck on and I don't know why...

void readMovieData(MovieData *pMovie, int const size)
{
    string title;
    string director;
    int year;
    int time;

    for(int i = 0; i < size; i++)
    {
        cout << "\nPlease enter the Title of the movie: ";
        getline(cin,title);
        pMovie[i].setTitle(title);

        cin.ignore();

        cout << "Please enter the Director name of the movie: ";
        getline(cin,director);
        pMovie[i].setDirector(director);

        cin.ignore();

        cout << "Please enter the year it was released: ";
        cin >> year;
        if(year >= 1900 && year <= 2004)
            pMovie[i].setYear(year);
        else
        {
            cout << "\nPlease enter a year between 1900 and 2004." << endl;
            year = 0;
            system("PAUSE");
            break;
        }

        cout << "Please enter the time the movie last: ";
        cin >> time;
        if(time > 0 && time < 14400)
            pMovie[i].setTime(time);
        else
        {
            time = 0;
            cout << "\nPlease enter a time between 0 and 14400 in minutes." << endl;
            system("PAUSE");
            break;
        }
    }
}

void MovieDatastats(MovieData *pMovie, int const size)
{
    float sumTime = 0.0;
    float averageTime = 0.0;
    for (int i = 0; i < size; i++)
    {
        sumTime +=pMovie[i].getTime();
    }
    averageTime = sumTime/size;

    cout << "\nYour average time of all your movie is: " << averageTime << endl;

    int oldYear = 0;
    int count = 0;
    int recentYear = 0;
    int counter = 0;

    oldYear = pMovie[0].getYear();
    recentYear = pMovie[0].getYear();

    for (int j = 1; j < size; j++)
    {
        if(pMovie[j].getYear() < oldYear)
        {
            oldYear = pMovie[j].getYear();
            count = j;
        }
        else if(pMovie[j].getYear() > recentYear)
        {
            recentYear = pMovie[j].getYear();
            counter = j;
        }
    }

    cout << "\nThe oldest Movie you have watched is called..." << endl;
    cout << "Title: " << pMovie[count].getTitle() << endl;
    cout << "Director: " << pMovie[count].getDirector() << endl;
    cout << "Release Year: " << pMovie[count].getYear() << endl;

    cout << "\nThe most newest Movie you have watched is called..." << endl;
    cout << "Title: " << pMovie[counter].getTitle() << endl;
    cout << "Director: " << pMovie[counter].getDirector() << endl;
    cout << "Release Year: " << pMovie[counter].getYear() << endl;
}

Once I run the program and enter the information, I can enter the title and director's name but once I am up to when I get the year, the program will no longer accept anything and finishes. As we can see from the output below, it skipped the Title, and Took away the first letter of the word. How can I fix this?

Here is my output.

How many movies did you watch last month? 2

Please enter the information of your Movies!

Please enter the Title of the movie: Shark Attack

Please enter the Director name of the movie: Random Name

Please enter the year it was released:

Please enter a year between 1900 and 2004.

Press any key to continue . . .

Here are the information with your Movies!

Your average time of all your movie is: 0

The oldest Movie you have watched is called...

Title:

Director: hark Attack

Release Year: 0

The most newest Movie you have watched is called...

Title:

Director: hark Attack

Release Year: 0

Process returned 0 (0x0) execution time : 19.781 s Press any key to continue

.

Foi útil?

Solução

After cin >> amount;, you still have a new line in the stream, so the new line will be assigned to the title when getline(cin,title) is called, title will be empty. Then you call ignore() with default parameters: streamsize n = 1, int_type delim = traits_type::eof() which will discards one character you input if the program has not received an EOF. You needn't call ignore() after getline().
Delete all the cin.ignore(); and add this line: cin.ignore(A_BIG_NUMBER, '\n'); above getline(cin, title);, it will work.

Outras dicas

You are breaking the for loop with the break after system("PAUSE")

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top