Problem with struct

Can anyone tell me why this code produces an error, please?

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
#include "iostream"
#include "string"
int n;
using namespace std;


struct info_car
{
	string make_model;
	int year;
	double a_price;
	double d_price;
};


int main()
{
	using namespace std;
	cout << "How many info cars will you add?\nNumber: ";
	cin >> n;
	info_car *info = new info_car[n];
	int i = 0;

	for (i = 0; i < n; ++i)
	{
		cout << "|____________________Car info #" << i + 1 << "____________________|\n";
		cout << "Enter the make and model of automobile: ";
		cin >> info[i]->make_model;
		cout << "Enter the model year: ";
		cin >> info[i]->year;
		cout << "Enter the original asking price: ";
		cin >> info[i]->a_price;
		info[i]->d_price = (0.913)*(info[i]->a_price);
		cout << "___________________________________________________________________|\n\n";
	}

	for (i; i >= 0; --i)
	{
		cout << info[i]->make_model << endl;
		cout << info[i]->year << endl;
		cout << info[i]->a_price << endl;
		cout << info[i]->d_price << endl;
	}

	delete[] info;
	return 0;

}


Here is the error
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
Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	32	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	34	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	36	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	37	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	37	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	43	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	44	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	45	
Error (active)	E0044	expression must have pointer type	fstream	c:\Users\Admin\Documents\Visual Studio 2017\Projects\C++ Experiments\fstream\fstream\fstream.cpp	46	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	32	
Error	C2232	'->info_car::make_model': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	32	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	34	
Error	C2232	'->info_car::year': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	34	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	36	
Error	C2232	'->info_car::a_price': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	36	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	37	
Error	C2232	'->info_car::d_price': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	37	
Error	C2232	'->info_car::a_price': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	37	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	43	
Error	C2232	'->info_car::make_model': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	43	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	44	
Error	C2232	'->info_car::year': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	44	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	45	
Error	C2232	'->info_car::a_price': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	45	
Error	C2819	type 'info_car' does not have an overloaded member 'operator ->'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	46	
Error	C2232	'->info_car::d_price': left operand has 'struct' type, use '.'	fstream	c:\users\admin\documents\visual studio 2017\projects\c++ experiments\fstream\fstream\fstream.cpp	46	

Last edited on
Top tip. Tell people the error, rather than making them guess. That said:



cin >> info[i]->make_model;

info[i] isn't a pointer to a info_car; it is an info_car.

Try cin >> info[i].make_model;


In the second loop, i begins too high so you're trying to read an element from the array that doesn't exist. Put:
cout << "Trying to read from info[" << i << "]" << endl;
at line 39.
Thanks, I am sorry, the next time i will tell more about the error.
Topic archived. No new replies allowed.