how to make a loop repeat at a certain point?

if (initial_pile == 1 || initial_pile == 2 || initial_pile == 3)
{
int computer_choose1 = 1 + rand() % ((initial_pile) - 1 + 1);

cout << "The computer chose " << computer_choose1 << " numbers\n";

int new_pile2 = initial_pile - computer_choose1;

cout << "There are only " << new_pile2 << " left. Choose any number.\n";
cin >> user_choose1;

if (1 <= user_choose1 && user_choose1 <= new_pile2)
{
int computer_choose1 = 1 + rand() % ((choose_pile) - 1 + 1);
int new_pile = new_pile2 - (user_choose1 + computer_choose1);
int new_pile1 = new_pile2 - user_choose1;

if (new_pile1 == 0)
{
cout << "You Lose!\n";
initial_pile = new_pile1;
}
else
{
cout << "Thus, there are " << new_pile << " marbles left.\n";

initial_pile = new_pile;

if (1 <= computer_choose1 && computer_choose1 <= choose_pile)
{
if (new_pile == 0)
cout << "You Win!\n";
}
}
}
else
{
cout << "ERROR: Please choose a number again.\n";
}


Here is my code and I want to repeat the loop from "int new_pile2 = initial_pile - computer_choose1;" if the program gets to "cout << "ERROR: Please choose a number again.\n";"
What kind of operator do I need to use in order to make it repeat at a certain starting point?
It would be a lot helpful if you wrap your code with {code} tags. (Replace '{' with '[')...

Like this:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main(void)
{
      cout << "This is some code..." << endl;
      return 0;
}


I would help you but I am having difficulty reading the code. Please repost the code with write tags.

P.S. You can click the "<>" symbol in the toolbox on the right side to add code tags and then enter your code inside the tags.
Last edited on
how about using the "The goto statement"

http://www.cplusplus.com/doc/tutorial/control/
No need for the goto statement, don't bother reading that page. Goto is a control structure that has a lot of scope issues. Use a while-loop instead.

Repost of the original code:
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
if (initial_pile == 1 || initial_pile == 2 || initial_pile == 3)
{
    int computer_choose1 = 1 + rand() % ((initial_pile) - 1 + 1);
    cout << "The computer chose " << computer_choose1 << " numbers\n";
    int new_pile2 = initial_pile - computer_choose1;
    cout << "There are only " << new_pile2 << " left. Choose any number.\n";
    cin >> user_choose1;
    if (1 <= user_choose1 && user_choose1 <= new_pile2)
    {
        int computer_choose1 = 1 + rand() % ((choose_pile) - 1 + 1);
        int new_pile = new_pile2 - (user_choose1 + computer_choose1);
        int new_pile1 = new_pile2 - user_choose1;
        if (new_pile1 == 0)
        {
            cout << "You Lose!\n";
            initial_pile = new_pile1;
        }
        else
        {
            cout << "Thus, there are " << new_pile << " marbles left.\n";
            initial_pile = new_pile;
            if (1 <= computer_choose1 && computer_choose1 <= choose_pile)
            {
                if (new_pile == 0) cout << "You Win!\n";
            }
        }
    }
    else
    {
        cout << "ERROR: Please choose a number again.\n"; 
    }
}    // You missed a } here, I placed it myself 
There are many ways. You can create functions which perform the specific functions and just recursively call them. But I hear it is evil (as in dangerous code)...
You could use goto but as Kyon pointed out, it's also evil code.
A better option would be using loops. You could:

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
37
if (initial_pile == 1 || initial_pile == 2 || initial_pile == 3)
{
    int computer_choose1 = 1 + rand() % ((initial_pile) - 1 + 1);
    cout << "The computer chose " << computer_choose1 << " numbers\n";

while(1)
{
    int new_pile2 = initial_pile - computer_choose1;
    cout << "There are only " << new_pile2 << " left. Choose any number.\n";
    cin >> user_choose1;
    if (1 <= user_choose1 && user_choose1 <= new_pile2)
    {
        int computer_choose1 = 1 + rand() % ((choose_pile) - 1 + 1);
        int new_pile = new_pile2 - (user_choose1 + computer_choose1);
        int new_pile1 = new_pile2 - user_choose1;
        if (new_pile1 == 0)
        {
            cout << "You Lose!\n";
            initial_pile = new_pile1;
        }
        else
        {
            cout << "Thus, there are " << new_pile << " marbles left.\n";
            initial_pile = new_pile;
            if (1 <= computer_choose1 && computer_choose1 <= choose_pile)
            {
                if (new_pile == 0) cout << "You Win!\n";
            }
        }
    break;
    }
    else
    {
        cout << "ERROR: Please choose a number again.\n"; 
    }
}
}

Last edited on
@Zia Ur Rehman: There is nothing wrong with functions recursivley calling themselves as far as virus signitures go. It's just hard for some of the new and mid level people to read\understand. In this particular case though the entire structure of the program would need to be overhauled to get it to work as it is supposed to (neat and efficent), I know this because I just tried to type up an example and my head started to hurt :p.

@OP: What game is this supposed to be?



Last edited on
how about using the "The goto statement"

Never do that.
Topic archived. No new replies allowed.