简体   繁体   English

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

[英]Printing Structure inputs from class member function

I would like to know how to input/initialize a start_date and end_date (which comes from a structure Date that has integers of month day and year from the function `initializeDate. Once I am able to initialize I assume I will be able to use the same logic in the printout member function. 我想知道如何输入/初始化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);
}

edit: the error that I am getting is 'start' was not declared in this scope. 编辑:我得到的错误是'start'未在此范围内声明。

This is very basic, please read a good book on C++. 这是非常基础的,请阅读一本关于C ++的好书。 Posting below because you have put in an effort :) 发布在下面,因为你付出了努力:)

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

** Just Edited the code as per your change in question **只是根据您的更改编辑代码

It looks like you keep updating the example code. 看起来你不断更新示例代码。 Based on the current revision, I think this is what you want: 根据目前的修订版,我认为这就是你想要的:

#include <iostream>
using namespace std;

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


void initializeDate(Date &date)
{
    cout<<"Please enter the month"<<endl;
    cin>>date.month;
    cout<<"Please enter the day"<<endl;
    cin>>date.day;
    cout<<"Please enter the year"<<endl;
    cin>>date.year;
}

int main()
{
  Date start, end;
  initializeDate(start);
  initializeDate(end);
  cout << start.year << "/" << start.month << "/" << start.day << endl;
  cout << end.year << "/"   << end.month   << "/" << end.day << endl;
  return 0;
};

Ok, there are a couple of problems here, that you should target. 好的,这里有几个问题,你应该瞄准。 First, to fix your code, the error is very simple: there isn't anywhere in your code in which a variable named start was declared/defined. 首先,为了修复代码,错误非常简单:代码中没有任何名称为start的变量被声明/定义。 So, the compiler is asking you what start is. 所以,编译器会问你是什么start You are trying, I assume, to initialize the values of the members of d, that you passed in the function initializeDate , and all you have to do is just replace every occurence of the word start with d , and you'll get: 我假设你正在尝试初始化你在函数initializeDate传递的d成员的值,而你所要做的就是用d替换单词start每个出现,你将得到:

void initializeDate(Date &d)
{
    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);
}

Now, although this works, it's not the best way to initialize the date. 现在,虽然这有效,但它不是初始化日期的最佳方式。 Since Date is a struct , you can initialize its members using a constructor method. 由于Datestruct ,因此可以使用构造函数方法初始化其成员。 This is achieved by writing it like this: 这可以通过这样编写来实现:

struct Date{
    int day, month, year;
    Date(int, int, int);
    };

Date:: Date(int day, int month, int year){
    this->day = day;
    this->month = month;
    this->year = year;
    }

int main(){
    Date today(11, 3, 2013);
    cout << "today, the date is " << today.day << "-" << today.month << "-" << today.year << endl; 
    return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM