Looping Question
Oct 12, 2014 at 9:09am UTC
My question is: How would I just loop the output of (cardB == cardA)? Let's say for example, if the outcome was a tie, hence cardB == cardA, how would I add a loop so that it'll generate a new outcome?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
srand(time(0));
//variables
int cardA = 2 + (rand() % 13);
int cardB = 2 + (rand() % 13);
int cardAsuit = (rand() % 4);
int cardBsuit = (rand() % 4);
string cardAsuit2;
string cardBsuit2;
if (cardAsuit == 0) cardAsuit2 = "Clubs" ;
if (cardAsuit == 1) cardAsuit2 = "Spades" ;
if (cardAsuit == 2) cardAsuit2 = "Hearts" ;
if (cardAsuit == 3) cardAsuit2 = "Diamonds" ;
if (cardBsuit == 0) cardBsuit2 = "Clubs" ;
if (cardBsuit == 1) cardBsuit2 = "Spades" ;
if (cardBsuit == 2) cardBsuit2 = "Hearts" ;
if (cardBsuit == 3) cardBsuit2 = "Diamonds" ;
if (cardA >= 2 && cardA <= 10)
cout << "Computer's card is a " << cardA << " of " << cardAsuit2 << endl;
if (cardA == 11)
cout << "Computer's card is a Jack of " << cardAsuit2 << endl;
if (cardA == 12)
cout << "Computer's card is a Queen of " << cardAsuit2 << endl;
if (cardA == 13)
cout << "Computer's card is a King of " << cardAsuit2 << endl;
if (cardA == 14)
cout << "Computer's card is an Ace of " << cardAsuit2 << endl;
if (cardB >= 2 && cardB <= 10)
cout << "Humans card is a " << cardB << " of " << cardBsuit2 << endl;
if (cardB == 11)
cout << "Human's card is a Jack of " << cardBsuit2 << endl;
if (cardB == 12)
cout << "Human's card is a Queen of " << cardBsuit2 << endl;
if (cardB == 13)
cout << "Human's card is a King of " << cardBsuit2 << endl;
if (cardB == 14)
cout << "Human's card is an Ace of " << cardBsuit2 << endl;
if (cardA > cardB)
cout << endl << "Computer wins!" << endl;
if (cardB > cardA)
cout << endl << "Human wins!" << endl;
if (cardB == cardA)
cout << endl << "It's a tie!" << endl << "Let's go again!" << endl;
return 0;
}
Oct 12, 2014 at 3:20pm UTC
All you have to do in this is to add a "do" loop around the code you wish to repeat. In my code I've also added "cin.ignore" so that the user has to press "enter" before another outcome is generated.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
srand(time(0));
do {
//variables
int cardA = 2 + (rand() % 13);
int cardB = 2 + (rand() % 13);
int cardAsuit = (rand() % 4);
int cardBsuit = (rand() % 4);
string cardAsuit2;
string cardBsuit2;
if (cardAsuit == 0) cardAsuit2 = "Clubs" ;
if (cardAsuit == 1) cardAsuit2 = "Spades" ;
if (cardAsuit == 2) cardAsuit2 = "Hearts" ;
if (cardAsuit == 3) cardAsuit2 = "Diamonds" ;
if (cardBsuit == 0) cardBsuit2 = "Clubs" ;
if (cardBsuit == 1) cardBsuit2 = "Spades" ;
if (cardBsuit == 2) cardBsuit2 = "Hearts" ;
if (cardBsuit == 3) cardBsuit2 = "Diamonds" ;
if (cardA >= 2 && cardA <= 10)
cout << "Computer's card is a " << cardA << " of " << cardAsuit2 << endl;
if (cardA == 11)
cout << "Computer's card is a Jack of " << cardAsuit2 << endl;
if (cardA == 12)
cout << "Computer's card is a Queen of " << cardAsuit2 << endl;
if (cardA == 13)
cout << "Computer's card is a King of " << cardAsuit2 << endl;
if (cardA == 14)
cout << "Computer's card is an Ace of " << cardAsuit2 << endl;
if (cardB >= 2 && cardB <= 10)
cout << "Humans card is a " << cardB << " of " << cardBsuit2 << endl;
if (cardB == 11)
cout << "Human's card is a Jack of " << cardBsuit2 << endl;
if (cardB == 12)
cout << "Human's card is a Queen of " << cardBsuit2 << endl;
if (cardB == 13)
cout << "Human's card is a King of " << cardBsuit2 << endl;
if (cardB == 14)
cout << "Human's card is an Ace of " << cardBsuit2 << endl;
if (cardA > cardB)
cout << endl << "Computer wins!" << endl;
if (cardB > cardA)
cout << endl << "Human wins!" << endl;
if (cardB == cardA)
cout << endl << "It's a tie!" << endl << "Let's go again!" << endl;
cin.ignore(); // press enter for the next outcome
} while (true ); // infinitely repeats
return 0;
}
Hope this helps!
Topic archived. No new replies allowed.