【问题标题】:How to ask the user to enter input in c++ in my class's and objects如何要求用户在我的班级和对象中输入 C++ 输入
【发布时间】:2021-12-28 08:46:32
【问题描述】:
#include<iostream>
using namespace std;

class Date {
    int Day;
    int Month;
    int Year;

public:
    void setDay(int day) {
        if(day>=1 && day<=31)
        Day = day;
    }
    int getDay() {
        return Day;
    }
    void setMonth(int month) {
        if(month <= 12)
        Month = month;
    }
    int getMonth() {
        return Month;
    }
    void setYear(int year) {
        if(year >=1111)
        Year = year;
    }
    int getYear() {
        return Year;
    }

    void DisplayDate()
    {
        cout << "Days:  " << Day << " \n";
        cout << "Month: " << Month << " \n";
        cout << "Year: " << Year << " \n";
    }
    Date(int day, int month, int year) {
        Day = day;
        Month = month;
        Year = year;
    }
    void Birthday()
        {
            cout << "Your Birthday is " << Day << " / " << Month << " / " << Year << "\n";
        }
    

};

int main()
{
    
    Date date1 = Date(10, 10, 2000);
    date1.DisplayDate();
    date1.Birthday();

}

此代码用于输入日期年份和月份。如何添加 cin 功能以要求用户输入他的数据? 我尝试在我的主函数中添加cin,但它给我带来了很多错误。

【问题讨论】:

  • 你试过什么?你得到了什么错误?从技术上讲,它应该像写 cin &gt;&gt; nameOfTheVariable 一样简单。
  • 您可以编写一个 setter 函数来执行此操作。一个单独的函数,执行所有这些 cin 操作,然后将这些值分配给文字。

标签: c++ class object


【解决方案1】:

您可以简单地编写一个构造函数,从任何std::istream 对象(如std::cin)获取您需要的数据。

只需将构造函数添加到您的类中:

class Date {
    int Day;
    int Month;
    int Year;

public:

    Date( std::istream& is )
    {
        std::cout << "Please enter day" << std::endl;
        is >> Day;
        std::cout << "Please enter Month" << std::endl;
        is >> Month;
        std::cout << "Please enter Year" << std::endl;
        is >> Year;
    }   
... REST OF YOUR CLASS ...
};
int main()
{

    //Date date1 = Date(10, 10, 2000);

    // Construct your object with the constructor and the `std::cin` object
    Date date1{ std::cin };
    date1.DisplayDate();
    date1.Birthday();

}

现在您还可以从文件或任何其他std::istream 构造您的类。如果您还喜欢从文件中读取,则为请求的参数输出到std::cout 是没有意义的:-)

【讨论】:

  • 公开日期中“std::istream& is”的目的是什么
  • is 是对任何类型的 std::istream 的引用。因此,您也可以将std::cin 作为文件传递,例如。 std::ifstream.
【解决方案2】:

我假设你做了类似的事情 cin &gt;&gt; date1.Day &gt;&gt; date2.Month &gt;&gt; date3.Year ,这显然不起作用,因为您的变量是私有的。将它们公开或输入到其他变量中并使用方法设置它们的值。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案3】:

不知道你遇到了什么问题,你能列出你遇到的错误吗?你应该可以写int day; cin &gt;&gt; day;

【讨论】:

    【解决方案4】:

    无法理解您的问题是什么。您只需要添加cin 对象来要求用户输入他们的数据,如下所示:

    int main()
    {
        int day=0, month=0, year=0;
        cout << "plearse enter day month year" << endl;
        cin >> day >> month >> year;
        Date date1 = Date(day, month, year);
        date1.DisplayDate();
        date1.Birthday();
    
    }
    

    【讨论】:

    • 尝试使用此方法,但由于多重初始化,我不断收到语法错误。
    • 可能是编译器版本问题,尝试初始化变量,int day = 0,month = 0, year = 0;
    • 简单说明一下,cin 不是函数,它是标准库提供的对象。请参阅文档:en.cppreference.com/w/cpp/io/cin。欢迎使用 StackOverflow!
    • 你说得对,cin是一个宾语,因为我的英语很差,所以表达的很含糊,:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2020-01-30
    • 2020-05-24
    • 2021-12-31
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多