Structures and Member Functions

Hey guys so I'm creating a program that lets the user enter info for an array of Cars, but for some reason when I call the member function "assignValues" from the structure Manufacturer it gives me a compiler error? What seems to be the problem? Any advice would be appreciated! :)

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
using namespace std;


enum transactionType { leased, purchased };

union payType {
	double mpay;
	int pprice;
};
enum carType {domestic, foreign};

struct Car {

	char model[20];
	int year;
	transactionType type;
	payType payment;
	char ch;
	Manufracter man;

	Car()
	{
		year = 0;
		strcpy(model, "\0");
	}
	Car(char s[], int y) {
		strcpy(model, s);
		year = y;
	}


	void assignValues() {
		
		man.assignValues();
	
		cout << "Enter car model: ";
		cin >> model;
		cout << "Enter model year: ";
		cin >> year;
		cout << "Leasing (L/l) OR Purchasing (P/p): ";
		cin >> ch;
		if (ch == 'L' || ch == 'l')
		{
			type = leased;
			cout << "Enter monthly payment: $";
			cin >> payment.mpay;
		}
		else
		{
			type = purchased;
			cout << "Enter purchase price: $";
			cin >> payment.pprice;
		}
	}
	void displayValues() {
	
		man.displayValues();
		cout << "Model: " << model << endl;
		cout << "Year: " << year << endl;
		if (type == purchased) {
			cout << "Purchased for : $" << payment.pprice << endl;
		}
		else
			cout << "Leased for: $" << payment.mpay << endl;

	}
};
struct Manufracter {
	char name[20];
	char source;
	carType region;

	Manufracter() {
		strcpy(name, "\0");
		source = '\0';
	}
	Manufracter(char x[], char n) {
		strcpy(name, x);
		source = n;
	}
	void assignValues() {
		cout << "Enter manufracter name: ";
		cin >> name;
		cout << "Enter domestic or foreign: ";
		cin >> source;
		if (source == 'd' || source == 'D')
		{
			region = domestic;
		}

		else
			region = foreign;
	}
	void displayValues() {
		cout << "Manufracter: " << name << endl;
		cout << "Source: ";
		if (source == domestic)
			cout << "Domestic" << endl;
		else
			cout << "Foreign" << endl;
	}
};

	int main() {
		int cars;
		Car* vehicle;
		cout << "Enter number of cars to input: ";
		cin >> cars;
		vehicle = new Car[cars];
		for (int x = 0; x < cars; x++)
			vehicle[x].assignValues();
		for (int y = 0; y < cars; y++)
			vehicle[y].displayValues();

		delete[] vehicle;

		return 0;
	}
Last edited on
20:2: error: 'Manufracter' does not name a type 
In constructor 'Car::Car()': 
25:21: error: 'strcpy' was not declared in this scope 
In constructor 'Car::Car(char*, int)': 
28:18: error: 'strcpy' was not declared in this scope 
In member function 'void Car::assignValues()': 
35:3: error: 'man' was not declared in this scope 
In member function 'void Car::displayValues()': 
58:3: error: 'man' was not declared in this scope 
In constructor 'Manufracter::Manufracter()': 
75:20: error: 'strcpy' was not declared in this scope 
In constructor 'Manufracter::Manufracter(char*, char)': 
79:17: error: 'strcpy' was not declared in this scope


The error on line comes from the Manufracter struct not being before the Car struct. Try to avoid spelling errors too :+) Really each struct should be in it's own files (a .hpp and .cpp for each), and they should be classes and not have public data. Provide an interface to access the data.


Is there any reason why you are using char arrays and not std::string? They are much easier to deal with.

strcpy needs an include file, google to see what it is, but prefer std::string in any case.

new & delete aren't recommended either, the best thing is to use a STL container like std::vector and not worry about memory management at all. Otherwise consider using smart pointers - Google std::unique_ptr

using namespace std; is also not recommended, just put std:: before each std thing - that's what the expert coders all do. Google that as well :+)

Good Luck !!
Last edited on
Thank you for your help I got my program to work, moving the structs fixed the errors! :)
and as for your recommendations I am not using std:: because my class hasn't gone over that and I'm barley a beginner maybe in the future when I take another C++ class I will learn to use those crazy functions you used!
Topic archived. No new replies allowed.