vector

can someone please help me?

how can I create a vector with the following devices: printer, disk, cd/rw.

so the user is going to ask first for the number of printers, disk, cd/rw, so if user enters 3 for printers, 4 for disks and 2 for cd/rws it should print the following:

so it needs to print p1 p2 p3 d1 d2 d3 d4 c1 c2

I need to do this using vector, someone please help!

If I understand your quiz correctly, you may first declare a vector<string>, and insert values depending on the user input. And finally you just print all the contents in the vector to screen.
so this is what I did, but all it does it output 2 2 2 if that's what I enter. but if I enter 2 for AllDevices [0] it needs to print 1 2. So therefore it should be printing 1 2 1 2 1 2
Last edited on
First of all: devices is not changed outside deviceSetup() since you pass it as a copy.

but all it does it output 2 2 2 if that's what I enter.
You mean line 20?
If you want that other output you should cout << j; right after line16.

You might want to change line 16:
devices.push_back (j);

What is the loop on line 10 good for?
Last edited on
now its just outputting 01 01 01.
Last edited on
:) the computer is your friend not your enemy

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
void deviceSetup (vector <int> &devices) { // Note: &
	int AllDevices[3];
	cout << "Sysgen started... \nHow many devices? \n- Printers: ";
	cin>> AllDevices[0];
	cout << "- Disks: ";
	cin >>AllDevices[1];
	cout << "- CD/RW drives: ";
	cin >>AllDevices[2];

    for (int i = 0; i < 3; i++) {
		devices.push_back(AllDevices[i]);

	for (int j = 0; j < AllDevices[i]; j++) {
			devices.push_back (j);
			cout << j + 1 << " ";
		}
    }
}


int main(){

  vector <int> devices;
  deviceSetup (devices);

  return 0;
}
Last edited on
Topic archived. No new replies allowed.