d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Problems
Add Reply New Topic New Poll
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 11 2012 01:23pm
Code
while (counter <3)
{

 cout << "\tEnter a card: ";
 cin  >> card;

 switch (card)
 {
 case 't':
 case 'T':
 case 'k':
 case 'K':
 case 'q':
 case 'Q':
 case 'j':
 case 'J':
  total = total + 10;
  break;
 case 'A':
  if (total >= 11)
   total = total + 1;
  else
   total = total + 11;
  break;
 case 'a':
  if (total >= 11)
   total = total + 1;
  else
   total = total + 11;
  break;
 case '2':
 case '3':
 case '4':
 case '5':
 case '6':
 case '7':
 case '8':
 case '9':
  total = total + card - '0';
  break;
 default:
  cout << "\n\tError: You have not entered a correct card number." <<endl<<endl;
  return 0;
  break;
 }
 //cout << "\t\tCard Value: "<<total<<endl;
 counter = counter++;

}


I have that and I need it to print the value of each card at the end of the sequence
Also When I enter anything with the first number of the case aka 23 it will accept the 2 and move to the next loop instead of the error message
how do I fix that
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Oct 11 2012 03:05pm
strlen(card)
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 11 2012 04:56pm
Code
cin >> input;

if (isdigit(input))
{
int test = atoi(input);
if (test < 2 || test > 9)
{
...//print error
}
else
}
... //do stuff with it
}
}
else
{
...//check to see if t q k j or whatever
}


something like this maybe?

input would have to be a character array to use atoi IIRC.

This post was edited by Eep on Oct 11 2012 04:57pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 11 2012 09:01pm
Quote (Richter @ Oct 11 2012 05:05pm)
strlen(card)


this.

if strlen returns greater then 1 its invalid input

id also use the tolower() or toupper() function so that all your card inputs are the same so you dont have to use case switches that fall through to get the same output. would eliminate the need for upper and lower case case switches

you could also use a file streaming function to get input from STDIN but limit it to 1 character. the function fgets() comes to mind where you can supply a file descriptor (STDIN), a character buffer (array of characters), and a character limit (int) and fgets will read input from STDIN and put the data into the character buffer until it reaches the limit or it encounters a newline character which ever comes first.

although im sure c++ has a built in string function to do this or limit the amount of characters somehow.

i think string.substring() would work ( switch(card.substring(0,1) ) assuming you are using c++ strings, but would be a hack patch job for the real error of reading in to many characters.

This post was edited by AbDuCt on Oct 11 2012 09:06pm
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 12 2012 02:02am
Not allowed to use strings I ended up using three switches, Thanks anyways though :D
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Oct 12 2012 02:33am
Quote (Speed93 @ Oct 12 2012 10:02am)
Not allowed to use strings I ended up using three switches, Thanks anyways though :D


;)
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 12 2012 03:02am
Quote (Nymphomaniac @ Oct 12 2012 04:33am)
;)


^^ huge props to this chica right here
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 12 2012 07:53am
Quote (Speed93 @ Oct 12 2012 04:02am)
Not allowed to use strings I ended up using three switches, Thanks anyways though :D


glad you figured it out :3 next time you post though post complete code. its hard working with snippets not knowing what variables are defined as.

This post was edited by AbDuCt on Oct 12 2012 07:53am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 12 2012 10:37am
Quote (AbDuCt @ Oct 12 2012 09:53am)
glad you figured it out :3 next time you post though post complete code. its hard working with snippets not knowing what variables are defined as.


coughpythoncough
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 12 2012 07:20pm
Quote (carteblanche @ Oct 12 2012 12:37pm)
coughpythoncough


python weirds me out. the entire time ive been programming ive always had to cast or declare a type for a variable but then python is like LOL NOPE JUST USE ME ANY WAY YOU PLEASE LIKE A DIRTY WHORE.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll