loop problem

why does the "multiples++" variable have any effect in the code although it isnt used any where while calculation of the multiples? the programme should only show the values of the "counter" variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   1: #include <iostream>
2:
3: int main()
4: {
5: int counter = 0;
6: int multiples = 0;
7:
8: while (true)
9: {
10: counter++;
11: if (counter % 14 == 0)
12: {
13: std::cout << counter << ā€œ ā€œ;
14: multiples++;
15: }
16: if (multiples > 19)
17: {
18: break;
19: }
20: }
21:
22: std::cout << ā€œ\nā€;
23: return 0;
24: }
I don't quite follow. multiples does have an effect in the code, its what you are using to break out of your "infinite" while loop.
multiples here is used to control the termination of the loop.

It serves a similar purpose to the condition n < 20 in this code:
1
2
3
4
5
6
    int multiple = 0;

    for (int n = 0; n < 20; n++)
        cout << (multiple += 14) << " ";

    cout << endl;


or perhaps closer to the original:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    int counter = 0;
    int multiples = 0;

    while (multiples<20)
    {
        counter++;
        if (counter % 14 == 0)
        {
            cout << counter << " ";
            multiples++;
        }
    }

    cout << endl;
Last edited on
ok i got it thanks :)
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
31
32
33
34
35
36
#include <iostream>
using namespace std;
void playgame ()
{}
void loadgame ()
{}
void playmultiplayer ()
{}
int main ()
{
int input;
cout << "1. Play game\n";
cout << "2. Load game\n";
cout << "3. Play multiplayer\n";
cout << "4. Exit\n";
cout << "Selection: ";
cin >> input;
switch ( input )
{
case 1: 
playgame();
break;
case 2:
loadgame();
break;
case 3:
playmultiplayer();
break;
case 4:
cout << "Thank you for playing!\n";
break;
default:
cout << "Error, bad input, quitting\n";
break;
}
}


how can i put it in a loop so that when the default value is printed it asks the user for input again?
closed account (3qX21hU5)
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
31
// Right now the only way to exit the loop and program is the select number 4
while (true)
{
    int input;
    cout << "1. Play game\n";
    cout << "2. Load game\n";
    cout << "3. Play multiplayer\n";
    cout << "4. Exit\n";
    cout << "Selection: ";
    cin >> input;
    switch ( input )
    {
        case 1: 
            playgame();
            break;
        case 2:
            loadgame();
            break;
        case 3:
            playmultiplayer();
            break;
        case 4:
            cout << "Thank you for playing!\n";
            // You could use break; instead of return 0; if you want to only exit the loop and not the
            // whole program.
            return 0;
        default:
            cout << "Error, bad input, quitting\n";
            break;
    }
}


I would also suggest you indent each scope instead of having them all at the beginning so that the program is easier to read.
but doing this just puts it in an infinite loop if anything else is typed .
The default just keeps looping. I wanted to know if there was any way to give the user a second chance for entering when the default statement is printed.

Can someone help plzz.......
Last edited on
Topic archived. No new replies allowed.