String help, please

So I've been fiddling with this code for weeks. It gets to the
1
2
3
string words;
	cout << "\n Please enter " << amount << " words" << endl;
	getline (cin, words); 
section and shuts down. I am so lost!

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
  #include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
const int MAX_TRIES = 5;
int letterFill (char, string, string&);

int main()
{
	string name;
	char letter;
	int num_of_wrong_guesses = 0;
	int amount;
	string word;

	cout << "Welcome to Hangman \n" << endl;

	// Entering the amount of words to be used.  

	std::cout << "How many words do you have? (Please enter a number between 2 and 10)" << std::endl;
	cin >> amount;

	// Entering the words needed for gameplay.

	string words;
	cout << "\n Please enter " << amount << " words" << endl;
	getline (cin, words);

	cout << "\n Thank you.  Now lets play Hangman!" << endl;

	// instert a code to choose a word at random

	srand(time(NULL));

	int n = rand() % 10;
	word = words[n];

	// code for actual gameplay

	string unknown(word.length(), '*');
	cout << "\n Each letter is represented by an asterisk.";
	cout << "\n You have to type only one letter in each try.";
	cout << "\n You have " << MAX_TRIES << " tries to guess your word";
	cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";


	while (num_of_wrong_guesses < MAX_TRIES)
	{
		cout << "\n\n" << unknown;
		cout << "\n Guess a letter: ";
		cin >> letter;

		if (letterFill(letter, word, unknown) == 0)
		{
			cout << endl << "Sorry, that letter isn't there!" << endl;
			num_of_wrong_guesses++;
		}
		else
		{
			cout << endl << "You have found a letter!" << endl;
		}
		cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
		cout << " guesses left." << endl;


		if (word == unknown)
		{
			cout << word << endl;
			cout << "You got it right!";
			break;
		}
	}

	// Game over
	if (num_of_wrong_guesses == MAX_TRIES)
	{
		cout << "\n Sorry, you lose... you've been hanged." << endl;
		cout << "The word was : " << word << endl;
	}

	cin.ignore();
	cin.get();
	return 0;
}

int letterFill(char guess, string secretword, string &guessword)
{
	int i;
	int matches = 0;
	int len = secretword.length();
	for (i = 0, i < len; i++;)
	{
		// Did we already match this letter in a previous guess?
		if (guess == guessword[i])
			return 0;
		// Is the guess in the word?
		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}
	return matches;
}
Last edited on
closed account (E0p9LyTq)
Your std::string variable words is not an array. Using operator[] accesses a single character within the string.

A std::string is an array internally, but not in the way you are using it. You are wanting an array of strings.
Last edited on
closed account (E0p9LyTq)
A quick and dirty snippet for entering a list of words to add to a vector, and then use the C++ random library to chose one of those words:

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
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <chrono>


int main()
{
   std::vector<std::string> words;
   std::string input;

   std::cout << "Enter your words (use QUIT to quit): ";

   while (true)
   {
      std::cin >> input;

      if (input == "QUIT")
      {
         break;
      }
      words.push_back(input);
   }
   std::cout << "\nYou added " << words.size() << " words.\n";

   // create a default random engine seeding it with the current system clock time
   std::default_random_engine URNG (static_cast<unsigned int> (std::chrono::system_clock::now().time_since_epoch().count()));

   // create a distribution to choose a random word
   std::uniform_int_distribution<> dis(0, words.size() - 1);

   std::string word = words[dis(URNG)];

   std::cout << "\nThe chosen word is '" << word << "'\n";

}


Enter your words (use QUIT to quit): now is the time for all good QUIT

You added 7 words.

The chosen word is 'all'
Last edited on
Thanks. I'll try that. Then add the next part I need to do. I feel like I'll never finish this assignment. Been at it for 2 weeks already. :(
Finally handed this in. Thanks again for your help. (I had to add a few other bits for it)
Topic archived. No new replies allowed.