Reputation: 93
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.
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.
Upvotes: 0
Views: 429
Reputation: 3085
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;
};
Upvotes: 1
Reputation: 913
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. So, the compiler is asking you what start
is. 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:
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. 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;
}
Upvotes: 0
Reputation: 771
This is very basic, please read a good book on 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
Upvotes: 1