loop problem

Apr 20, 2013 at 1:09pm
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: }
Apr 20, 2013 at 1:13pm
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.
Apr 20, 2013 at 1:49pm
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 Apr 20, 2013 at 2:08pm
Apr 21, 2013 at 6:28am
ok i got it thanks :)
Apr 21, 2013 at 7:18am
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?
Apr 21, 2013 at 7:25am
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.
Apr 21, 2013 at 10:27am
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 Apr 22, 2013 at 7:20pm
Topic archived. No new replies allowed.