CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Feb 2008
    Posts
    24

    Save and reading .dat file:S

    Hi, ive been making a blackjack program and current im trying to get it to save and load how much money you have. This is working but it looks like its saving the address of the variable instead of the value.

    The functions which arnt working are void save() and void load()

    Edit: They are at the bottom of the code:P

    Code:
    //Blackjack
    //By Thomas Bean
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <fstream>
    using namespace std;
    void game();   //Main game loop
    void display(int card);   //Displays card details
    void p_blackjack(int total, int bet);   //Displays win or loose player
    void dealer(int total, int bet); //Displays win or loose dealer
    void display_cash();
    void load();
    void save();
    int cash = 200; //Global, how much money player has
    int main()  //Main loop
    {
        int menu;
         begin:
         system("CLS");
         cout << "                        Blackjack!"<< endl;
         cout << "                        By Thomas Bean\n" << endl;
    	 display_cash();
         cout << "Please choose an option:" <<endl;
         cout << "1. Play The Game" << endl;
         cout << "2. Rules" << endl;
         cout << "3. Reset Money" << endl;
    	 cout << "4. Load Saved Game" << endl;
    	 cout << "5. Save Game" << endl;
    	 cout << "6. Quit" << endl;
         cin >> menu;
         switch (menu) //Menu System
         {
         case 1:
                cout << "Enjoy the game!";
                game();
                goto begin;                       
         case 2:
                cout << "Rules!\n";
                goto begin;             
         case 3:
    			cash = 200;
    			goto begin;
    	 case 4:
    		    load();
    			goto begin;
    	 case 5:
    			save();
    			goto begin;
    	 case 6:
    
                cout << "Goodbye!\n";
                cout << "Press the enter key to exit";
                cin.ignore(cin.rdbuf()->in_avail() + 1);
         }
         menu = 0;
    
         return 0;
    }
    
    
    void game()
    {
         system("CLS");
         cout << "                        Blackjack!\n\n"<< endl;
         int card = 0, total = 0, dealer_total = 0, i = 0, deck[53], game = 0, menu2 = 1, hcount = 4;
         int points[54] = {0,2,3,4,5,6,7,8,9,10,10,10,10,11,2,3,4,5,6,7,8,9,10,10,10,10,11,2,3,4,5,6,7,8,9,10,10,10,10,11,2,3,4,5,6,7,8,9,10,10,10,10,11};
         int hand[6], dealer_hand[6], bet = 0;
         int finish = 0;
         srand(time(0));
         while (game = 0);
         {
         //Fills deck 1 - 52
         for (int i=1; i<53; i++) {
            deck[i] = i;  
         }        
         //Shuffles deck      
         for (int i=0; i<(52-1); i++) 
         {
                int r = i + (rand() % (52-i)); 
                int temp = deck[i]; deck[i] = deck[r]; deck[r] = temp;
         } 
         //Gets bet
    	 bet:
         cout << "Please enter how much you would like to bet(e.g. 20):" << endl;
         cout << "You currently have $"<<cash << endl;
         cin >> bet;
    	 if (cash - bet < 0)
    	 {
    		 cout << "Please enter a valid bet!\n" << endl;
    		 goto bet;
    	 }
    	 
         
         cout << "Your cards:"<< endl;
         for (int i = 0; i<=1; i++) //Deals 2 cards to player, display to screen
         {
          card = 0;
          card = deck[i];
          hand[i] = card;
          display(card);
          total += points[card];            
         }
         cout << "\nPoints: "<< total << endl; //Displays points to screen
    
         cout << "\n\nDealers Cards:"<< endl; //Deals 2 cards to dealer, displays second
         for (int i = 2; i<=3; i++)
         {
          card = 0;
          card = deck[i];
          dealer_hand[i - 2] = card;
          if (i == 2)
          cout << "X"<< endl;
          if (i == 3)
          display(card);
          dealer_total += points[card];            
         }
         cout << "\n" << endl;
         if (total == 21) //Player blackjack
         {
           cout << "Blackjack!\nYou Win!" << endl;
           cash += bet;
           cout << "You now have $"<< cash << endl;  
    	   cout << "Please press 1 to exit" << endl;
           cin >> finish;
           return;
    
         }
         if (dealer_total == 21) //Dealer blackjack
         {
                cout << "Dealer Blackjack!\nYou Loose!" << endl;
    			cash -= bet;
    			cout << "You now have $"<< cash << endl;
    			cout << "Goodbye!\n";
    			cout << "Please press 1 to exit" << endl;
    			cin >> finish;
    			return;
         }
         cout << "Player:" << endl;
         while (menu2 = 1 && hcount <= 6) //Player hits or stands
         {
             cout << "Do you want to:" << endl;
             cout << "1. Hit" << endl;
             cout << "2. Stand" << endl;
             cin >> menu2;
         
             if (menu2 == 1)
             {
              card = deck[hcount];
              hand[hcount] = card;
              hcount ++;
              display(card);
              total += points[card];
              cout << "Players Total - " << total <<"\n" << endl;      
             }
             else
             {
                 break;
             }
             p_blackjack(total, bet);
             if (total >= 21)
             {
              cout << "\n" << endl;
                cout << "Goodbye!\n";
    			cout << "Please press 1 to exit" << endl;
    			cin >> finish;
    			return;
             }
             
         }
         dealer: //Dealer hits or wins
         if (total < 21)
         cout << "\nDealers Cards:" << endl;
         while (dealer_total < total && hcount <= 10 && total < 21)
         {
           card = deck[hcount];
           dealer_hand[hcount - 4] = card;
           hcount ++;
           display(card);
           dealer_total += points[card];
           cout <<"Dealers Total - " << dealer_total <<"\n" << endl;
           dealer(dealer_total, bet);
           if (dealer_total >= 21)
           {
              break;
           }
    	   
                      
         }
         if (dealer_total >= total && dealer_total <=21)
         {
           cout << "\n\nDealers Total = " << dealer_total << endl;
           cout << "Players Total = " << total << endl; 
           cout << "You Loose!" << endl;
    	   cash -= bet;
    	   cout << "You now have $"<< cash << endl;
         }
         }
         cout << "Goodbye!\n";
         cout << "Press the enter key to exit";
         cin.ignore(cin.rdbuf()->in_avail() + 1);
         return;
    } 
    void display(int card)
    {
         char royalty[5] = {'J','Q','K','A'};
         if (card<=13)
          {
             cout << "Heart - ";
             if (card<=9)
             {
                  cout << card + 1 << endl;
             }
             else
             {
                 cout << royalty[card - 10] << endl;
             }
          }
          if (card <=26 && card >= 14)
          {
             cout << "Diamond - ";
             if (card<=22)
             {
                  cout << card - 12 << endl;
             }
             else
             {
                 cout << royalty[card - 23] << endl;
             } 
          }
          if (card <=39 && card >= 27)
          {
             cout << "Club - ";
             if (card<=35)
             {
                  cout << card - 25 << endl;
             }
             else
             {
                 cout << royalty[card - 36] << endl;
             }  
          }
          if (card <=52 && card >= 40)
          {
             cout << "Spade - ";
             if (card<=48)
             {
                  cout << card - 38 << endl;
             }
             else
             {
                 cout << royalty[card - 49] << endl;
             }  
          }
    }
    
    void p_blackjack(int total, int bet)
    {
         if (total == 21)
         {
         cout << "Player Has 21!\nYou Win!" << endl;
         cash += bet;
         cout << "You now have $"<< cash << endl;     
         }
         if (total > 21)
         {
    	 cash -= bet;
         cout << "You Loose!" << endl;
    	 cout << "You now have $"<< cash << endl;  
         }
    
    }   
    void dealer(int total, int bet)
    {
         if (total == 21)
         {
         cout << "Dealer Has 21!\nYou Loose!" << endl;
    	 cash -= bet;
    	 cout << "You now have $"<< cash << endl;
         }
         if (total > 21)
         {
         cout << "Dealer Busts! You Win!" << endl;
         cash += bet;
         cout << "You now have $"<< cash << endl;     
         }       
    }
    
    void display_cash()
    {
    	if( cash == 200)
    		cout << "You currently have $" << cash << "\n" << endl;
    	if( cash < 200)
    		cout << "You currently have $" << cash << "! You have lost $" << (200 - cash) << "! You suck!\n" << endl;
    	if( cash > 200)
    		cout << "You currently have $" << cash << "! You have earnt $" << (cash - 200) << "! Well done!\n" << endl;
    }
    
    void load()
    {
    	ifstream inputFile;
    	int number = 0;
    	inputFile.open("save.dat");
    	inputFile>&number;
    	cash = number;
    	inputFile.close();
    }
    void save()
    {
    	ofstream outputFile;
    	outputFile.open("save.dat");
    	outputFile<<&cash;
    	outputFile.close();
    }
    Thankyou for any help
    Last edited by boburob; May 17th, 2008 at 04:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured