c++ - 从类成员函数打印结构输入

标签 c++

我想知道如何输入/初始化一个 start_dateend_date(它来自一个结构 Date,其整数为 month dayyear 来自函数 `initializeDate。一旦我能够初始化,我假设我将能够在打印输出中使用相同的逻辑成员函数。

struct Date
{
    int month;
    int day;
    int year;
};


void initializeDate(Date &d)
{
    cout<<"Please enter the month"<<endl;
    cin>>start.month;
    cout<<"Please enter the day"<<endl;
    cin>>start.day;
    cout<<"Please enter the year"<<endl;
    cin>>start.year;
    string dummy;
    getline(cin, dummy);
}

编辑:我收到的错误是“开始”未在此范围内声明。

最佳答案

这是非常基础的,请阅读一本关于 C++ 的好书。在下面发帖是因为您付出了努力:)

void Information::initializeDate(Date &d)    //comes from the Information class.
{
    // Commented as part of question change!  
    // Date d;     // Guessing that the structure is the private member of the class.
    cout<<"Please enter the month"<<endl;
    cin>>d.month;
    cout<<"Please enter the day"<<endl;
    cin>>d.day;
    cout<<"Please enter the year"<<endl;
    cin>>d.year;
    string dummy;
    getline(cin, dummy);
}

** 刚刚根据您的问题修改了代码

关于c++ - 从类成员函数打印结构输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331103/

相关文章:

c++ - 如何使用 MPI 在多个独立启动的程序之间传输数据

c++ - 现在和现在之间的差异时间会产生 1 小时的差异 (C++)

c++ - 对 google::protobuf::internal::empty_string_[abi:cxx11] 的 undefined reference

c++ - 二进制表达式 ('double(*)(double' 和 'double' 的无效操作数)

c++ - 视觉 C++ 2012 : why does priority_queue require overloading of assignment operator?

c++ - std::ifstream/FILE* 可以改变大小吗?

c++ - 在文件中插入新行会删除接下来的两个字符 C++

c++ - MVC std::shared_ptr 中的循环依赖

c++ - C/C++ - 任何好的网络服务器库?

c++ - C++中是否存在真正的静态多态性?