loop help

This is a discussion on loop help within the C++ Programming forums, part of the General Programming Boards category; hello im writing a blackjack program and i would like to condense some of it. i was wondering if there ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    loop help

    hello
    im writing a blackjack program and i would like to condense some of it. i was wondering if there was any way to make this into a loop?

    Code:
    cout<<"how many cards do you have? \n";
    		cin>>repeatcard;
    
    		
    		if (repeatcard=3 && total<21){
    			cout<<"card: "<<number<<endl;
    			cout<<"enter the card? \n";
    			cin>>card3;
    			total=card1+card2+card3;
    			cout<<" 3 cards, card total: "<<total<<endl;
    			cout<<"do you want another card? ";
    			cin>>anothercard;
    			}
    		if(repeatcard=4 && total<21){
    				cout<<"card: "<<number<<endl;
    				cout<<"enter the card? \n";
    				cin>>card4;
    				total=card1+card2+card3+card4;
    				cout<<"4 cards, card total: "<<total<<endl;
    				cout<<"do you want another card? ";
    				cin>>anothercard;
    				}
    		if (repeatcard=5 &&  total<21){
    				cout<<"card: "<<number<<endl;
    				cout<<"enter the card? \n";
    				cin>>card5;
    				total=card1+card2+card3+card4+card5;
    				cout<<"5 cards, card total: "<<total<<endl;
    				cout<<"do you want another card? ";
    				cin>>anothercard;

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Firstly, your code technically shouldn't work (based on what's here), although a typo has saved it. The variable anothercard isn't related in any way to repeatcard, so based on your model it won't be able to increment. What has happened is that you typed = instead of == in the if() statements, and that is assigning values instead of checking them. Also, number doesn't change, so the person will draw the same card over and over.

    You have virtually the same code repeated three times. All it takes to make a loop is cut out two of them and replace any specific code with formulaic code, so that it can be automated. Let's look at how we'd do that:

    The conditions are obviously that the player wants another card and that he hasn't yet busted. You could write this very similarly in the conditional for a do...while() loop. We'll use a do...while() because that forces it to run at least once.

    Code:
    do { //loop stuff here
    } while ( repeatcard == 'y' && total < 21 )
    Now we need to ask the player if he wants another card, and input his response. To check what he input, we need an if() statement. If he says yes (or 'y'), we'll execute the card-generating code, and if he says no, we won't. We'll use the same variable that's tested in the loop, so that we have a way to escape the loop. We don't need to use a break statement, because the loop will exit with a response of 'n'. This segment would look like this:

    Code:
    cout << "Would you like another card? Y/n ";
    cin >> repeatcard;
    if ( repeatcard == 'y' )
     { //Generate card here }
    The card generation can be automated and really streamlined in comparison to your current code. You would have to initialize numCards outside the loop, and probably set it to two for starters. Consider this:

    Code:
    numCards++;
    newCard = (rand() % 11) + 1;
    total += newCard; //this is the same as total = total + newCard
    cout << "You drew a " << newCard << ". Your hand is worth " << total << endl;
    If you put all that together, you'd have a loop that cuts through all the unneeded part of your code. Of course, you need to make sure to integrate it with the rest of the program, but it is definitely a good jumping point. Tell us how it turns out!

  3. #3
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    this is what i got so far
    Code:
    if(total<21)
    {							
    		do
    		{
    			cout<<"do you want another card? <Y or N>? \n";
    			cin>>anothercard;
    			cout<<"how many cards do you have? \n";
    			cin>>repeatcard;
    				number = 1 + (rand() % 13);     //card generator
    				cout<<"card: "<<endl;
    				if(number==1){	
    				cout<<" A ";
    				a=11;}
    				else if(number>=2 && number<=9)
    				cout <<" "<<number<<" ";
    				else{
    					if(number=10)
    						cout<<" 10 ";
    					else if(number== 11){
    						cout<<" J ";
    						j=10;}
    					else if(number==12){
    						cout<<" Q ";
    						q=10;}
    					else {
    						cout<<" K ";
    						k=10;}
    					}		//endelse
    
    			cout<<"enter the card:  ";
    			cin>>newcard;
    			total+=newcard;
    			cout<<"total: "<<total<<endl;
    					if(total<21){
    					cout<<"do you want another card? <Y or N>:  ";
    					cin>>anothercard;}
    		}while( total<21 && repeatcard=='y' || repeatcard=='Y');			//end do loop
    
    	
    }
    also i dont think my card generator is right. for a,j,q,k should they be int or char?

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    I think face cards should be output as chars, like in the game.

    Code:
    if(number=10)
    cout<<" 10 ";
    you need another = sign in the if statement

    Code:
    while( total<21 && repeatcard=='y' || repeatcard=='Y');
    I would add nested parentheses around the repeatcard bit here.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    thats what i thought to, but when i input a facecard(j,q,k,a) my program crashes, i mean the code seems logical enough.
    also, is there a way to like save the card generator code as some function or something and then when i want to all i have to do recall the function.

    thanks
    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    /*
    	Randall Foor
    	Blackjack program
    	10/6/10
    
    */
    
    int main(){
    	int number,repeatcard,newcard;
    	int card1,card2,total;
    	char repeat,anothercard,j,q,k,a;
    	
    
    	for(int x=10;x>0;x++)			//generator for first 2 cards
    {
    	srand(time(0));
    	cout<<"cards:";
    	for(int nextcard=2;nextcard>0;nextcard--){
    
    
    		number = 1 + (rand() % 13) ; //generates nums 1-13
    
    		if(number==1){		//facecards generator
    			cout<<" A ";
    			a=11;
    			}
    		else if(number>=2 && number<=9)
    		cout <<" "<<number<<" ";
    		else{
    			if(number==10)
    				cout<<" 10 ";
    			else if(number== 11){
    				cout<<" J ";
    				j=10;
    				}
    			else if(number==12){
    				cout<<" Q ";
    				q=10;
    				}
    			else {
    				cout<<" K ";
    				k=10;
    				}
    			}		//endelse
    	}		//endloop
    
    
    cout<<"how many cards do you have? \n";
    cin>>repeatcard;
    
    
    cout<<"enter the cards \n";
    		cin>>card1>>card2;
    
    total=card1+card2;
    		cout<<"total "<<total<<endl;
    
    if(total<21)
    {							
    		do
    		{
    			cout<<"do you want another card? <Y or N>? \n";
    			cin>>anothercard;
    			cout<<"how many cards do you have? \n";
    			cin>>repeatcard;
    				number = 1 + (rand() % 13);
    				cout<<"card: "<<endl;
    				if(number==1){	
    				cout<<" A ";
    				a=11;}
    				else if(number>=2 && number<=9)
    				cout <<" "<<number<<" ";
    				else{
    					if(number==10)
    						cout<<" 10 ";
    					else if(number== 11){
    						cout<<" J ";
    						j=10;}
    					else if(number==12){
    						cout<<" Q ";
    						q=10;}
    					else {
    						cout<<" K ";
    						k=10;}
    					}		//endelse
    
    			cout<<"enter the card:  ";
    			cin>>newcard;
    			total+=newcard;
    			cout<<"total: "<<total<<endl;
    					if(total<21){
    					cout<<"do you want another card? <Y or N>:  ";
    					cin>>anothercard;
    					}
    		}while( total<21 && (repeatcard=='y' || repeatcard=='Y'));			//end do loop
    
    	
    }	
    
    if(total==21)								//if21 you win
    			cout<<"you win \n";
    if(total>21)								//if higherthan 21 lose
    			cout<<"busted \n";
    
    if (anothercard=='n' || anothercard=='N')				//if you dont want another card
    		break;
    		
    
    
    
    cout<<"\n Do you want to repeat? <Y or N> \n";
    	cin>>repeat;
    
    	if(repeat=='y' || repeat=='Y')
    	{		
    		system("cls");
    	}
    
    	if(repeat=='n' || repeat=='N')
    	{	
    		system("cls");
    		break;
    	}
    }
    
    
    
    
    	system("pause");
    		return 0;
    }

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    You can generate your deck of cards, store them in an array[] and then "shuffle them", this is then your deck ready to play.

    there are various ways of shuffling, a very simple one is to use two arrays, one is filled with all the cards in order, then you just pick an array index randomly -(and if it is not empty)- swap the contents into your other array to contain the shuffled deck, you need to then fill the picking array with a 0 or other flag to show that card has already been chosen and is now empty, so ignore and pick again.

    As for your face cards issue, what is the problem? You only need to be concerned with showing the user a char, internally you can just use numbers, i wont tell anybody if you dont ;->
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    As for your face cards issue, what is the problem? You only need to be concerned with showing the user a char, internally you can just use numbers, i wont tell anybody if you dont ;->
    im fine with the output of the face cards to the user. when the user inputs the face card the program crashes. i can input numbers fine, it calculates the total fine, and outputs the right total. i think my problem is that im not assigning the facecards right.

    my variables
    Code:
    int number,repeatcard,newcard;
    int card1,card2,total;
    char repeat,anothercard,j,q,k,a;
    the card generator
    Code:
    number = 1 + (rand() % 13);
    				cout<<"card: "<<endl;
    				if(number==1){	
    				cout<<" A ";
    				a=11;}
    				else if(number>=2 && number<=9)
    				cout <<" "<<number<<" ";
    				else{
    					if(number==10)
    						cout<<" 10 ";
    					else if(number== 11){
    						cout<<" J ";
    						j=10;}
    					else if(number==12){
    						cout<<" Q ";
    						q=10;}
    					else {
    						cout<<" K ";
    						k=10;}
    					}		//endelse
    how the user enters the cards
    Code:
    cout<<"enter the cards \n";                 //for the first 2 cards that are generated 
    		cin>>card1>>card2;
    total=card1+card2;                             //calculates the total of the 2 cards
    		cout<<"total "<<total<<endl;
    
    
    //more code 
    
    
    cout<<"enter the card:  ";                  //if the user wants anothercard, the card is generated
    cin>>newcard;
    total+=newcard;
    cout<<"total: "<<total<<endl;

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    You don't actually need to declare any a q j k chars in your version, there is no need for you to try anything like j = 10, (which makes no sense as it is a char anyhow)

    if number == 11 then output a jack
    you can then just allow an integer for value = 10

    this sort of card thing is really better done using structs or classes with members for suit, value, etc
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    If you want to put the card generation code in its own function, that's pretty simple and really a good way to do it, since the code gets reused. Always try to avoid repeating code! The more times you type something, the more likely you are to make mistakes. If you don't understand functions, check out this site's function tutorial here.

    Code:
    int drawCard() {
        int draw = 1 + ( rand() % 13);
        return draw;
    }
    The program crashes with letter input because newcard is an <int>, and you are trying to feed it characters. Integer variables cannot handle character input, they will universally crash. But why are you asking the player to input the produced card, anyhow? Is there a reason? If not, just let the computer figure out what the card is for you, like this:

    Code:
    newcard = drawCard();
    cout << "You drew a "; //Don't use endl here! By leaving it open we can use the next part
    
    if (newcard == 1) //This code prints the value based on newcard without using a new variable
        cout << "A\n"; 
    else if (newcard > 1 && newcard < 11)
       cout << newcard << endl;
    else if (newcard == 11)
       cout << "J\n";
    else if (newcard == 12)
       cout << "Q\n";
    else if (newcard == 13)
       cout << "K\n";
    
    total += newcard;
    
    cout << "Now your hand is worth " << total << endl;

  10. #10
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    i want to input a letter/facecard ex. k,q,j,a when it asks for the card generated,and not have my program crash. it will do everything but accept letters for the input of a card. if found out why it wouldnt continue the do loop if i wanted another card, it should have been
    Code:
    while( total<21 && (anothercard=='y' || anothercard=='Y') && repeatcard<4)
    
    //instead of 
    
    while( total<21 && (repeatcard=='y' || repeatcard=='Y'));

    i havent used structs or classes yet, sorry if i seem pushy or annoying, im kinda new to c++.

    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    /*
    	Randall Foor
    	Blackjack program
    	10/6/10
    
    */
    
    int main(){
    	int number,repeatcard,newcard;
    	int card1,card2,total;
    	char repeat,anothercard;
    	
    
    	for(int x=10;x>0;x++)			//generator for first 2 cards
    {
    	srand(time(0));
    	cout<<"cards:";
    	for(int nextcard=2;nextcard>0;nextcard--){
    
    
    		number = 1 + (rand() % 13) ; //generates nums 1-13
    
    		if(number==1)	//facecards generator
    			cout<<" A ";
    		else if(number>=2 && number<=9)
    		cout <<" "<<number<<" ";
    		else{
    			if(number==10)
    				cout<<" 10 ";
    			else if(number== 11)
    				cout<<" J ";
    			else if(number==12)
    				cout<<" Q ";
    			else 
    				cout<<" K ";
    			}		//endelse
    	}		//endloop
    
    
    cout<<"how many cards do you have? \n";
    cin>>repeatcard;
    
    
    cout<<"enter the cards \n";
    		cin>>card1>>card2;
    
    	if(card1=='j' || card1=='q'|| card1=='k')
    		total=10+card2;
    	if(card2=='j' || card2=='q' || card2=='k')
    		total=card1+10;
    	if(card1=='a')
    		total=11+card2;
    	if(card2=='a')
    		total= card1+11;
    	else 
    		total=card1+card2;
    		cout<<"total "<<total<<endl;
    
    if(total<21)
    {							
    		do
    		{
    			cout<<"do you want another card? <Y or N>? \n";
    			cin>>anothercard;
    			cout<<"how many cards do you have? \n";
    			cin>>repeatcard;
    				number = 1 + (rand() % 13);
    				cout<<"card: "<<endl;
    				if(number==1){	
    				cout<<" A ";
    				}
    				else if(number>=2 && number<=9)
    				cout <<" "<<number<<" ";
    				else{
    					if(number==10)
    						cout<<" 10 ";
    					else if(number== 11)
    						cout<<" J ";
    					else if(number==12)
    						cout<<" Q ";
    					else 
    						cout<<" K ";
    						
    					}		//endelse
    
    			cout<<"enter the card:  ";
    			cin>>newcard;
    			total+=newcard;
    			cout<<"total: "<<total<<endl;
    
    				if(total<21){
    				cout<<"do you want another card? <Y or N>:  ";
    				cin>>anothercard;
    				cout<<"how many cards do you have: \n";
    				cin>>repeatcard;
    
    					number = 1 + (rand() % 13);
    					cout<<"card: "<<endl;
    					if(number==1){	
    					cout<<" A ";
    					}
    					else if(number>=2 && number<=9)
    					cout <<" "<<number<<" ";
    					else{
    						if(number==10)
    							cout<<" 10 ";
    						else if(number== 11)
    							cout<<" J ";
    						else if(number==12)
    							cout<<" Q ";
    						else 
    							cout<<" K ";
    							
    						}		//endelse
    
    					cout<<"enter the card:  ";
    					cin>>newcard;
    					total+=newcard;
    					cout<<"total: "<<total<<endl;
    
    				}			//end if
    		}while( total<21 && (anothercard=='y' || anothercard=='Y') && repeatcard<4);			//end do loop
    
    	
    }	
    
    if(total==21)								//if21 you win
    			cout<<"you win \n";
    if(total>21)								//if higherthan 21 lose
    			cout<<"busted \n";
    
    if (anothercard=='n' || anothercard=='N')				//if you dont want another card
    		break;
    		
    
    
    
    cout<<"\n Do you want to repeat? <Y or N> \n";
    	cin>>repeat;
    
    	if(repeat=='y' || repeat=='Y')
    	{		
    		system("cls");
    	}
    
    	if(repeat=='n' || repeat=='N')
    	{	
    		system("cls");
    		break;
    	}
    }
    
    
    
    
    	system("pause");
    		return 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    The method you want to use will not work. You cannot, for example, assign "10" to a <char>, nor can you assign "J" to an <int>. So your code:

    Code:
    cout<<"enter the cards \n";
            cin>>card1>>card2;
    
        if(card1=='j' || card1=='q'|| card1=='k')
            total=10+card2;
        if(card2=='j' || card2=='q' || card2=='k')
            total=card1+10;
    will not work, because card1 cannot hold a value 'J'. The issue is that you are completely misusing variable types. This is essentially what you're trying to do:
    - When the computer looks for an integer, you give it a letter.
    - When you check if the first integer is a character (which can't happen), you assign total to 10 + a letter
    - When you check if the second integer is a character, you assign total to a letter + 10.

    Maybe you have a little experience with another programming language. Python, as an example, basically lets you do whatever the hell you want with variables. This code might actually run were it translated into Python. But in C++, variables are one variable type and they stay that variable type. There are limitations on their use. If you don't know C++ variables very well, hop on over to the tutorial here. Using variables is at the very core of any non-trivial program, so you absolutely must know how to use them by heart.

  12. #12
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    its alot of code but this is my program so far.
    i got everything to work except the aces for when the user goes over 21.
    i was thinking that if the user inputs an ace it would count how many were put in total, and at the end if the user was over 21 then subtract 10 for each ace,but im not sure on how to do that.
    do you think that this would work?
    Code:
    //after the user inputs the cards
    if(card1char=='a' || cardchar2=='a'){
    aces=aces+1;
    value=0;
    }
    //if the user enters more aces later
    if(newcardchar=='a' || newcardchar=='A')
    ace++
    //to calculate the total
    if(//newcardchar or card1char or card2char=='a' or'A')
    total+=newcard-(aces*10)
    my entire program so far
    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    /*
    	Randall Foor
    	Blackjack program
    	10/6/10
    
    */
    
    int main(){
    	int number,repeatcard,newcard;
    	int card1,card2,total;
    	char repeat,anothercard,card1char,card2char,newcardchar;
    	
    	
    
    	
    
    	for(int x=10;x>0;x++)			//generator for first 2 cards
    {
    	srand(time(0));
    	cout<<"cards:";
    	for(int nextcard=2;nextcard>0;nextcard--){
    
    
    		number = 1 + (rand() % 13) ; //generates nums 1-13
    
    		if(number==1)	//facecards generator
    			cout<<" A ";
    		else if(number>=2 && number<=9)
    		cout <<" "<<number<<" ";
    		else{
    			if(number==10)
    				cout<<" 10 ";
    			else if(number== 11)
    				cout<<" J ";
    			else if(number==12)
    				cout<<" Q ";
    			else 
    				cout<<" K ";
    			}		//endelse
    	}		//endloop
    
    
    cout<<"\n how many cards do you have? \n";
    cin>>repeatcard;
    
    
    cout<<"enter the cards <A,2-9,T,J,Q,K> \n";
    		cin>>card1char>>card2char;
    
    
    				if(card1char=='a'||card1char=='A')
    				card1=11;
    				if(card2char=='a'||card2char=='A')
    				card2=11;
    				if(card1char=='2') 
    				card1=2;
    				if(card2char=='2')
    				card2=2;
    				if(card1char=='3')
    				card1=3;
    				if(card2char=='3')
    				card2=3;
    				if(card1char=='4')
    				card1=4;
    				if(card2char=='4')
    				card2=4;
    				if(card1char=='5')
    				card1=5;
    				if(card2char=='5')
    				card2=5;
    				if(card1char=='6')
    				card1=6;
    				if(card2char=='6')
    				card2=6;
    				if(card1char=='7')
    				card1=7;
    				if(card2char=='7')
    				card2=7;
    				if(card1char=='8')
    				card1=8;
    				if(card2char=='8')
    				card2=8;
    				if(card1char=='9')
    				card1=9;
    				if(card2char=='9')
    				card2=9;
    				if((card1char=='j'||card1char=='J') || (card1char=='q'||card1char=='Q') || (card1char=='k'||card1char=='K')|| (card1char=='t'||card1char=='T'))
    				card1=10;
    				if((card2char=='j'||card2char=='J') || (card2char=='q'||card2char=='Q')|| (card2char=='k'||card2char=='K')|| (card2char=='t'||card2char=='T'))
    				card2=10;
    
    
    
    		total=card1+card2;
    		cout<<"total "<<total<<endl;
    		if(total>21 && (card1char=='a' || card2char=='a')){			//if total is more than 21 and you have an ace, subtract 10 for each ace
    					total=total-10;
    					cout<<"new total: "<<total<<endl;}
    
    if(total<21)
    {							
    		do
    		{
    			cout<<"do you want another card? <Y or N>? \n";
    			cin>>anothercard;
    			cout<<"how many cards do you have? \n";
    			cin>>repeatcard;
    			if(repeatcard==5){
    				cout<<"you cant have that many cards";
    					break;}
    
    				number = 1 + (rand() % 13);
    				cout<<"card: "<<endl;
    				if(number==1){	
    				cout<<" A ";
    				}
    				else if(number>=2 && number<=9)
    				cout <<" "<<number<<" ";
    				else{
    					if(number==10)
    						cout<<" 10 ";
    					else if(number== 11)
    						cout<<" J ";
    					else if(number==12)
    						cout<<" Q ";
    					else 
    						cout<<" K ";
    						
    					}		//endelse
    
    				cout<<"enter the card<A,2-9,T,J,Q,K>:  ";
    			cin>>newcardchar;
    
    
    				if(newcardchar=='a')
    				newcard=11;
    				if(newcardchar=='2') 
    				newcard=2;
    				if(newcardchar=='3')
    				newcard=3;
    				if(newcardchar=='4')
    				newcard=4;
    				if(newcardchar=='5')
    				newcard=5;
    				if(newcardchar=='6')
    				newcard=6;
    				if(newcardchar=='7')
    				newcard=7;
    				if(newcardchar=='8')
    				newcard=8;
    				if(newcardchar=='9')
    				newcard=9;
    				if(newcardchar=='j' || newcardchar=='q' || newcardchar=='k' || newcardchar=='t')
    				newcard=10;
    			
    
    
    
    			total+=newcard;
    			cout<<"total: "<<total<<endl;
    
    				if(total<21){
    				cout<<"do you want another card? <Y or N>:  ";
    				cin>>anothercard;
    				cout<<"how many cards do you have: \n";
    				cin>>repeatcard;
    
    					number = 1 + (rand() % 13);
    					cout<<"card: "<<endl;
    					if(number==1){	
    					cout<<" A ";}
    					else if(number>=2 && number<=9)
    					cout <<" "<<number<<" ";
    					else{
    						if(number==10)
    							cout<<" 10 ";
    						else if(number== 11)
    							cout<<" J ";
    						else if(number==12)
    							cout<<" Q ";
    						else 
    							cout<<" K ";
    						}		//endelse
    
    					cout<<"enter the card<A,2-9,T,J,Q,K>:  ";
    					cin>>newcard;
    
    					if(newcardchar=='a'|| newcardchar=='A')
    					newcard=11;
    					if(newcardchar=='2') 
    					newcard=2;
    					if(newcardchar=='3')
    					newcard=3;
    					if(newcardchar=='4')
    					newcard=4;
    					if(newcardchar=='5')
    					newcard=5;
    					if(newcardchar=='6')
    					newcard=6;
    					if(newcardchar=='7')
    					newcard=7;
    					if(newcardchar=='8')
    					newcard=8;
    					if(newcardchar=='9')
    					newcard=9;
    					if((newcardchar=='j'|| newcardchar=='J')||(newcardchar=='q' || newcardchar=='Q')||(newcardchar=='k' ||newcardchar=='K')||(newcardchar=='t'||newcardchar=='T'))
    					newcard=10;
    					
    
    
    					total+=newcard;
    					cout<<"total: "<<total<<endl;
    					}			//end if
    
    					if(total>21 && (newcardchar=='a' || card1char=='a' || card2char=='a')){			//if total is more than 21 and you have an ace, subtract 10 for each ace
    					total=total-10;
    					cout<<"new total: "<<total<<endl;
    					}	//end first if
    
    
    			
    		}while( total<21 && (anothercard=='y' || anothercard=='Y') && repeatcard<=4);			//end do loop
    
    	
    }
    
    
    if(total==21)								//if21 you win
    			cout<<"you win \n";
    if(total>21)								//if higherthan 21 lose
    			cout<<"busted \n";
    
    if (anothercard=='n' || anothercard=='N')				//if you dont want another card
    		break;
    		
    
    
    
    cout<<"\n Do you want to repeat? <Y or N> \n";
    	cin>>repeat;
    
    	if(repeat=='y' || repeat=='Y')
    	{		
    		system("cls");
    	}
    
    	if(repeat=='n' || repeat=='N')
    	{	
    		system("cls");
    		break;
    	}
    }
    
    
    
    
    	system("pause");
    		return 0;
    }
    Last edited by rfoor; 10-07-2010 at 12:37 PM.

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    just some more pointers you need to address (forgive the pun) before anything else, learn to use switch statement instead of your masses of ifs, and dont call srand repeatedly inside a loop, your random stuff wont be random. i cant go through the other question at moment
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    I would also suggest better indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    may i ask why you ask the player how many cards they have? and then ask them to record the cards also?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21