Why is my function continuing even after I give it wrong input?

I made up a game called password hacker in C++, purpose is to guess the password through given hints, when I input the correct password, it works correct, and moves to the new level as well.

But it does the same even when I input wrong password as well.

#include <iostream>

void Intro(int Level) {
  std::cout << "SUP, this PC is locked\n\n" << "well, sure why not give it a try.... it will all be over soon when you type the password incorrectly.\n" << "type your best code here to break security of server number " << Level;
}
bool PlayGame(int Diff) {
  Intro(Diff);

  int CodeA = 0;
  int CodeB = 1;
  int CodeC = 2;

  int CodeProduct = CodeA * CodeB * CodeC;
  int CodeSum = CodeA + CodeB + CodeC;
 
  std::cout << std::endl;

  //Instructions
  std::cout << "+ 3 number password" << "\n+The numbers adds up to " << CodeSum << "\n+The numbers multiply up to " << CodeProduct << std::endl;

  int PlayerGuessA;
  int PlayerGuessB;
  int PlayerGuessC;

  std::cin >> PlayerGuessA >> PlayerGuessB >> PlayerGuessC;

  int PlayerSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;
  int PlayerProduct = PlayerGuessA * PlayerGuessB * PlayerGuessC;
  std::cout << "You entered:\n" << PlayerGuessA << " " << PlayerGuessB << " " << PlayerGuessC;

  std::cout << "\n \n Your numbers multiply up to: " << PlayerProduct;
  std::cout << "\n Your numbers add up to: " << PlayerSum;

  if (PlayerSum != CodeSum && PlayerProduct != CodeProduct) {
    std::cout << std::endl << "Like i said earlier, its not usefull" << std::endl;
    return false;

  } else {
    std::cout << std::endl << "Well, arent you Sherlock " << std::endl;
    return true;
  }

}

int main() {
  int Lev = 1;
  while (true) {
    bool bLevelComplete = PlayGame(Lev);
    std::cin.clear(); //clears any errors
    std::cin.ignore(); //discards buffer
    ++Lev;
  }
  return 0;
} 

That isn’t implemented yet. Currently the loop unconditionally increments Lev

P.S. It’s a little weird to have a negated condition like this

  if (PlayerSum != CodeSum && PlayerProduct != CodeProduct) {
    std::cout << std::endl << "Like i said earlier, its not usefull" << std::endl;
    return false;

  } else {
    std::cout << std::endl << "Well, arent you Sherlock " << std::endl;
    return true;
  }

Instead of

  if (PlayerSum == CodeSum && PlayerProduct == CodeProduct) {
    std::cout << std::endl << "Well, arent you Sherlock " << std::endl;
    return true;

  } else {
    std::cout << std::endl << "Like i said earlier, its not usefull" << std::endl;
    return false;
  }

i know its weird to have negated condition, but desperate time required me to take desperate measures, lol. but i found out the error, thanks to you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.