A LITTLE HELP NEDEED WITH GAME HANGMAN

HELOO!!!
I AM WRITING A CODE FOR A HANG MAN GAME AS MY FINAL PROJECT *i am new to c++ as its my 1st semester
the code has a problem i want that if user inputs a key more than once it tells the user that you have entered the key befor...instead of that my code read the repeated number as a right number :/
NEED HELP!!!!!
*it was a long code but i have added a portion of it but it runs well *
plzz help if some one can :)






<[code][#include<iostream>
#include<ctime>
#include<windows.h>
#include<iomanip>
using namespace std;
int main()
{ int right=0;
int count=0;
int wrong=5;
char entry;
char arraydash[8]={'*','*','*','*','*','*','*','*'};
char arraypuzzle[8]={'P','A','K','I','S','T','A','N'};



cout<<"YOU HAVE TO GUESS A WORLD\nENTER SINGLE ALPHABET AT A TIME WITH
CAPS ON \n YOU WILL HAVE 5 CHANCES IF YOU ENTER A WRONG WORD\nEACH (*) REPRESENT A ALPHABET \n";
Sleep(3000);
system("pause");


cout<<endl;
//1
do
{

cout<<"#1.\n*hint name of a country\n";

for (int i=0;i<8;i++)
{
cout<<arraydash[i];
}
cout<<"\nENTER THE ALPHABET :";
cin>>entry;
for (int i=0;i<8;i++)
{
if (entry==arraypuzzle[i])
{ right++;
count++;
arraydash[i]=entry;
}
}
if(count>0)
{
cout<<"You have guessed it right \n";

for (int i=0;i<8;i++)
{
cout<<arraydash[i];
}
cout<<endl;
count=0;
cout<<endl;
}
else
{
cout<<"TRY AGAIN\n";
cout<<"YOU HAVE "<<wrong--<<" tries left\n";
}

}
while((wrong>-1)&&(right<8));
if(wrong>-1)
{
cout<<"\nYOU WON\n";
}
else
{
cout<<"BETTER LUCK NEXT TIME THE WORD WAS \n";
for(int i=0;i<8;i++)
{
cout<<arraypuzzle[i];
}
}
wrong=5;
right=0;
cout<<endl;
cout<<endl;
system("pause");

}>]
Last edited on
Please use code tags when posting your code.

What you can do is check if your entry character is in the arraydash and handle it how you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (entry==arraypuzzle[i])
{ 
	if (arraydash[i] == entry)
	{
		cout << "Letter already inputted\n";
		count++;
	}
	else
	{
		right++;
		count++;
		arraydash[i]=entry;
	}
}


Also your program treats lowercase and uppercase differently, which I imagine you want them both to work. So you can just convert your character to either one.
Use code-tags and indent it properly.
Post a minimal example, not your whole program.
You could keep a std::vector<char> entries;
whenever someone enters their guess, you can check the vector (via std::find), if it already contains the entry. If yes, you can say so (e.g. via std::cout). If not, add it to the vector via push_back(). Little example:
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
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    using namespace std;
    cout << "Enter some letters:\n";

    vector<char> letters;
    char ch;
    while (cin >> ch) {
        // auto is a C++11 feature (keyword). The compiler will 'find' the correct type for you
        // it would be std::vector<char>::iterator
        auto iter = find(letters.begin(), letters.end(), ch);
         // if the element wasn't found, 
        // an iterator pointing at the one-past-last element is returned
        if (iter != letters.end())     // ch exists in 'letters'
            cout << "You already entered that letter.\n";
        else {
            cout << "hhmmm, I haven't seen that yet.\n";
            // add an element to the vector:
            letters.push_back(ch);
        }
    }
}

vector:
has 3 Elements.
Indexed from 0 to 2 (like arrays)
| 0 | 1 | 2 | < - - index

|---------------
| a | b | c |  |
|----------------
  ^             ^
begin() end()
Hope that helps...
Last edited on
@Callum5042
i am new next time i will use code tags
but if the WORD has a characted repeated in it then wat ?
i will not work then :/
@bugbyte
thanks mate but i am a begginer now i cant use these fancy funtions :v :'(
if it could be simple ...... :/
@FASI:
Yes, it could be simple, I could just rewrite the whole program and post it, but I'm not going to do that.
I feel the code I showed you was rather simple and understandable. You don't need to understand how vector or find work, you can just use them with their simple interface, that makes many parts of the standard lib so beautiful. Who says you can't use them? After a whole semester of C++, I would assume you have at least learned about std::vector, since it is probably on of, if not the most useful STL container(s).
I doubt anyone will come up with a simpler solution for this (ok, maybe like 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
#include <array>
#include <iostream>

int main()
{
    using namespace std;

    // 'true' if was entered, 'false' if not
    array<bool, 26> letters;    // bool letters[26];

    // there is a better way to do this, but it's simple
    for (std::size_t i = 0; i < letters.size(); ++i)
        letters[i] = false;

    cout << "Enter some letters:\n";
    char ch;
    while (cin >> ch) {
        if (ch < 'A' || ch > 'Z')
            cout << "Please enter capitalized letters only (A-Z).\n";
        else {
            if (letters[ch - 'A'])
                cout << "You already entered that.\n";
            else {
                letters[ch - 'A'] = true;
                cout << "hhhmm, I'll have to remember that.\n";
            }
        }
    }
}

Note: I have only used IO and array from the standard lib (which is like a plain C-Style array, but argubly better)

If you have trouble understanding this, try studying this output:
1
2
3
4
5
6
7
cout << "ASCII codes:\n";
cout << "A: " << int('A') << '\n';
cout << "Z: " << int('Z') << '\n';

// +1, because 'A' - 'A' is 0
cout << "A is the " << int('A' - 'A') + 1 << ". letter of the alphabet.\n";
cout << "Z is the " << int('Z' - 'A') + 1 << ". letter of the alphabet.\n";
Last edited on
@bugbyte
sadly i havent learned about std::vector
but the second code is making sence to me :D
and i havent used C
only c++
2nd code is easier to understand for me and i can also use it later :D
many many thanks to you for helping
i was my first post on this forum and its awesome :D
can u guide me how to use code tags ?
yes. when you push the reply button, you find a set of tiny buttons to the right of the text field. The have the header "Format:".
The very first one (<>) will insert code tags, which look like this:
[code][/code]
. In between these tags you shall paste your code.
Of course you can write them out manually, but I recommend using the button.
Glad I could help ;)
Last edited on
thanks again :D
Topic archived. No new replies allowed.