d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Please Basic C++
Add Reply New Topic New Poll
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 22 2012 08:23pm
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;
}


Can't figure out what Im doing wrong

It will all compile but it says card3 is being used without being initialized.

This post was edited by Speed93 on Oct 22 2012 08:23pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 22 2012 08:36pm
Code
if (counter==0)
{
 card1=card;
 if (counter==1)
  card2=card;
  if (counter==2)
   card3=card;
}


i think you meant to use else if instead of nesting the entire block under counter == 0 (only card1 will be assigned right now)
Member
Posts: 9,916
Joined: May 11 2007
Gold: 3,280.00
Oct 22 2012 08:49pm
Im not quite sure how to do that...
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 23 2012 01:50am
Quote (Speed93 @ Oct 22 2012 09:49pm)
Im not quite sure how to do that...


if (counter==0)
{
card1=card;
}
else if (counter==1)
{
card2=card;
}
else if (counter ==2)
{
card3=card;
}

I think that would work.

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