Two Identical For Loops giving problems

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout<<"Please enter the number of cylinders each disk has: "<<endl; 

for ( int a = d; d >= 1 ; d = d - 1){
cout<<"Cylinders in Disk "<< d <<": ";
cin>>n;
while (n <= 0 ){
   cout<<"Positive integers only, please!: ";
   cin>>n;
}
}

cout<<"Generated the following devices: ";
for (int a = p; p >= 1 ; p = p - 1){
   cout<<"p"<< p << " ";
}
for (int a = d; d >= 1 ; d = d - 1){
	cout<<"d"<< d << " ";
}
for (int a = c; c >= 1 ; c = c - 1){
	cout<<"c"<< c << " ";
}


So it's outputting the p and c, but skipping d from this for loop:
for (int a = d; d >= 1 ; d = d - 1){
cout<<"d"<< d << " ";
}
Can you see where you've used 'd' as a variable already? Why aren't you using the variable that you are creating within each for loops scope?
So how would I do that....using the variable I am created within the for loop scope?
The point is that when you do this:
1
2
3
for ( int a = d; d >= 1 ; d = d - 1){
...
}

You create a variable 'a' but then you don't do anything with it. Also, the first time through the loop, 'd' contains whatever value it had when you entered the code. To put it another way, this loop is the same as:
1
2
3
for (; d >= 1 ; d = d - 1){
...
}


Is this what you intended to do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cout<<"Please enter the number of printers <max 9>: ";
cin>>p;
while (p < 0 || p >= 10){
   cout<<"Positive integers <max 9> only, please!: ";
   cin>>p;
}

cout<<"Please enter the number of disks <max 9>: ";
cin >>d;
while (d < 0 || d >= 10){
   cout<<"Positive integers <max 9> only, please!: ";
   cin>>d;
}

cout<<"Please enter the number of CD/RW drives <max 9>: ";
cin >>c;
while (c < 0 || c >= 10){
   cout<<"Positive integers <max 9> only, please!: ";
   cin>>c;
}


So first, the user is entering number of printers, disks and CR/DW.

1
2
3
4
5
6
7
8
9
10
cout<<"Please enter the number of cylinders each disk has: "<<endl; 

for ( int a = d; d >= 1 ; d = d - 1){
cout<<"Cylinders in Disk "<< d <<": ";
cin>>n;
while (n <= 0 ){
   cout<<"Positive integers only, please!: ";
   cin>>n;
}
}


So then, the user is entering the number of cylinder in the disks depending on the number of disks.

1
2
3
4
5
6
7
8
9
10
cout<<"Generated the following devices: ";
for (int a = p; p >= 1 ; p = p - 1){
   cout<<"p"<< p << " ";
}
for ( ; d >= 1 ; d = d - 1){
	cout<<"d"<< d << " ";
}
for (int a = c; c >= 1 ; c = c - 1){
	cout<<"c"<< c << " ";
}


So then, it needs to print out: P1 P2 P3 P4 P5 D1 D2 D3 D4 D5 D6 C1 C2 C3 C4 C5 C6 etc.... depending on the numbers of printers, disks, and CR/DW but it's skipping the disks.
Last edited on
Some one please help me:
So the first thing is it asks user to enter number of printers, disks, and CR/DW.
Suppose user enters the following values: printers = 2, disks = 2, CR/DW =2

Then the second thing is it asks user to enter the number of cylinders in each disk. So it would print:
Cylinders in disk 2: ____
Cylinders in disk 1: ____
And the user enters the numbers of cylinders in each disk.

The third thing is it would print P2 P1 D2 D1 C2 C1, but it is only printing P2 P1 C2 C1, skipping D2 D1.




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.
cout<<"Cylinders in Disk "<< d d+1<<": ";

This lines is giving me errors.
Oops. It should be cout<<"Cylinders in Disk "<< d+1<<": ";
closed account (4y64izwU)
Say I enter 8 for the number of disks then the program just does
Cylinders in Disk 9: ___
and then freezes, please help
Last edited on
You need to the code the enter the number of cylinders. So that part should be:
1
2
3
4
5
6
7
8
9
10
11
12
cout<<"Please enter the number of cylinders each disk has: "<<endl;
cin >> d;  // This is the new line.
std::vector<int> cylinderCounts;
for ( int i=0; i<d; ++i){
    cout<<"Cylinders in Disk "<< d+1<<": ";
    cin>>n;
    while (n <= 0 ){
        cout<<"Positive integers only, please!: ";
        cin>>n;
    }
    cylinderCounts[i] = n;
}
Topic archived. No new replies allowed.