A
arnuld
problem: define a /struct Date/ to keep track of dates. provide
functions that read Dates from input, write Dates to output &
initialize a date with date.
solution: i thought of a /vector/ of /Date structures/. "Date" struct
carries 3 things: month, date & year. i wrote the following code & of
course it gives an error:
#include <iostream>
#include <string>
#include <vector>
struct Date {
std::string month;
int date;
int year;
};
std::vector<Date> vd;
void create_date(std::string m, int d, int y) {
Date da = {m, d, y};
vd.push_back(da);
}
void date_input(std::vector<Date> vdate) {
std::string month;
int date, year;
std::cout << "Enter Date (MM DAY YEAR): ";
std::cin >> month;
std::cin >> date;
std::cin >> year;
create_date(month, date, year);
}
void print_date(Date* vdate) {
std::cout << vdate->month << '\n'
<< vdate->date << '\n'
<< vdate->year << '\n';
}
void print_all_dates(std::vector<Date> vdate) {
for(std::vector<Date>::iterator iter=vdate.begin();
iter != vdate.end();
++iter)
{
print_date(*iter);
}
}
int main() {
date_input(vd);
}
--------- error -----------------------------------
05_ex-13.cpp: In function 'void print_all_dates(std::vector<Date,
std::allocator<Date> >)':
05_ex-13.cpp:44: error: cannot convert 'Date' to 'Date*' for
argument '1' to 'void print_date(Date*)'
functions that read Dates from input, write Dates to output &
initialize a date with date.
solution: i thought of a /vector/ of /Date structures/. "Date" struct
carries 3 things: month, date & year. i wrote the following code & of
course it gives an error:
#include <iostream>
#include <string>
#include <vector>
struct Date {
std::string month;
int date;
int year;
};
std::vector<Date> vd;
void create_date(std::string m, int d, int y) {
Date da = {m, d, y};
vd.push_back(da);
}
void date_input(std::vector<Date> vdate) {
std::string month;
int date, year;
std::cout << "Enter Date (MM DAY YEAR): ";
std::cin >> month;
std::cin >> date;
std::cin >> year;
create_date(month, date, year);
}
void print_date(Date* vdate) {
std::cout << vdate->month << '\n'
<< vdate->date << '\n'
<< vdate->year << '\n';
}
void print_all_dates(std::vector<Date> vdate) {
for(std::vector<Date>::iterator iter=vdate.begin();
iter != vdate.end();
++iter)
{
print_date(*iter);
}
}
int main() {
date_input(vd);
}
--------- error -----------------------------------
05_ex-13.cpp: In function 'void print_all_dates(std::vector<Date,
std::allocator<Date> >)':
05_ex-13.cpp:44: error: cannot convert 'Date' to 'Date*' for
argument '1' to 'void print_date(Date*)'