Hangman program not accepting input

The dreaded hangman program...
I can't, for the life of me, figure out why my program won't accept any input from the user. I feel it's simply organization or bad use of functions. Every time I enter a letter to guess, it says it isn't a single letter, when it is. Please help me, I've lost almost all my hair over this.
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "pch.h"
#include<iostream>
#include<stdlib.h>
#include<string>

using namespace std;

int main() {

	string wordtoguess; 
	string lines;
	string userGuess;
	char inletter; 
	int i;
	int guess = 5;
	int len;

	cout << "Insert a word for player two to guess: ";
	cin >> wordtoguess;

	for (int i = 0; i < wordtoguess.length(); i++)
	{
		lines += "_ ";
	}

	system("cls");

	cout << lines << endl;

	while (guess > 0) 
	{
		cout << "You have " << guess << " guesses left. Guess a letter: ";
		cin >> userGuess;

		//checking if player entered single char or not

		while (true) 
		{

			if (userGuess.length() == 1) 
			{
				inletter = userGuess[0];
			}

			cout << "You did not enter a letter! Try again: ";
			cin >> userGuess;

		}

		//find letters guessed and positions

		string guess_positions = "";

		for (int i = 0; i < wordtoguess.length(); i++) 
		{
			if (wordtoguess[i] == inletter || abs(wordtoguess[i] - inletter) == 32) 
			{

				lines[2 * i] = inletter;
				char numstr[21];

				sprintf_s (numstr, "%d", i);
				guess_positions = guess_positions + numstr; //store position
			}
		}

		if (guess_positions.length() > 0) 
		{

			cout << "You guessed the letters at position ";

			for (i = 0; i < guess_positions.length(); i++) 
			{

				if (i == 0)
				{

					cout << guess_positions[i];
				}
				else
				{
					cout << " and " << guess_positions[i];
				}
			}

			cout << " correct" << endl;

			cout << lines << endl;

			//check all places got filled in lines wordtoguess

			len = 0;

			for (i = 0; i < lines.length(); i++) 
			{
				if (lines[i] != ' '&&lines[i] != '-') {

					len += 1;

				}
			}

			//decide to continue or win

			if (len == wordtoguess.length()) 
			{
				cout << "You win!! You guessed the word right in " << (5 - guess) + 1 << " guesses." << endl;
			}

			cout << "You have " << guess << " guesses left. Take a guess at the word:";
			cin >> userGuess;

			//compare actual word with guessed word i.e, each char

			if (wordtoguess.length() == userGuess.length()) 
			{

				len = 0;

				for (i = 0; i < userGuess.length(); i++) 
				{

					if (wordtoguess[i] == userGuess[i] || abs(wordtoguess[i] - userGuess[i]) == 32) 
					{

						len++;
					}
				}

				if (len == wordtoguess.length()) 
				{
					cout << "You win!! You guessed the word right in " << (5 - guess) + 1 << " guesses." << endl;
				}
				else 
				{
					guess--;//if entered word is not same decrease guess count
				}

			}
			else 
			{
				guess--;//if entered word is not same decrease guess count
			}

		}
		else 
		{
			guess--;//if entered word is not same decrease guess count
		}
	}
	if (guess = 0) 
	{
		cout << lines << endl;
		cout << "Sorry! you have lost the game! Secret word is: " << wordtoguess << endl;
	}



	return 0;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
		while (true) 
		{

			if (userGuess.length() == 1) 
			{
				inletter = userGuess[0];
			}

			cout << "You did not enter a letter! Try again: ";
			cin >> userGuess;

		}

How does this loop ever end?
^^ also I would recommend creating a hangman class or even breaking the code up into functions,the main function should be generally as concise as possible right now it's quite hard to read and if you are looking back at this code maybe in 6 months to a years time or so it will be very hard to decypher.
@adam2016 I'm not allowed to use classes in my program.
closed account (E0p9LyTq)
I'm not allowed to use classes in my program.

Are user-created functions allowed? You could streamline main by taking the bulk of your code and putting it into a few functions you call in main.
Topic archived. No new replies allowed.