For loop problem

Ok so I'm trying to make a program which will tell that how many pancakes 10 people ate indivdually. But I can't seem to understand that why is 'y' not incrementing?

Program should be like this.

Person - Pancakes
1 ----------- 5
2 ----------- 6

and so on





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

 #include <iostream>

 using namespace std;

 int main()
 {
        int hamza [9];
        int y;

        for (y=5; y<=14; y++)

        int talha [9];
        int x;

        cout << "Person  -  Pancakes\n";

        for(x=1; x<=10; x++)
        {
            cout << x <<"   -------   "<< y << endl;

        }


    return 0;
}

Hey there,

It seems that you are not handling the iteration of your for loop anywhere. Try appending the following onto your Y Loop:
for (y=5; y<=14; y++) {}
This will ensure that no confusion is made when compiling and running your code.

In addition, ensure that when you define your Y variable that the value is 5:
int y = 5;

Does this fix the problem?

Thanks,
Garry++
Last edited on
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
#include <iostream>

 using namespace std;

 int main()
 {
        //int hamza [9];
        int y=5;

        //for (y=5; y<=14; y++)

        //int talha [9];
        int x;

        cout << "Person  -  Pancakes\n";

        for(x=1; x<=10; x++,y++)
        {
            cout << x <<"   -------   "<< y << endl;

        }


    return 0;
}
Hello again,

Chriscpp also offers a solid solution: removing the excess code and incrementing both the values at the same time. I would have offered this solution myself but I was unsure if you wanted both the X and Y incrementation to be the same.

In any case, may I offer an extension to chriscpp's method:
1
2
3
4
for(x=1 && y=5; x<=10 && y<=14; x++,y++)
{
cout << x <<"   -------   "<< y << endl;
}


Good Luck,
Garry++
Looking at talha and hamza I get the feeling that the OP wants something like this:

1
2
3
4
for (int x = 0; x < 9;  x++)
{
   cout << talha[x] << "   -------   " << hamza[x] << '\n';
}
Thanks Chriscpp and everyone else. Thats was exactly what I wanted my code to to.

So now I'm trying to make user enter the number of pancakes eated by the person1, person 2 and so on.

So I made this. Its working fine. But after entering the 10 values of number of pancakes eaten by each person. The prog doesnt end. It starts asking for person 1 again. What to do?


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

#include <iostream>

 using namespace std;

 int main()
 {

    int x;
    int y;
    int sum;



     for (x=1; x<=10; x++)
     for (y=1; y<=10; y++)
     {

    cout << "How many pancakes did person " << y << " eat?\n";
    cin >> x;

     }



    return 0;
}


Last edited on
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
#include <iostream>

 using namespace std;

 int main()
 {

    int x;
    int y;
    int sum;



     for (x=1; x<=10; x++)
     {
        //Ensure you are handling this loop. Or it will call the next loop 10 times.
        //Add braces.
     }
     for (y=1; y<=10; y++)
     {

    cout << "How many pancakes did person " << y << " eat?\n";
    cin >> x;

     }



    return 0;
}


This edit should make the program end at the correct time.
Garry++
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
#include <iostream>

 using namespace std;

 int main()
 {

    int x;
    int y;
    //int sum;



     //for (x=1; x<=10; x++)
     for (y=1; y<=10; y++)
     {

    cout << "How many pancakes did person " << y << " eat?\n";
    cin >> x;

     }



    return 0;
}
Topic archived. No new replies allowed.