getline只需要一条信息 [英] getline takes only a piece of the information

查看:106
本文介绍了getline只需要一条信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);

这是我的类,这里的函数的定义和原型....他们都在同一个main.cpp
这是我的身体

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);

}

对于那些一直帮助我的人,通过我的原型功能。我有(MovieData * pMovie,int const size)作为参数,当它是不必要的。真正感谢帮助!
我不需要调用同一个对象两次,它们相互冲突。
现在,这里是我的功能,我一直坚持,我不知道为什么...

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?

这是我的输出。


你上个月看电影了吗? 2

How many movies did you watch last month? 2

请输入电影的信息!

请输入电影的标题:Shark Attack

Please enter the Title of the movie: Shark Attack

请输入电影的导演名称:随机名称

Please enter the Director name of the movie: Random Name

请输入发行年份:

请输入1900到2004年之间的年份。

Please enter a year between 1900 and 2004.

按任意键继续。 。

这是您的电影的信息!

您所有电影的平均时间是:0

Your average time of all your movie is: 0

您观看的最古老的电影是...

The oldest Movie you have watched is called...

标题:

导演:hark Attack

Director: hark Attack

发行年份:0

已观看的视频...

标题:

导演:hark Attack

Director: hark Attack

发布年份:0

处理返回0(0x0)执行时间:19.781 s按任意键到
continue

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

推荐答案

$ c> cin>> amount; ,您仍然在流中有一个新行,因此当时,新行将被分配给 title 调用getline(cin,title) title 将为空。然后用默认参数调用 ignore() streamsize n = 1,int_type delim = traits_type :: eof()如果程序没有收到EOF,它将丢弃您输入的一个字符。您不必在 getline()后调用 ignore()

删除所有 cin.ignore(); 并添加以下行: cin.ignore(A_BIG_NUMBER,'\\\
'); getline(cin,title); ,它会工作。

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.

这篇关于getline只需要一条信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆