I would like to make a program in which you have to guess the letters and when you guess all the letters, you have to guess the word. When I enter a wrong letter, the word "wrong" is constantly displayed on the screen.

xayanooo 0 Reputation points
2024-04-18T18:53:58.11+00:00

#include <iostream>

#include <windows.h>

using namespace std;

int main()

{

int i = 3;

int b;

char a;

string c;

system("cls");

int d;

cout << "Choose game:" << endl << "1. Zgadnij wyraz" << endl << "2. Melonierzy" << endl << "3.Uno" << endl;

cin >> d;

if (d == 1)

{

    cin >> a;

    while (a != 'd' || a != 'o' || a != 'm')

    {

        if (a == 'd' || a == 'o' || a == 'm')

        {

            cout << "you guessed letter";

            i--;

            if (i == 0)

            {

                cout << "guessed letters form a word";

                cin >> c;

                if (c == "dom")

                {

                    cout << "you won";

                    break;

                }

                else

                    cout << "wrong word";

                break;

            }

            cin >> a;

        }

        else

        {

            cout << "wrong "<<endl;

            

        }

    }

    cin >> a;

}

return 0;

}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,532 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Barry Schwarz 2,111 Reputation points
    2024-04-19T06:41:11.48+00:00

    The problem is with your while statement. It always evaluates to 1. If a == 'd', the first inequality is false but the second is true so the result is 1. In any other case, the first inequality is true so the result is 1. Thus the result is always 1. I'm pretty sure that you meant to use &&, not ||.

    As a result of this error, when an incorrect letter is input, the if statement evaluates to 0 and you drop down to the else which prints "wrong". And then you reach the bottom of the while without ever changing a. So the process repeats with the old value of a and you get the same result again. And again...

    0 comments No comments