2 for loops in a single program?

I have to make my program display days 1-30 along side numbers increasing by 5 each day beginning with 6 on the first day. I wrote 2 for loops on separate tabs but now I want to know if its possible to combine them into 1. I'm still new to c++ so any tips appreciated. Thank you.

For loop 1

int day,;

for (day = 1; day <=30; day = day + 1)

For loop 2

int candy;

for (int candy = 6; candy <=151; candy = candy +5)
1
2
3
4
5
6
7
for(int day = 1; day <= 30; ++day)
{
    for(int candy = 6; candy <= 151; candy += 6)
    {
    ...
    }
}

Is good enough. Nested loops are very common.
Last edited on
Use nested for loops, they can do the job for you
Currently trying nested loops with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
 
    int day,candy;
    
    
   
    
    for (day = 1; day <= 30; day = day +1)
    {
     for (candy = 6; candy <= 151; candy = candy + 5)
      {       
                    cout << day << " " << candy << endl;
                    }
                    }
    system("PAUSE");
    return 0;
    
}


This is my output window which is not correct. It's supposed to say 1-30 and the 6-151 next to it >.>

21 11
21 16
21 21
21 26
21 31
21 36
21 41
21 46
21 51
21 56
21 61
21 66
21 71
21 76
21 81
21 86
21 91
21 96
21 101
21 106
21 111
21 116
21 121
21 126
21 131
21 136
21 141
21 146
21 151
22 6
22 11
22 16
22 21
22 26
22 31
22 36
22 41
22 46
22 51
22 56
22 61
22 66
22 71
22 76
22 81
22 86
22 91
22 96
22 101
22 106
22 111
22 116
22 121
22 126
22 131
22 136
22 141
22 146
22 151
23 6
23 11
23 16
23 21
23 26
23 31
23 36
23 41
23 46
23 51
23 56
23 61
23 66
23 71
23 76
23 81
23 86
23 91
23 96
23 101
23 106
23 111
23 116
23 121
23 126
23 131
23 136
23 141
23 146
23 151
24 6
24 11
24 16
24 21
24 26
24 31
24 36
24 41
24 46
24 51
24 56
24 61
24 66
24 71
24 76
24 81
24 86
24 91
24 96
24 101
24 106
24 111
24 116
24 121
24 126
24 131
24 136
24 141
24 146
24 151
25 6
25 11
25 16
25 21
25 26
25 31
25 36
25 41
25 46
25 51
25 56
25 61
25 66
25 71
25 76
25 81
25 86
25 91
25 96
25 101
25 106
25 111
25 116
25 121
25 126
25 131
25 136
25 141
25 146
25 151
26 6
26 11
26 16
26 21
26 26
26 31
26 36
26 41
26 46
26 51
26 56
26 61
26 66
26 71
26 76
26 81
26 86
26 91
26 96
26 101
26 106
26 111
26 116
26 121
26 126
26 131
26 136
26 141
26 146
26 151
27 6
27 11
27 16
27 21
27 26
27 31
27 36
27 41
27 46
27 51
27 56
27 61
27 66
27 71
27 76
27 81
27 86
27 91
27 96
27 101
27 106
27 111
27 116
27 121
27 126
27 131
27 136
27 141
27 146
27 151
28 6
28 11
28 16
28 21
28 26
28 31
28 36
28 41
28 46
28 51
28 56
28 61
28 66
28 71
28 76
28 81
28 86
28 91
28 96
28 101
28 106
28 111
28 116
28 121
28 126
28 131
28 136
28 141
28 146
28 151
29 6
29 11
29 16
29 21
29 26
29 31
29 36
29 41
29 46
29 51
29 56
29 61
29 66
29 71
29 76
29 81
29 86
29 91
29 96
29 101
29 106
29 111
29 116
29 121
29 126
29 131
29 136
29 141
29 146
29 151
30 6
30 11
30 16
30 21
30 26
30 31
30 36
30 41
30 46
30 51
30 56
30 61
30 66
30 71
30 76
30 81
30 86
30 91
30 96
30 101
30 106
30 111
30 116
30 121
30 126
30 131
30 136
30 141
30 146
30 151
Press any key to continue . . .
Last edited on
bump :(
Provide an example of how the output should look like.
Nested loops aren't called for here:

1
2
for ( unsigned day = 1, candy = 6; day < 30; ++day, candy += 5 )
    std::cout << day << ' ' << candy << '\n' ;


http://ideone.com/AuHCeL
Last edited on
I think I almost got it

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
#include <iostream>
using namespace std;
int main()
{
 
    int day,candy,increase;
    candy = 6;
    increase = 5;
    day = 1;
    
   
    
   for(candy = 6; candy <= 151; candy = candy + increase)
 
{
    for(day = 1; day <= 30; ++day)
    {
                            candy = candy + increase;
                            cout << day << " " << candy << endl;
                            
    }
    system("PAUSE");
}
    
    return 0;
    
}


Here is the output. HOWEVER. The right column should start with 6 not 11. As in:
1 6
2 11
1 11
2 16
3 21
4 26
5 31
6 36
7 41
8 46
9 51
10 56
11 61
12 66
13 71
14 76
15 81
16 86
17 91
18 96
19 101
20 106
21 111
22 116
23 121
24 126
25 131
26 136
27 141
28 146
29 151
30 156
Press any key to continue . . .

Well, I definetly misunderstood the goal.
To start with 6 swap lines 18 and 19, but if that's all you need to do, cire's solution is better.
Yes! That's what I was looking for. Thank you. Here is the final piece if anyone was interested.
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
#include <iostream>
using namespace std;
int main()
{
 
    int day,start,increase;
    start = 6;
    increase = 5;
    day = 1;
    
   
    
   for(start = 6; start <= 151; start += increase)
 
{
    for(day = 1; day <= 30; ++day)           
    {
                            cout << day << " " << start << endl;
                            start = start + increase;
                            
    }
    system("PAUSE");
}
    
    return 0;
    
}


1 6
2 11
3 16
4 21
5 26
6 31
7 36
8 41
9 46
10 51
11 56
12 61
13 66
14 71
15 76
16 81
17 86
18 91
19 96
20 101
21 106
22 111
23 116
24 121
25 126
26 131
27 136
28 141
29 146
30 151
Press any key to continue . . .
Yes! That's what I was looking for. Thank you. Here is the final piece if anyone was interested.


Well, it's a piece of code that looks like it was written by someone who was guessing at what he was doing. As I mentioned, there is no need for nested loops (and indeed, you might as well not have one here, because the outer "loop" will only ever be executed one time.)

This is entirely equivalent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main()
{

    int start = 6;
    const int increase = 5;

    for (int day = 1; day <= 30; ++day)
    {
        cout << day << " " << start << '\n';
        start += increase;
    }

    system("PAUSE");
}
Topic archived. No new replies allowed.