Need help to tweak this beginner program :|

This is a discussion on Need help to tweak this beginner program :| within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> using namespace std; int main() { int cards, num, count; char value, j, k, q, ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    Need help to tweak this beginner program :|

    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    int main()
    {
    
    
    int cards, num, count;
    char value, j, k, q, t, a, J, K, Q, T, A;
    int total = 0;
    
    
    cout<<"How many cards do you have?"<<endl;
    cin>>cards;
    count=1;
    
    
    
    
    
    
        for (count = 1; count <= cards; count++)
        {    
            if (num=='k'||'K'||'q'||'Q'||'j'||'J'||'t'||'T')
            {
            num=10;
            }
            else if (num=='a'||'A')
            {
            num=11;
            }
        cout <<  "Please enter the card value for card " << count <<": "<< endl;
        cin >> num;
        total=total+num;
        }
    
    
    if (total <=21 && total >= 2)
    
    
    {
    cout << "Total is "<< total << endl;
    }
    
    
    else
    cout<< "Bust"<<endl;
    
    
    return 0;
    
    
    }



    This is just a blackjack program that counts the cards. Something is wrong with my q, k, j, a, I'm trying to give q k j = 10 and ace = 11 or 1. I got the program correctly with normal numbers, but when I start to use q, k, j and a, it starts to bug out :|

    Any help would be appreciated, I'm a beginner and this is an exercise, I'm just a bit stuck at the moment

    The output should be something like this:

    Example 1:
    How many cards do you have? 2
    Please enter the card values for card 0: a
    Please enter the card values for card 1: j
    Your score is 21

    Example 2:
    How many cards do you have? 3
    Please enter the card values for card 0: j
    Please enter the card values for card 1: k
    Please enter the card values for card 2: 3
    BUSTED

    Edit: Took out the extra things on int, I had a lot of inputs when I was using the switch command, but since I just used a loop instead I took out the card1 card2 etc.
    Last edited by Sephyx; 10-04-2011 at 09:55 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
        for (count = 1; count <= cards; count++)
        {   
            if (value=='k'||'K'||'q'||'Q'||'j'||'J'||'t'||'T')
            {
            num=10;
            }
            else if (value=='a'||'A')
            {
            num=11;
            }
        cout <<  "Please enter the card value for card " << count <<": "<< endl;
        cin >> num;
        total=total+num;
        }
    Consider the flow of this loop. You check the variable 'value' to handle face cards and the 10 card. But from whence do you get 'value'? What is 'value' supposed to represent? Where is it being assigned, and how?
    Consider this post signed

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Also note that simply chaining || will not have the effect you are probably expecting. You will need to individually compare value to each character value. In cases where I find myself doing a lot of logically OR'd numeric comparisons like you have here I usually throw it into a switch as:

    Code:
    switch (value)
    {
       case 'k':
       case 'K':
       case 'q':
       case 'Q':
       // etc...
       num = 10;
       break;
       case 'a':
       case 'A':
       num = 11;
       break;
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Oh that's right, value is nothing, I edited and replaced it with num, but I still get weird values. When I type in 2 for 'cards' for the loop to repeat twice, I get 10 for k j and q, but after I type in the first card the program ends. When I type in 3 for 'cards' I get even weirder values such as q being 20 and the program suddenly ending, k/j being a bust right away.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    I tried to exchange this with the if / else if, but it had the same effect, I'm still getting weird values ;(

    Quote Originally Posted by valaris View Post
    Also note that simply chaining || will not have the effect you are probably expecting. You will need to individually compare value to each character value. In cases where I find myself doing a lot of logically OR'd numeric comparisons like you have here I usually throw it into a switch as:

    Code:
    switch (value)
    {
       case 'k':
       case 'K':
       case 'q':
       case 'Q':
       // etc...
       num = 10;
       break;
       case 'a':
       case 'A':
       num = 11;
       break;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    What exactly did you try?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You can't just change value to num in the if statements. "num" doesn't become initialized to anything valid until it gets to the cin >> num; call. You need to get the users input before you check it as a first task. You should also probably post some updated code if you want updated help.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Umm I tried Valaris' suggestion to use the switch/case and exchange it with the if/else if statement that I had to convert j,k,q into 10, but my program is still giving me off values when I try it :|

    Updated code:
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    int main()
    {
    
    
    int num, count, cards;
    char j, J, q, Q, k, K, a, A;
    int total = 0;
    
    
    cout<<"How many cards do you have?"<<endl;
    cin>>cards;
    
    
    count=1;
    
    
    	for (count = 1; count <= cards; count++)
    	{	
    
    
    	cout <<  "Please enter the card value for card " << count <<": "<< endl;
    	cin >> num;
    	
    	switch (num) // Put after cin>>num to translate value.
    	{
    	case 'k':
    	case 'K':
    	case 'q':
    	case 'Q':
    	case 'j':
    	case 'J':
    	num = 10;
    	
    	break;
    	
    	case 'a':
    	case 'A':
    	num = 11;
       
    	break;
    	}
    	
    	total=total+num;
    	}
      
    if (total <=21 && total >= 2) //Less/Equal to 21 or Greater/Equal to 2
    
    
    {
    cout << "Total is "<< total << endl;
    }
    
    
    else
    cout<< "Bust"<<endl;
    
    
    return 0;
    
    
    }
    Last edited by Sephyx; 10-04-2011 at 10:18 PM.

  9. #9
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Notice you are expecting a char value into num but you have declared it an int. num (or perhaps some better named variable) should be of type char. Then set an integer value to the numeric value of the card.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Wow, yeah, by putting 'num' into char it fixed a lot of things it works for the most part, only one last thing.

    Edit: When I put 'num' into char it was able to fix the k,j,q values, but now it's not taking in the numeric values, how do I put the numeric values into char to add it to the 'case'? Since num cant be both num and char at the same time.

    Updated Code:
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    int main()
    {
    
    
    int count, cards;
    char j, J, q, Q, k, K, a, A, num;
    int total = 0;
    
    
    cout<<"How many cards do you have?"<<endl;
    cin>>cards;
    
    
    count=1;
    
    
        for (count = 1; count <= cards; count++)
        {    
    
    
        cout <<  "Please enter the card value for card " << count <<": "<< endl;
        cin >> num;
        
        switch (num) // Put after cin>>num to translate value.
        {
        case 'k':
        case 'K':
        case 'q':
        case 'Q':
        case 'j':
        case 'J':
        num = 10;
        
        break;
        
        case 'a':
        case 'A':
        
            if (total<=10 && total>=2)
            num = 11;
            else
            num = 1;
        
        break;
        }
        
        total=total+num;
        
        }
      
    if (total <=21 && total >= 2) //Less/Equal to 21 or Greater/Equal to 2
    
    
    {
    cout << "Total is "<< total << endl;
    }
    
    
    else
    cout<< "Bust"<<endl;
    
    
    return 0;
    
    
    }
    My last problem right now is the value of the Ace, I put

    Code:
        if (total<=10 && total>=2)
            num = 11;
            else
            num = 1;
    Under case a/A, but let's say you have 2 cards, when you type in 'q' then 'a' it is correct since the total would be 10 from the value of 'q' allowing 'a' to be 11 and giving me a 21, but when I type 'a' first it gives me a 1 since the total is 0. Finally almost have it >.< I need to get better at sorting out int/char/double variables ;\
    Last edited by Sephyx; 10-04-2011 at 11:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help beginner with program please
    By bubbles56 in forum C Programming
    Replies: 1
    Last Post: 03-23-2011, 10:37 PM
  2. Beginner, cant get my program to run :-(
    By leahknowles in forum C Programming
    Replies: 5
    Last Post: 03-23-2011, 03:44 PM
  3. Beginner C++ Help. My very first program.
    By Jechob in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2010, 01:34 PM
  4. Please Help Me with this beginner C++ Program!
    By ClearSights in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 10:22 AM
  5. tweak CMOS settings
    By yin_howe_ho in forum Windows Programming
    Replies: 3
    Last Post: 09-30-2003, 03:11 PM

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