Thread: Why does this keep closing?

  1. #1
    Registered User Cloud6's Avatar
    Join Date
    Jul 2006
    Location
    USA
    Posts
    11

    Why does this keep closing?

    Well, after reading some source code, I wrote a text blackjack ( nothing too fancy, bare essentials ) and I compiled it. Compiles just fine, but when I try and run it, it closes immediately. What am I doing wrong?

    Code:
    /****************************************
    * Text BlackJack Game
    * Author: Cloud6
    * Level: Beginner
    ****************************************/
    
    #include <iostream>
    using namespace std;
    
    //declare player structure
    struct Player
    {
           unsigned int money;
           unsigned int wins, losses;
           bool won, lost;
    };
    Player player;  
    
    void play();
    
    int main()
    {
        //declaring needed variables
        unsigned short input;
        unsigned short a = 0;
        
        //setting default stats
        player.money = 600;
        player.wins = 0;
        player.losses = 0;
        player.won = false;
        player.lost = false;
        
        //begin game
        cout<<"Welcome to Text BlackJack"<<endl;
        
        while(a)
        {
                       cout<<"\n\nPlease select your option."<<endl;
                       cout<<"1. Check money"<<endl;
                       cout<<"2. Play"<<std::endl;
                       cout<<"3. Review Status"<<endl;
                       cout<<"4. Quit"<<std::endl;
                       cin>>input;
                       switch(input)
                       {
                                    case 1:
                                           cout<<"You currently have "<<player.money<<" dollars at your disposal.";
                                           break;
                                    case 2:
                                           play();
                                           a = 1;
                                           break;
                                    case 3:
                                           cout<<"\n\nWins: "<<player.wins<<" Losses: "<<player.losses<<".";
                                           break;
                                    case 4:
                                           return 0;
                                           break;
                       }
                       a = 0;
                       
                       if (player.money == 0)
                       {
                                        cout<<"Out of money there..."<<endl;
                                        cout<<"Press any key to quit"<<endl;
                                        system("pause");
                                        return 0;
                       }
        }
        
        return 0;
    }
    
    void play()
    {
    //actual code goes here
    // this part is not impoartant
    }
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Code:
    unsigned short a = 0;
    ...
    while(a) { ... }
    while(0) will never execute.
    signature under construction

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    yeah, you probably want while(1)...I think.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or initialize 1 to a and replace return 0; in the fourth case: with a = 0;.
    Last edited by jafet; 07-14-2006 at 03:52 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User Cloud6's Avatar
    Join Date
    Jul 2006
    Location
    USA
    Posts
    11
    Thanks! I changed it and now it runs just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Stop CFormView from closing
    By bdiamond in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2004, 01:22 PM
  4. Premature closing of threads
    By *ClownPimp* in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-07-2003, 11:27 AM
  5. closing microsoft internet explorer
    By canine in forum Windows Programming
    Replies: 11
    Last Post: 03-19-2002, 09:12 AM