help with my card game

Write your question here.
how to write my code shorter by using function. i have tried to use function to reduce the size of my code, so that i don't have to repeat my code over and over.
please help!

#include <iostream>
#include <time.h>
using namespace std;

struct card //declare structure "card"
{
int number;
int suit;
};

int main()
{
card computer; //define structure variables
card user;
char again; //define variable of type char
cout << "Welcome to the Card Game!"<< endl; //display outputs to users
cout << "The highest card win!" << endl;
cout << "Suit order is: clubs, diamonds, hearts and spades." << endl;
do
{
srand(time(0));
computer.number = rand() % 13 + 2;
computer.suit = rand() % 4;
user.number = rand() % 13 + 2;
user.suit = rand() % 4;

//if computer card = user card
while ( computer.number == user.number && computer.suit == user.suit)
{
srand(time(0)); //randomly pick the cards again
computer.number = rand() % 13 + 2;
computer.suit = rand() % 4;
user.number = rand() % 13 + 2;
user.suit = rand() % 4;
}
//display computer card to monitor
cout << "The computer card is the ";
if (computer.number == 11) //card number 11 = Jack
cout << "Jack" << " of ";
else if (computer.number == 12) //card number 12 = Queen
cout << "Queen"<< " of ";
else if (computer.number == 13) //card number 13 = King
cout << "King" << " of ";
else if (computer.number == 14) //card number 14 = Ace
cout << "Ace" << " of ";
else if (computer.number < 11) //card number < 11 = that #
cout << computer.number << " of ";

if (computer.suit == 0) //card suit 0 = Clubs
cout << "Clubs" << endl;
else if (computer.suit == 1) //card suit 1 = Diamonds
cout << "Diamonds" << endl;
else if (computer.suit == 2) //card suit 2 = Hearts
cout << "Hearts" << endl ;
else if (computer.suit == 3) //card suit 3 = Spades
cout << "Spades" << endl;

//display user card to monitor
cout << "The user card is the ";
if (user.number == 11) //card number 11 = Jack
cout << "Jack" << " of ";
else if (user.number == 12) //card number 12 = Queen
cout << "Queen"<< " of ";
else if (user.number == 13) //card number 13 = King
cout << "King" << " of ";
else if (user.number == 14) //card number 14 = Ace
cout << "Ace" << " of ";
else if (user.number < 11) //card number < 11 = that #
cout << user.number << " of ";

if (user.suit == 0)
cout << "Clubs" << endl; //card suit 0 = Clubs
else if (user.suit == 1)
cout << "Diamonds" << endl; //card suit 1 = Diamonds
else if (user.suit == 2)
cout << "Hearts" << endl ; //card suit 2 = Hearts
else if (user.suit == 3)
cout << "Spades" << endl; //card suit 3 = Spades

//compare computer card and user card & display result to users
if (computer.number > user.number)
cout << "You lose!" << endl <<endl;
else if (computer.number < user.number)
cout << "You win!" << endl<< endl;
else if (computer.number == user.number)
{
if (computer.suit > user.suit)
cout << "You lose!" << endl<< endl;
else if (computer.suit < user.suit)
cout << "You win!" << endl<< endl;
}
cout << "Do you want to play again? (y/n): "; //ask users if they want to continue play
cin >> again;
cout <<endl;
}
while ( again == 'Y'|| again == 'y'); //if user enter "y" or "Y", play the game again

system ("pause");
return 0;
}
Last edited on
Your code does not compile.#include <cstdlib> and it is <ctime> not <time.h>
Topic archived. No new replies allowed.