Program is looping too many times

I've been playing and playing, but I can't get my program to loop quite correctly, I need getSize() to loop for as many characters are in 'string answer;' but not if int lives == 5. Any help, thank you!

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
#include <string>
#include "Next.h"
#include "string"
#include "string.h"

using namespace std;

// Program Variables
    string answer;
    string player1, player2;
    int answerchar;
    char a;
    int lives = 0;
    int i = 0;
    int u = 0;
    int player1score, player2score;
    size_t nCharCounter = 0;

// Function Prototypes:
    int init();
    void getWord();
    int player1check();
    int player2check();
    int printscore();
    int close();
    int getSize();


// Functions
int init(){
    cout << "Welcome to Hangman!\n" << "The rules are simple!\n" << "You get five tries to guess the word.\n" << "Go!\n\n\n";
    cout << "WAIT! How could I forget? We need some players!\n" << "Enter the first players name: ";
    cin >> player1;
    cout << "Enter the second players name: ";
    cin >> player2;
}

void getWord(){
    cout << "Give me a word!:\n";
    cin >> answer;
}

int player2check()
{
        printscore();
    cout << player2 << " Give me a letter!:\n";
    cin >> a;
    if(answer[u] == a){
            cout << "You Got it!!!\n";
            player2score++;
            u++;
    } else if(answer[u] != a){
        cout << "You lost too!\n";
        lives++;
        player1check();
        u++;
    }
}

int player1check()
{
    cout << player1 << " Give me a letter!:\n";
    cin >> a;
    if(answer[u] == a){
                    cout << "You got it!!!\n";
                    player1score++;
                    u++;
                } else if(answer[u] != a){
                    player2check();
                    lives++;
            }
        printscore();

}

int close(){
    cout << player1score << endl;
    cout << player2score << endl;
        if(player1score > player2score){
            cout << player1 << " wins!\n";
        }else if(player1score < player2score){
            cout << player2 << " wins!\n";
        }
}

int printscore(){
    cout << player1 << "'s score is: " << player1score << endl;
    cout << player2 << "'s score is: " << player2score << endl;
    cout << "Live's left are: " << lives << endl;
}

int getSize(){

    if(nCharCounter <= answer.length()){
        player1check();
        nCharCounter++;
    }
}

int main()

{
    init();
    getWord();
    do getSize();
    while(nCharCounter <=6 || lives <=5);
    close();
        return 0;
    }
Hi,

Firstly, you are using too many global variables. They may be not be a big deal, but they tend to complicate your code and make your code harder to read.

Always find a way to use locals instead of globals. If you still can't avoid it, modify the names of globals the standard way so that people can distinguish between them and local variables much easier.

http://www.learncpp.com/cpp-tutorial/4-2a-why-global-variables-are-evil/
Welcome to Hangman!
The rules are simple!
You get five tries to guess the word.
Go!



WAIT! How could I forget? We need some players!
Enter the first players name: Player1
Enter the second players name: Player2
Give me a word: Blue

...


Then what do you think you expect the rest of the output to be?
Topic archived. No new replies allowed.