I'm trying to make a blackjack game but I'm having a little trouble with a do/while loop.
Maybe a do/while loop isn't the right kind of loop to be using? I dunno, you tell me.
Here's the code:

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;
int number;
char ans;
char Ans;
int rand();  //number between 1 and 10;

int main(int argc, char *argv[])
{

    cout << "Welcome to Blackjack! \n";
    cout << "\n"; // these are for double spacing.
    system ("pause ");
    cout << "\n";

    srand(time(NULL));
    cout << "You've been dealt a(n) " << rand() % 10 + 2 <<"\n";
    cout << "\n";   
    cout << "To hit, press 1, to stay press 2. \n";
    cout << "\n";
    cin >> ans; cout <<"\n";
        if(ans=='1'){
             cout << "You're now at a(n) " << rand() % 10 + 1 + rand() % 10 + 2 << "\n";
             cout <<"\n";
              if(rand()==21){
                 cout <<"BLACKJACK!\n";
                 cout << "\n";
                 cout <<" Do you want to play again?\n";
                 cout << "\n";
                 cin >> Ans;
                 }
             }

I need to get the program to start over if they choose yes after getting a blackjack. Any ideas?

Recommended Answers

All 4 Replies

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
   char Ans;

   cout << "Keep Going? ";
   cin >> Ans;
   if(Ans=='y' || Ans=='y') main(argc, argv);
}

An idea, anyway. Otherwise, a do-while will work fine.

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
   char Ans;

   do
   {
   cout << "Keep Going? ";
   cin >> Ans;
   } while (Ans=='y' || Ans=='y');
}

The first didn't work, I'll try the second in just a sec. I'm new to c++ so just wondering, what does || do?

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
   char Ans;

   cout << "Keep Going? ";
   cin >> Ans;
   if(Ans=='y' || Ans=='y') main(argc, argv);
}

An idea, anyway. Otherwise, a do-while will work fine.

Recursion with main() is rarely a good idea, in fact if I recall right (which I might not be) it's illegal according to the C++ standard.

The first didn't work, I'll try the second in just a sec. I'm new to c++ so just wondering, what does || do?

|| is logical OR so in otherwords it's saying "If condition 1 is true or condition 2 is true, execute again" instead of trying to explain it properly myself however I'll just point you to http://www.cplusplus.com/doc/tutorial/operators/ which has basically what you need to know on operators.

Ah I see. Well the second one worked. I just wasn't putting the do in the right place before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.