Structures killing me

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
^
1
2
3
4
5
void get_info( Person& )
{
   cout << "Enter First Name: ";
   cin >> Person.first_name; // error
}

The function get_info() takes one parameter by reference. The type of the parameter is Person. What is the name of the parameter?

A function with similar issue:
1
2
3
4
void foo( double& )
{
  // how do I access the unnamed parameter here?
}

This documentation should help you..there is unfortunately a lot of issues wrong with the code, void get_info(Person&) is trying to return when it is a void, the parameter being passed is wrong, see modified function below

void get_info(Person p)
{
cout << "First Name: ";
getline (cin,p.first_name);
cout << "Last Name: ";
getline (cin,p.last_name);

}
you can finish the above function, other modification will be need in the code as well. please see reference link to help below

http://www.cplusplus.com/doc/tutorial/structures/
Last edited on
Keskiverto,

I was trying to make Person the name, does it have to be changed at get info?
Last edited on
To give you an idea...
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
#include <iostream>
#include <string>

struct Person
{
	std::string first_name;
	std::string last_name;
	int bmonth;
	int byear;
	int bdate;
};

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

void get_info(Person& p)
{
	std::cout << "Enter First Name: ";
	std::cin >> p.first_name;
	std::cout << "Enter Last Name: ";
	std::cin >> p.last_name;
	std::cout << "Enter Birth Year: ";
	std::cin >> p.byear;
	std::cout << "Enter Birth Date: ";
	std::cin >> p.bdate;
	std::cout << "Enter Birth Month: ";
	std::cin >> p.bmonth;
}

int calc_age(Date& d, Person& p)
{
	int age = d.year - p.byear;

	return age;
}

void print_info(Person& p, Date& d)
{
	std::cout <<"Name: " << p.last_name << ", "
	<< p.first_name << std::endl;

	std::cout <<"Age: " << calc_age(d,p) << std::endl;
}

int main()
{
	Person p;
	Date Date_Today{12,7,2018};
	get_info(p);
	calc_age(Date_Today,p);
	print_info(p,Date_Today);
	return 0;
}


Output:
Enter First Name: John
Enter Last Name: Doe
Enter Birth Year: 1953
Enter Birth Date: 11
Enter Birth Month: 11
Name: Doe, John
Age: 65
It gave me this:

Perhaps.cpp: In function ‘int main()’:
Perhaps.cpp:53:7: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
  Date Date_Today {12, 7, 2018};
       ^
Interesting. It works on the online compiler http://cpp.sh/6sgsl and on my compiler Visual Studio.
closed account (E0p9LyTq)
@Warzombie, the code chicofeo provided requires a C++11 or later compliant compiler. Yours is according to your warning message. Warning, not error.

So nothing to worry about. The code compiled.
I use CS50 IDE, so that might be it
closed account (E0p9LyTq)
Any decent compiler/IDE should give warnings as yours did.

Some require changing the default warning level. For instance, Visual Studio hides a lot of helpful warnings at the default setting.

Warnings are not errors. Though they could be a clue about potential problems with your program design. Such as assigning a double to an int should generate a warning.
Ya I just needed a = at Date Date_Today = {12, 7, 2018};
Thx guys
Topic archived. No new replies allowed.