Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

code ignoring if then statement (c++)

+3 votes
asked Sep 24, 2020 by Ike Roland (320 points)

So I am writing a 'simple' childish code that will determine if the player or the computer will go first.



int main()
    std::cout<<"announcer:\"today we are going to witness a roast battle bettween ZAK AND...\""<<std::endl;
    std::cout<<"announcer **whispers**:\"wait what was your name again?"<<std::endl;
    std::string player;
    std::cout<<"announcer:\"ZAK AND "<<player<<"!!!!!!!!\""<<std::endl;
    std::cout<<"          \"now we need to decide who will go first,reff do your stuff.\""<<std::endl;
    std::cout<<"reff:\"choose heads or tails if you think heads type 1 if tails the 2\""<<std::endl;
    int num1;
  int flip =rand() % 3+ 1 ;

//to show me what the number was
  if(num1 = flip ){
    if(num1 = 1){
        std::cout<<"it was heads so "<<player<<" will go first"<<std::endl;
        std::cout<<"it was tails so "<<player<< " will go first"<<std::endl;
      if(num1 = 1){
          std::cout<<"it was tails so zak will go first"<<std::endl;
          std::cout<<"it was heads so zak will go first"<<std::endl;



but whenever i get to the if then statement at the bottom of the code it will output "it was head so player will go first"

regardless if the player chose 2 plz help!!!

2 Answers

0 votes
answered Sep 24, 2020 by LiOS (6,420 points)
selected Sep 28, 2020 by Ike Roland
Best answer
The if statements have assignment (=) operator rather than equal to (==) operator, which when tested caused issues.

You would want if(num1 == flip ){ instead of if(num1 = flip ){
commented Sep 25, 2020 by Peter Minarik (88,130 points)
Just a bit of addition to the *why*.

In C (and C++) a condition evaluates to false, when its numeric value is 0. It's true in any other cases.

"if (num = flip)" or "if (num1 = 1)" is not comparison operation, but assignment, which will leave "num" with the value of "flip" or 1. After this, the condition is this: "if (num)", so if "num" got assigned a non-zero value, then the condition evaluates to true.
+1 vote
answered Sep 25, 2020 by Peter Minarik (88,130 points)

 LiOS has answered how to solve the problem.

I'd like to point out something else: the coin flip.

The coin flip can have 2 results (normally XD): head or tail.

To simulate this, you'd want something like this:

int flip = rand() % 2;

This above can have two outcomes: 0 or 1;

Your version

int flip = rand() % 3 + 1;

, however can have 3 outcomes: 1, 2, 3. If that was your intention, fine. If not, use the modulo 2 instead. ;)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.