creating multiple objects based on input

I'm reading data from a file that instructs how many objects of a class I need to create.

What I attempted to do is create a dynamic array of objects but the issue becomes that the array becomes inaccessible outside of its scope
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
#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>

#include "Product.h"
#include "MachineA.h"

using namespace std;
bool passCode();

void main()
{
	int attempts = 3;
	while (attempts > 0) {
		bool pass = passCode();
		if (pass == true)
		{
			attempts = 0;
			fstream indata("machines.txt");
			Product product;//reads in products.txt

			if (indata.is_open()) {
				string type;
				const string typeA = "100A";
				const string typeC = "100C";

				while (!indata.eof()) {
					int numOfMachines;
					indata >> type;
					indata >> numOfMachines;

					if (type.compare(typeA) == 0) {//machine a
						M100A *machineA = new M100A[numOfMachines]; //make n amount of machine A objects based on file

						for (int i = 0; i < numOfMachines; i++) {//fill in data

							int q, d, n;
							int numofItems;
							string display;
							int id, amount;

							indata >> q >> d >> n;
							indata >> numofItems;

							machineA[i].setChange(q, d, n);
							machineA[i].setNumofProducts(numofItems);
							for (int j = 0; j < numofItems; j++) {
								indata >> display >> id >> amount;
								machineA[i].setItems(display, id, amount);
							}
						}
					}
					else if (type.compare(typeC) == 0) {//machine c

					}
					else {//if not 100a or 100c
						string skip; //to skip everything that isnt 100a or 100c
						int numbertoSkip;
						for (int i = 0; i < numOfMachinesA; i++) {
							getline(indata, skip);
							indata >> numbertoSkip;
							for (int j = 0; j < numbertoSkip; j++) {
								getline(indata, skip);
							}
						}//nested fors

					}
				}//else
			}
			cout << "Available Machines: ";
			for (int i = 0; i < numOfMachinesA; i++) {
				bool check = machineA[i].checkMachine(i);
				if (check == true) {
					cout << "100A" + i;
				}
				cout << "Select a machine: ";
				string response;
				cin >> response;
			}
			else {
				cout << "File not found\n";
			}
		}
		else {
			attempts--;
			cout << attempts << " attempts remaining...\n";
		}
	}
	system("pause");
}

bool passCode()
{
	const string password = "jk";
	string code;

	cout << "Please enter startup code --> ";
	cin >> code;
	if (code.compare(password) == 0)
	{
		cout << "Initialize machines. Please wait ..." << endl;
		cout << "Machine is ready." << endl;
		return true;
	}
	else
	{
		return false;
	}
}


notice that machineA's scope is limited to the if block, which is necessary to verify that the data from the file calls for that object to be created.

I was thinking of using a vector, but I don't know how (or if it is possible) to create multiple objects through a loop.
Last edited on
Vectors would probably be easier especially since they can increase and decrease in size if needed. I noticed the if block as well and its from my understanding that it will only stay in that scope (until the closing brace).
The variable machineA stores the address of the memory you've allocated. Since you declare that variable inside your if block, then yes, it will only be in scope inside that block. The scoping rules for pointers are the same as for any other type of variable.

Note that, every time you while loop that starts on line 29 iterates, it checks the type, and if the type is "100A", you create an entirely new array at line 35, throwing away the previous value of machineA and storing a new one in its place. Is this really what you wanted to do?

Also, you're using a variable numOfMachinesA in a couple of loops. Nowhere do you even define that variable, let alone assign it any value.

EDIT: As kingkush says, you're almost always better off using std::vector, than C-style arrays.
Last edited on
Topic archived. No new replies allowed.