0

I am trying to write a code for the game, rock-paper-scissors.

I recently just learned how to use a function to get user input and decided to try it here.

int getInput()
{
  int val;
  cin >> val; //only values from 0 to 2
  return val
}

int main() 
{
  int userChoice;
  while(userChoice = getInput()){//I get a warning here
   srand(time(NULL)); //always randon numbers
   int computer_choice = rand() % 3;
   switch(userChoice) {
      case:0
         //compare with computer_choice
         break;
      case:1
         //compare with computer_choice
         break;
      case:2
         //compare with computer_choice
         break;
    }
  }
}

Now, I have two questions: Firstly, is using userChoice = getInput() as a parameter for my While Loop good practice? I am skeptical because of the warning I get which is this:

suggest parentheses around assignment used as truth value

I believe this means, this parameter returns true. Using while(true) directly produces an infinite loop. I want to keep the game going until the user decides to quit. I could easily have used while(cin >> userChoice) but like I mentioned earlier, I am looking to apply what I recently learned or is this just a case where directly using while(cin >> userChoice) is more pragmatic?

Secondly, the loop breaks when the userChoice == 0 but other choices work. Is this because 0 returns false and so the while parameter is passed a False value and then breaks?

Thanks

Onome Sotu
  • 666
  • 9
  • 18
  • 4
    Loop breaks because 0 is interpreted as false. – KonstantinL Mar 20 '17 at 11:33
  • @KonstantinL, thanks, what about handling user input? what would you advice? – Onome Sotu Mar 20 '17 at 11:35
  • 1
    @OnomeSotu `while(userChoice = getInput())` is completely different from `while(cin >> userChoice)`, so choose whatever does what you want. First variant reads until you input `0`, while the other reads until `cin::operator bool` returns `false`. – Algirdas Preidžius Mar 20 '17 at 11:38
  • I'm closing this as a dupe since your second question has been asked and answered. Your first question is primarily opinion based and and is a matter of style and how exactly you want to the code to flow. – NathanOliver Mar 20 '17 at 11:39
  • @NathanOliver, I am learning. I wouldn't know what is opinion or style based except being told. Besides, I got a warning which prompted me to ask this question. If it was good practice, should I be getting a warning? Are there better options? – Onome Sotu Mar 20 '17 at 11:42
  • 1
    You get the warning because using `=` is normally a mistake. In this case it is not and does what you think. Typically you would rewrite it as `(userChoice = getInput()) == true` to express that you want to assign to a variable and test that it is true. As far as a better option goes I would `while(cin >> userChoice)` unless you need some special handling like needing to limit the user input. – NathanOliver Mar 20 '17 at 11:45
  • Thank you @NathanOliver. At least I understand why I get the warning. – Onome Sotu Mar 20 '17 at 11:50
  • Just for example: while( (userChoice = getInput()) <= 2 ){ - end the loop on an invalid input. – KonstantinL Mar 20 '17 at 12:16

0 Answers0