d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To I Get This Loop To Execute Three Times
Add Reply New Topic New Poll
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 22 2012 08:06pm
Code
switch (card)
{
case 't':
case 'T':
case 'k':
case 'K':
case 'q':
case 'Q':
case 'j':
case 'J':
 total = total + 10;
 break;
case 'A':
case 'a':
 if (total >= 9)
 {
  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 + card1 - '0';
 break;
default:
 cout << "\n\tError: You have not entered a correct card number." <<endl<<endl;
 return 0;
 break;
 if (counter==0)
  card=card1;
  if (counter==1)
   card=card2;
 else
  card=card3;

 counter++;
}


For 3 of the cards the user enters.
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Oct 22 2012 08:13pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 23 2012 01:53am
I am assuming you meant to post this entire code


Code
//Lab 4-1


#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

int main(void)
{
//Variable Declarations

int total = 0;
char card;
char card1;
char card2;
char card3;
char counter=0;

//Description of the the program to the user instructing them how to enter the cards
cout << "This program is based off a game called black jac"
 << "\nPlease enter T for Ten, J for Jack, Q for Queen, K for King, and A for Ace."
 << "\nPlease enter the number value of the card for the rest."
 << "\nPlease enter only 1 digit or character"<< endl <<endl;


//Calculations for card 1, testing and card value outputs.
while (counter<=2)
{
 cout << "\tEnter a card: ";
 cin  >> card;
 if (counter==0)
 {
  card1=card;
  if (counter==1)
   card2=card;
   if (counter==2)
    card3=card;
 }
 

 //Checking/Swaping the aces
 if (card1=='a' || card1=='A')
 {
  if (card3=='a'||card3=='A')
   swap(card1,card2);
  else
   swap(card1,card3);
 }
 switch (card)
 {
 case 't':
 case 'T':
 case 'k':
 case 'K':
 case 'q':
 case 'Q':
 case 'j':
 case 'J':
  total = total + 10;
  break;
 case 'A':
 case 'a':
  if (total >= 9)
  {
   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;
  counter++;
 }
}


//Totals
cout << endl;
cout.width(28);
cout << "\n\t\tCard Value: "<<card1;
cout << "\n\t\tCard Value: "<<card2;
cout << "\n\t\tCard Value: "<<card2;
cout << "Total: " << total << endl<<endl;
return 0;
}



before I try and help, do you know about/are you allowed to use FUNCTIONS on this project?

This post was edited by Eep on Oct 23 2012 01:55am
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 23 2012 12:43pm
no we arent
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 23 2012 01:07pm
in that case, instead of looping the entire program, I would just prompt the user for input 3 times (as in, 3 instances of a cin). Then do the rest outside of the loop.


like

cin >> card1;
cin >> card2;
cin >> card3;
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 23 2012 01:09pm
except the OP explicitly asks to do this in a loop. and if you broke it into 3 input variables, you'd have to duplicate the code 3 times for the three separate variables.

This post was edited by irimi on Oct 23 2012 01:10pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 23 2012 01:13pm
nvm. Read it wrong!

This post was edited by Eep on Oct 23 2012 01:15pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 23 2012 01:15pm
well, for starters, it would help if the OP posted the actual question, rather than just his code. at this point, it's not very clear what he's even supposed to be doing, other than his short question thus far of "how do I do this in a loop?"

in any case, assuming he just wants to total the card values and then do some swapping around, then all he needs to do is move the swapping logic outside of the while loop while leaving everything else inside.

This post was edited by irimi on Oct 23 2012 01:16pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 23 2012 01:16pm
Quote (irimi @ Oct 23 2012 02:15pm)
well, for starters, it would help if the OP posted the actual question, rather than just his code.  at this point, it's not very clear what he's even supposed to be doing, other than his short question thus far of "how do I do this in a loop?"


well he posted another thread IIRC, that is where I got the full code from.


http://forums.d2jsp.org/topic.php?t=64959239&f=122

"CANT FIGURE OUT WHAT I AM DOING WRONG"

This post was edited by Eep on Oct 23 2012 01:16pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll