need help figureing out why my if statements are not working right

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Phillis2013
    New Member
    • Jan 2012
    • 14

    need help figureing out why my if statements are not working right

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctime>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	srand ( time(NULL) );
    	string face;
    	int suit,yes,r;
    	r = rand()% 10+4;
    	cout << r;
    	
    	cout << "hello welcome to a virtual card trick.\n";
    	cout << "what suit do you want to see it with?\n";
    	cout << "1 for spades\n 2 for clubs\n 3 for diamonds\n 4 for hearts\n";
    	cin >> suit;
    	switch(suit)
    	{
    	case(1):
    		cout << "you choose spades. do you want to use spades? 1 for yes\n 2 for no\n";
    		cin >> yes;
    		switch(yes)
    		{
    		case(1):
    /*if(r=1){
    face ="Ace";
    }
    if(r=11){
    face="Jack";
    }
    if(r=12){
    face="Queen";
    }
    if(r=13){
    face="King";
    }*/
    /*if(r=1)
    {
    cout << "your card is " << face;
    cout << "";
    }
    if(r>10){cout << "your card is " << face;
    cout << "";
    }*/
    			if(1<r<=10){
    			cout << "your card is " << r;
    			cout << "";
    			}
    
    		case(2):
    			cout << "start program over to try again\n";
    			system("pause");
    			return 0;
    		}
    	case(2):
    		cout << "you choose clubs. do you want to use clubs? 1 for yes\n 2 for no\n";
    		cin >> yes;
    		switch(yes)
    		{
    		case(1):
    				if((r=1)||(r>10))
    			{
    				cout << "your card is " << face;
    			cout << "";
    			}
    			else{
    			cout << "your card is " << r;
    			cout << "";
    			}
    		case(2):
    			cout << "start program over to try again\n";
    			system("pause");
    			return 0;
    		}
    	case(3):
    		cout << "you choose diamonds. do you want to use diamonds? 1 for yes\n 2 for no\n";
    		cin >> yes;
    		switch(yes)
    		{
    		case(1):
    				if((r=1)||(r>10))
    			{
    				cout << "your card is " << face;
    			cout << "";
    			}
    			else{
    			cout << "your card is " << r;
    			cout << "";
    			}
    		case(2):
    			cout << "start program over to try again\n";
    			system("pause");
    			return 0;
    		}
    	case(4):
    		cout << "you choose hearts. do you want to use hearts? 1 for yes\n 2 for no\n";
    		cin >> yes;
    		switch(yes)
    		{
    		case(1):
    				if((r=1)||(r>10))
    			{
    				cout << "your card is " << face;
    			cout << "";
    			}
    			else{
    			cout << "your card is " << r;
    			cout << "";
    			}
    		case(2):
    			cout << "start program over to try again\n";
    			system("pause");
    			return 0;
    		}
    		
    	}
    
    
    	
    }
    when i un comment the if statements in the first case it out puts the number in the if statment even when the r number is differant please help
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    if(r=11){
      face="Jack";
    assigns 11 to r and then tests that r is true. 11 is always true. Use the == operator to test equality.

    i
    Code:
    f(r==11){
      face="Jack";

    Comment

    • Phillis2013
      New Member
      • Jan 2012
      • 14

      #3
      thank you i noticed that 5 minutes ago

      Comment

      Working...