C ++ Although loop completion with function

scratching my head since it works fine, but when I went to add some other functions, suddenly my program got scared and I can’t get it back to what it was.

the class makes me write a program for rock / paper / scissors to stand up to the computer, any help on why the loop keeps ending will be great

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


void RPSout(char);
int RPScomp();

int main() {

    char choice;
    int endit=0;

    while (endit == 0)
    {
        cout << "\n\n\tReady to play Rock/Paper/Scissors against the computer??(please choose R/P/S)(Q to quit)\n";
        cin >> choice;

        RPSout(choice);

        if (choice=='Q'||'q')
            {endit=1;}
    }
    return 0;
}


void RPSout(char choose)
{
    int RPS =0;
    int comp=0;
    switch (choose)
    {
    case 'R':
    case 'r':
    {
        cout <<"Your choice: Rock";
        break;
    }
    case 'P':
    case 'p':
    {
        cout <<"Your choice: Paper";
        break;
    }

    case 'S':
    case 's':
    {
        cout << "Your choice: Scissors";
        break;
    }

    case 'Q':
    case 'q':
    {
        cout << "Bye Bye Bye";
        break;
    }

    default:
        cout<<"You enter nothing!"<<endl;
        cout << "The valid choices are R/P/S/Q)";
    }
    return;
}

int RPScomp()
{
int comp=0;
const int MIN_VALUE =1;
const int MAX_VALUE =3;
    unsigned seed = time(0);

    srand(seed);

    comp =(rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE;
    return comp;
}
+4
source share
4 answers
if (choice=='Q'||'q')

It is equivalent

if ((choice == 'Q') || 'q')

, . 'q' char, "", . if (choice == 'Q' || true).

:

if (choice=='Q' || choice=='q')
+5

if (choice=='Q'||'q')

true , .

Try:

if (choice=='Q'||choice=='q')
+2

I think your if statement should be if (choice=='Q'|| choice=='q')

+1
source

Your problem if using the if statement

if (choice=='Q'||'q')
        {endit=1;}

|| "q" will always be true since "q" in ASCII is not 0 Change your code to

if (choice=='Q'|| choice=='q')
        {endit=1;}
0
source

Source: https://habr.com/ru/post/1675311/


All Articles