So Im trying to make an age calculator using structures, but it seems like everything Im trying gets me a sea of errors.
Here's my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
//two structures: Date and Person
struct Person
{
string first_name;
string last_name;
int bmonth;
int byear;
int bdate;
};
struct Date
{
int month;
int date;
int year;
};
//define function to get info
void get_info(Person&)
{
cout << "Enter First Name: ";
cin >> Person.first_name;
cout << "Enter Last Name: ";
cout >> Person.last_name;
cout << "Enter Birth Year: ";
cin >> Person.byear;
cout << "Enter Birth Date: ";
cin >> Person.bdate;
cout << "Enter Birth Month: ";
cin >> Person.bmonth;
return;
}
//define function to calculate age
void calc_age(Date Person)
{
int age = 0;
age = Date.year - Person.byear;
}
//define function to output info
void print_info (Person Date)
{
cout << "Name: " << Person << endl;
cout << "Age: " << age << endl;
return;
}
int main() {
//declare a Person
char person;
int Date_today;
string first_name;
string last_name;
int age;
int bmonth = 0;
int byear = 0;
int bdate = 0;
int month = 0;
int date = 0;
int year = 0;
Date = Date_Today;
Date_today = {12, 7, 2018}; //you may use this as "today's" date
//your code goes here!
return 0;
}
|
And Im getting this
IdeaBox.cpp: In function ‘void get_info(Person&)’:
IdeaBox.cpp:28:17: error: expected primary-expression before ‘.’ token
cin >> Person.first_name;
^
IdeaBox.cpp:30:18: error: expected primary-expression before ‘.’ token
cout >> Person.last_name;
^
IdeaBox.cpp:32:17: error: expected primary-expression before ‘.’ token
cin >> Person.byear;
^
IdeaBox.cpp:34:17: error: expected primary-expression before ‘.’ token
cin >> Person.bdate;
^
IdeaBox.cpp:36:17: error: expected primary-expression before ‘.’ token
cin >> Person.bmonth;
^
IdeaBox.cpp: In function ‘void calc_age(Date)’:
IdeaBox.cpp:48:14: error: expected primary-expression before ‘.’ token
age = Date.year - Person.byear;
^
IdeaBox.cpp:48:29: error: ‘struct Date’ has no member named ‘byear’
age = Date.year - Person.byear;
^
IdeaBox.cpp: In function ‘void print_info(Person)’:
IdeaBox.cpp:54:31: error: expected primary-expression before ‘<<’ token
cout << "Name: " << Person << endl;
^
IdeaBox.cpp:55:23: error: ‘age’ was not declared in this scope
cout << "Age: " << age << endl;
^
IdeaBox.cpp: In function ‘int main()’:
IdeaBox.cpp:74:9: error: expected unqualified-id before ‘=’ token
Date = Date_Today;
^
IdeaBox.cpp:76:30: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
Date_today = {12, 7, 2018}; //you may use this as "today's" date
^
IdeaBox.cpp:76:16: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘int’ in assignment
Date_today = {12, 7, 2018}; //you may use this as "today's" date
^