trying to loop or call the main function to run again

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int mainagain();

int main()
{
    //unsigned seed = time (0);     //commented the random generator to troubleshoot code
    //srand (seed);                 
    int x = rand(), guess;          
    char y, again;
    
    do { 
        cout << "Im thinking of a number, take a guess.\n";
        cin >> guess;
       
    if ( guess < x )
        cout << "guess higher\n";
    else if (guess > x )
        cout << "guess lower\n";
    else if (guess == x)
        cout << "jackpot\n";
    else cout << "invalid\n";
    }while (guess != x);
    
    if (guess == x)
        {
        cout << "\a";
            for(x = 1; x < 5; x++)
                cout << "\aJackpot" << endl;
        }
    
    cout << "Do you want to play again? y or n\n";
    cin >> again;
    
    if(again == y)
    {
        mainagain();            // trying to call the main function again to repeat the program
    }else
        cout << "Thanks for playing.\n";
        system ("PAUSE");
        return 0;
    
    system("PAUSE");
    return 0;
}
// not sure if i needed a seperate function to call or if i could call the main function within its self.
int mainagain()
{
    //unsigned seed = time (0);
    //srand (seed);
    int x = rand(), guess;
    char y, again;
    
    do { 
        cout << "Im thinking of a number, take a guess.\n";
        cin >> guess;
       
    if ( guess < x )
        cout << "guess higher\n";
    else if (guess > x )
        cout << "guess lower\n";
    else if (guess == x)
        {
        for(x = 1; x < 5; x++)
        cout << "\aJackpot" << endl;
        }
    else cout << "invalid\n";
    }while (guess != x);
    
    cout << "Do you want to play again? y or n\n";
    cin >> again;
    
    if(again == y)
    {
        cout << "lets play again.\n";
        main();
    }else
        cout << "Thanks for playing.\n";
        system ("PAUSE");
        return 0;
}
Last edited on
Don't ever call main. This is not BASIC; calling main does not take you back to the start - it creates a whole new main function on the stack, eating up memory, and then does it again when you call main again, and then again, and again, and again, each time eating up more and more of the stack, until you run out of memory.

I see you already know how to use the do-while loop. Perhaps sticking a

do{

at the top of main and a

} while (again == 'y');

at the end is what you need.
ok i can see how that will work, but on another note, how would i call a function to make it loop.

if(again ==y)
mainagain();

etc.

1
2
3
do{
somefunction();
} while (someCondition);


or

1
2
3
4
while(someCondition)
{
someFunction();
}


or

1
2
3
4
for(initialization; condition; increase)
{
someFunction();
}


These are all covered here: http://www.cplusplus.com/doc/tutorial/control/

Just don't call main ever, and be very careful calling a function from inside itself (this is known as recursion and if you get it wrong, you overflow the stack and crash).
Last edited on
im somewhat familiar with the loops, but i wanted to call functions to make the loop happen. and if again == y i wanted the same function to be called again.

I found my error:
I had if(again ==y)
should have been if(again =='y')
I guess i will have to make do with:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int mainagain();


int main()
{

    unsigned seed = time (0);
    srand (seed);                 
    int x = rand(), guess;          
    char y, again;
do
{   
    do { 
        cout << "Im thinking of a number, take a guess.\n";
        cin >> guess;
       
    if ( guess < x )
        cout << "guess higher\n";
    else if (guess > x )
        cout << "guess lower\n";
    else if (guess == x)
        cout << "jackpot\n";
    else cout << "invalid\n";
    }while (guess != x);
    
    if (guess == x)
        {
            for(x = 1; x < 5; x++)
                cout << "\aJackpot" << endl;
        }
    
    cout << "Do you want to play again? y or n\n";
    cin >> again;
    
    if(again == 'y')
    {
        main();            // trying to call the main function again to repeat the program
    }else
        cout << "Thanks for playing.\n";
        system ("PAUSE");
        return 0;
}while (again == 'y');
    system("PAUSE");
    return 0;
}
1
2
3
 if(again == 'y')
    {
        main();          


Why do you still have the call to main in there? That's nonsensical; why put in a do-while loop to loop around all of main if you're not going to use 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main()
{

    unsigned seed = time (0);
    srand (seed);                 
    char again;
do
{   
    int x = rand(), guess;     
    do { 
        cout << "Im thinking of a number, take a guess.\n";
        cin >> guess;
       
    if ( guess < x )
        cout << "guess higher\n";
    else if (guess > x )
        cout << "guess lower\n";
    else if (guess == x)
        cout << "jackpot\n";
    else cout << "invalid\n";
    }while (guess != x);
    
    if (guess == x)
        {
            for(x = 1; x < 5; x++)
                cout << "\aJackpot" << endl;
        }
    
    cout << "Do you want to play again? y or n\n";
    cin >> again;
}while (again == 'y');
     cout << "Thanks for playing.\n";
    system("PAUSE");
    return 0;
}
Last edited on
Thanks, I was a bit lost and jumbled up my code. Your coding looks much cleaner than mine. Ill adjust
Topic archived. No new replies allowed.