14 Replies - 2778 Views - Last Post: 31 March 2010 - 11:03 AM Rate Topic: -----

#1 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Question(converting char to int)

Posted 26 March 2010 - 06:31 PM

I was wondering if there was a way to convert from an Char to an Int?

heres a sample code i wrote:

#include<iostream>
using namespace std; 

int main()
{ 
    char A(0),B(0),total(0); 
    cout << "Please enter a value for A:";
    cout << endl;
    cin >> A; 
    cout << "Enter a value for B:";
    cout << endl; 
    cin >> B;
    cout << endl;
    cout << endl;
    total = A + B; 
    cout << "the total is: " << total;
    cout << endl;
    cout << "program end";
    cout << endl<< endl << endl;
    system ("pause"); 
    return 0;
} 



My assignment has a part in it that deals with entering values that are numbers and characters. should i post my assignment code or can you explain with my test code to how to take a number and convert it to type int.

My assignment is:

Write a program that scores a blackjack hand. In blackjack, a player receives from two to five cards. The cards 2 through 10 are scored as 2 through 10 points each. The face cards - jack, queen, and king are scored as 10 points. The goal is to come as close to a score of 21 as possible without going over 21. Hence, any score over 21 is called "busted". The ace can count as either 1 or 11, whichever is better for the user. For example, and ace and a 10 can be scored as either 11 or 21. Since 21 is a better score, this hand is scored as 21. An ace and two 8s can be scored as either 17 or 27. Since 27 is a "busted" score, this hand is scored as 17.


The user is asked how many cards she or he has, and the user responds with one of the integers 2,3,4, or 5. The user is then asked for the card values. Card values are 2 through 10, jack, queen, king, and ace. A good way to hande input is to use the type char so that the card input 2, for example, is read as the character '2', rather than as the number 2. Input the values 2 through 9 as the characters '2' through '9'. Input the values 10, jack, queen, king, and ace as the characters 't', 'j', 'q', 'k', and 'a'. (Of course, the user does not type in the single quotes.) Be sure to allow upper - as well as lowercase letters as input.


After reading in the values, the program should convert them from character values to numeric card scores, taking special care for aces. The output is either a number between 2 and 21 (inclusive) or the word Busted. Use fucntions, multiway branches, switch statements or nested if-else statements. Your program should include a loop that lets the user repeat the calculation until they are done doing so.

incase you are wondering me and USMarineMIke are not the same person. we are just in the same class with the same assignments!..

so heres my code so far but i just need to know the awnser to my question which is: is there a way to convert from char to int?

#include <iostream>
using namespace std;

class Blackjack
{ 
 public: 
 void input();
 void figureout();
 void get_sum();
 void output(); 
 Blackjack(int cards);
 Blackjack();
 private:
 int master_hand;
 int raw_score;
 int raw_score2;
 int final sum;
 char cards;
}

int main()
{ 
 int input; 
 Blackjack input1;
  do
  {
    input1= Blackjack(input);
    input1.input(); 
    input1.get_sum(); 
    input1.figureout(); 
    input1.output();
    cout << endl;
    cout << "Would you like to run the program again?(y or n)\n";
    cin  >> again;
    cout << endl;
  }while((again == 'y')||(again == 'Y'));
  cout << endl << endl;
  cout << "<program end>";
  cout << endl << endl; 
  system("pause");
  return 0;
}
void Blackjack::input()
{ 
 cout << "Enter the values of your cards(how many ever cards you have in your hand):";    
 cout << endl;
 while(cards == counter)
 {
  cin >> card;
  
 } 


im still working on it.. i know how to do most of it..except the question i asked.

Just wondering if the way is to use istream?.. or is there another way?

Is This A Good Question/Topic? 0
  • +

Replies To: Question(converting char to int)

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question(converting char to int)

Posted 26 March 2010 - 07:00 PM

A char basically is an int -- or to be precise an "integer type". The difference is that on most modern PCs an int occupies 4 bytes and a char occupies only 1 byte, so an int can take on values from -2147483648 to +2147483647 but a char can only represent -128 through +127 (or 0 through 255 if interpreted as unsigned integers). To see this in action, you can define a char variable and then print it, but instead of printing it as a char, cast it to an int, for example:
char c = 'A';
std::cout << static_cast<int>(c) << std::endl;

and see what is printed.

The 256 char values are used to represent characters through the use of ASCII codes. If you look at this
ASCII Chart you will see, for example, that the decimal values 48-57 represent the printing characters '0' through '9' and values 97-122 represent the lowercase characters 'a' through 'z'.

From this you should be able to figure out whatever conversion you had in mind.

This post has been edited by r.stiltskin: 26 March 2010 - 07:01 PM

Was This Post Helpful? 1
  • +
  • -

#3 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:00 PM

ok i just figured out that istream will not work for this!.. i tried to change my test code to istream and looped it so i can enter for A.
so my equation would be total = A + total. but im only able to enter once for istream.
Was This Post Helpful? 0
  • +
  • -

#4 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:11 PM

@r.stiltskin

but heres my issue that i face. um if i do type casting. How will it work for my assignment. The way i have thought for my assignment is that

Example: input would be something like this: 'A 2 3 K 4'.

A has 2 values 1 or 11. so im going to have 2 equations.

A will have diffrent initialization in the diffrent equations.
Was This Post Helpful? 0
  • +
  • -

#5 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:21 PM

I dont think type casting will work
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question(converting char to int)

Posted 26 March 2010 - 07:24 PM

I didn't tell you to use typecasting. I only posted that to explain what is "hidden" behind the char representation.

Your assignment, should you choose to accept it, is to figure out how to use that information. B)
Was This Post Helpful? 1
  • +
  • -

#7 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:35 PM

oh ok.. i misunderstood.
[quote]
From this you should be able to figure out whatever conversion you had in mind.
[/code]

i just realized that. is there a way to just read in both.. I was wondering if you could enter a char in place of an int and declare in the program the char value that went in as a int as a integer value
Was This Post Helpful? 0
  • +
  • -

#8 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:37 PM

You can use a switch statement on a char value. So, if you have a int variable named 'handValue' holding the total value of the current hand, and the user input is stored in a char variable named 'choice', you can use something that this:

switch (choice)
{
    case 'K':
    case 'Q':
    case 'J':
        handValue += 10;
        break;
}



Maybe that might help a little.
Was This Post Helpful? 0
  • +
  • -

#9 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:51 PM

but user enters an input like ' A 3 4 Q 7'. from that i have to determine the Blackjack hand. here is what my while loop for the input looks like

while(counter <= cards)
{
 // enter value for card
 raw_value = raw_value + value;


Was This Post Helpful? 0
  • +
  • -

#10 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 26 March 2010 - 07:59 PM

rephrasing time..

so heres the issue

i want to take input from user lets say ' J K A 4 2' and convert it to 10 * 10 * (what ever A will equal) *4 * 2

thats what i want
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question(converting char to int)

Posted 26 March 2010 - 08:32 PM

So you look at each char input individually and run it through something like the code that Skaggles already gave you (expanded to deal with the number cards).

The Ace you have to deal with more cleverly. You can't just add in a value and forget about it. You have to come up with a way to "remember" if the hand includes an Ace, and if so, reduce the value by 10 if it goes over 21 (only once). But I'm not going to write it for you.

This post has been edited by r.stiltskin: 26 March 2010 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Question(converting char to int)

Posted 26 March 2010 - 08:36 PM

You can add a default catch to the switch statement, assuming you know the value will only ever be A, K, Q, J, or 1-0. The default catch can use the static_cast that r.stiltskin explained earlier to catch all numbers characters and turn them into their appropriate integer value, while the face cards are caught like what I showed.
Was This Post Helpful? 0
  • +
  • -

#13 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 30 March 2010 - 07:23 PM

Yahoo!..
i found my self a better way

heres my final complete solved producted
// Blackjack.cpp
// Ahmer Javed
// I did not recieve any help 100% sure
// this program scores a Blackjack hand
// 30 march 2010

#include <iostream>
using namespace std;

class Blackjack
{ 
 public: 
 void input();
 // Precondition: gets how many times to run the while loop from constructor 
 // Post condition: gets the values of the cards in user's hands
 void figureout();
 // Post condition: figures out the statement to display to the user
 void output(); 
 // Post condtion: displays the correct Statement to the user
 Blackjack(int cards);
 // constructor gets the number of cards in users hand 2 to 5
 Blackjack();
 // a constructor that does nothing
 private:
 char card;// value of each card
 char output_statement;
 int card_value;// added value with when Ace is entered ace is 1
 int card_value2;// added value with when Ace is entered ace is 11
 int counter;// counter to control number of times the loop goes in input()
 int cards; // number of cards in users hand
 int number_of_cards;
};
void description();
//post condition : displays the description of the program to the user

int main()
{
 int input; 
 char again;
 Blackjack input1;
 description();
  do
  {
    input1= Blackjack(input);
    
    input1.input(); 
    
    input1.figureout(); 
    
    input1.output();
    
    cout << endl;
    
    cout << "Would you like to run the program again?(y or n)\n";
    cin  >> again;
    cout << endl;
    
  }while((again == 'y')||(again == 'Y'));
  cout << endl << endl;
  cout << "<program end>";// indicates end of program to the user
  cout << endl << endl; 
  system("pause");
  return 0;
}
void description()
{ 
 cout << "Welcome to the Program Blacjack, This program scores a blackjack hand.\n"
      << "The cards are 2 through 10 and are worth the number of points\nthat are on the card.\n"
      << "face cards Jack(J),Queen(Q) and King(K)"
      << "are worth 10 points.\nThe goal is to come close to 21 but not to go over\n"
      << "Ace can be either scored as 1 or 11\n"
      << "The Program will ask for how many cards in hand and legal values are 2 to 5\n"
      << "The legal values to enter value of cards is 2 to 10.\n"
      << "Enter 10,Jack Queen,King,Ace as T,J,Q,K,A";
      cout << endl << endl << endl;
 }
void Blackjack::input()
{ 
 counter = 0; // sets counter to zero within Input()
 card_value = 0; //sets card_value to zero
 card_value2 = 0; // sets Card_value2 to zero
 
 cout << "Enter the values of your cards(how many ever cards you have in your hand):";
 cout << endl;
 cout << "Press enter after each card til the message iput end comes up";    
 cout << endl;
 cout << "Number of cards are: "<< number_of_cards; // displays the number of cards user has entered
 cout << endl;
 cout << endl;

 do
 {
  cout << "Enter card value: ";// ask user to enter the card value of the card in hand 
  cin >> card;
  cout << endl;
  counter++;

  
 if (card == '2')// if statements convert from char to int
 { 
  card_value = card_value + 2; 
  card_value2 = card_value2 + 2;
 }
 else if (card == '3')
 { 
  card_value = card_value + 3; 
  card_value2 = card_value2 + 3;
 } 
 else if (card == '4')
 { 
  card_value = card_value + 4; 
  card_value2 = card_value2 + 4;
 } 
 else if (card == '5')
 { 
  card_value = card_value + 5; 
  card_value2 = card_value2 + 5;
 }  
 else if (card == '6')
 { 
  card_value = card_value + 6; 
  card_value2 = card_value2 + 6;
 }  
 else if (card == '7')
 { 
  card_value = card_value + 7; 
  card_value2 = card_value2 + 7;
 } 
 else if (card == '8')
 { 
  card_value = card_value + 8; 
  card_value2 = card_value2 + 8;
 } 
 else if (card == '9')
 { 
  card_value = card_value + 9; 
  card_value2 = card_value2 + 9;
  } 
 else if ((card == 'A')||(card == 'a'))
 { 
  card_value = card_value + 1; 
  card_value2 = card_value2 + 11;
} 
 else if ((card == 'Q')||(card == 'q'))
 { 
  card_value = card_value + 10; 
  card_value2 = card_value2 + 10;
} 
  if ((card == 'K')||(card == 'k'))
 { 
  card_value = card_value + 10; 
  card_value2 = card_value2 + 10;
}
 else if ((card == 'J')||(card == 'j'))
 { 
  card_value = card_value + 10; 
  card_value2 = card_value2 + 10;
 }  
 else if ((card == 'T')||(card == 't'))
 {
  card_value = card_value + 10; 
  card_value2 = card_value2 + 10;
 }         
} while(counter != number_of_cards);

 cout << "<Input end>";// indidates end of input to user
 cout << endl << endl;


}

void Blackjack::figureout()
{ 
 if(card_value <= 21)
 { 
  output_statement = 'A';
 }
 if(card_value2 <= 21) 
 { 
  output_statement = 'B';
 }
 cout << endl;

}
void Blackjack:: output()
{ 
 switch(output_statement)// displays the correct output statement with the master hand score
 { 
  case 'A': 
  { 
  cout << "The Master hand is "<< card_value; 
  cout << endl;
  break;
  }
  case 'B': 
  {
  cout << "The Master hand is "<< card_value2;
  cout << endl;
  break;
  }
  default:// if figure out does not send anything than displays the message
  {
  cout << "BUSTED:: score is over 21!";
  cout << endl;
  break;       
}
}
}
Blackjack :: Blackjack(int cards)// gets the number of cards in users hand
{
 cout << "Enter the amount of cards in your hand: ";
 cin >> cards; 
 cout << endl;
 while ((cards < 2)||(cards > 5))
 { 
  cout << "Invalid amount of cards in hand re- enter: ";
  cin  >> cards;
  cout << endl;
 }
 number_of_cards = cards;

}
Blackjack :: Blackjack() // constructor that does nothing Default constructor
{
          

          
}



i figured IF statements were the best way to make this program actually work and conversion was that simple. No offense but switch statements!?.. that for me would not have worked!

Thanks for some help!
Was This Post Helpful? 0
  • +
  • -

#14 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Question(converting char to int)

Posted 31 March 2010 - 02:54 AM

// Blackjack.cpp
// Ahmer Javed
// I did not recieve any help 100% sure
// this program scores a Blackjack hand
// 30 march 2010



You didn't receive any help at all? It looks like r.stiltskin and Skaggles did indeed offer you assistance.
Was This Post Helpful? 1
  • +
  • -

#15 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Question(converting char to int)

Posted 31 March 2010 - 11:03 AM

Well they did not help me make the lines of code in the version of black jack i made. They recommended to reduce or use switch statements which i did neither of. i instead did if statements and had my own idea produced.

Well they did not help me make the lines of code in the version of black jack i made. They recommended to reduce or use switch statements which i did neither of. i instead did if statements and had my own idea produced.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1