Hey, that's a good explanation of what you want the code to do!
The thing that's missing here is that you want to store the number of cylinders for each disk. That means you can't use a single variable to store it. Use a
vector<int>
. So add
#include <vector>
to the top of your program and then get the cylinders like this:
1 2 3 4 5 6 7 8 9 10 11
|
cout<<"Please enter the number of cylinders each disk has: "<<endl;
std::vector<int> cylinderCounts;
for ( int i=0; i<d; ++i){
cout<<"Cylinders in Disk "<< d d+1<<": ";
cin>>n;
while (n <= 0 ){
cout<<"Positive integers only, please!: ";
cin>>n;
}
cylinderCounts[i] = n;
}
|
Note that I'm using a different variable (i) in the loop. This way d doesn't change. I'll do the same thing later. Also note that
++i
is a shorthand way of saying
i=i+1
.
Now when you print the devices out, use the same idea: leave p, c, and d alone:
1 2 3 4 5 6 7 8 9
|
for (int i = 1; i<=p; ++i){
cout<<"p"<< i << " ";
}
for (int i=1; i<=d; ++i) {
cout<<"d"<< i << " ";
}
for (int i=1; i<=c; ++i) {
cout<<"c"<< i << " ";
}
|
But you really want to print the number if cylinders in each disk. So the loop for disks might be:
1 2 3
|
for (int i=1; i<=d; ++i) {
cout<<"d"<< i << " " << cylinderCounts[i-1] " cylinders << '\n';
}
|
I'm using cylinderCounts[i-1] instead of cylinderCounts[i] because vectors and "zero-based". In other words, the first element is at index 0, not index 1.