| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Random Number problem!
Ok so basically we have to create a program that "deals" the user and the computer 5 cards each. It then declares the winner based on the one with the highest card.example output: Your cards are: 3 14 K 5 9 The computer's cards are: 5 J 10 2 J You have the single highest card value. you won! Any help would be very greatly appreciated! ![]() This is the code I have so far: //High Card Game #include <iostream> #include <ctime> using namespace std; int main () { srand(time(0)); int user, computer; int counter = 0, high = 0; while (counter < 5) { user = rand()%10+4; if (user > high) { high = user; } counter++; } while (counter < 5) { computer = rand()%10+4; if (user > high) { high = user; } if (computer > high) { high = computer; } counter++; } if (user = 11) { user = 'J'; } if (user = 12) { user = 'Q'; } if (user = 13) { user = 'K'; } if (user = 14) { user = 'A'; } if (computer = 11) { computer = 'J'; } if (computer = 12) { computer = 'Q'; } if (computer = 13) { computer = 'K'; } if (computer = 14) { computer = 'A'; } cout<<"Your cards are:"<<user<<user<<user<<user<<user<<endl; cout<<"The computer's cards are:"<<computer<<computer<<computer<<computer<<computer<<endl; if (computer > user) { cout<<"The computer has the highest single card."<<endl; } else if (user > computer) { cout<<"You have the highest single card."<<endl; } else if (user = computer) { cout<<"You and the computer are tied fot the highest single card."<<endl; } return 0; } |
|
#2
|
||||
|
||||
|
First, I recommend you separate your "how to display this card" into a function of its own. I suggest the function signature string display(int);
Then, you should be using several variables, preferrably in a vector or array, to store the card values. vector<int> userhand(5); for (int i=0; i<5; i++) userhand[i]=rand()...
__________________
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Random Number problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|