-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.cpp
More file actions
58 lines (49 loc) · 1.52 KB
/
hangman.cpp
File metadata and controls
58 lines (49 loc) · 1.52 KB
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
string fruits[10] {"mango","papaya","apple","orange","banana","kiwi","watermelon","peach","grapes","strawberry"};
string answer = fruits[rand()% 10];
vector<int> foundchar;
int foundCount = 0;
int lives = 5;
while (foundCount < (answer.size()-1) && lives > 0)
{
cout << "Guess the fruits name:" << std :: endl;
cout << " \nYou currently have: " << lives << " lives." << std::endl;
foundCount = 0;
for (int i = 0; i < answer.length(); ++i)
{
if (find (foundchar.begin(), foundchar.end(), i) != foundchar.end())
{
std::cout << answer[i] << " ";
foundCount++;
}
else
{
cout << "_ ";
}
}
cout << std::endl;
char userChoice = ' ';
bool found = false;
cout << "Choose a character..." << std::endl;
cin >> userChoice;
for (int i = 0; i < answer.length(); i++)
{
if (userChoice == answer[i])
{
found = true;
foundchar.push_back (i);
}
}
if (found == false)
lives--;
}
if (lives != 0)
cout << "\nCongrats you found the word!" << std::endl;
else
cout << "The correct word is " << answer << std::endl;
cout << "\n You failed !" << std::endl;
return 0;
}