Thread: Closing program

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    Closing program

    Code:
        
       char name[50];
        char exit;
    
        cout << "Please enter you name for the High Scores list : ";
      
        cin.getline(name, 50);
        ofstream WriteFileScore("HiScores.txt");
    
        if(!WriteFileScore.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScores.txt.";
            exit = getch();
            
        }    
        else    
        {
            WriteFileScore << player.score;
            WriteFileScore.close();    
        }    
        
        ofstream WriteFileName("HiScoreNames.txt");
        
        if(!WriteFileName.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScoreNames.txt.";
            exit = getch();
        } 
        
        else
        {
            WriteFileName << name;
            WriteFileName.close();
        }
    After I type in the my name, the program just closes. I have tried a system("pause") and tried asking for input and it still close for some reason.
    Thanks for any help.
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The code you posted is going to close after you enter the name. Try posting the entire program, with the system("pause") that you said didn't work. Include the headers, the main declaration, etc.

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    1) The whole program is very large so I just gave the function.
    2) Not sure, but why should it close? Is it the WriteFileScore.close() function?
    3) The block of code I posted in my first post just closed without making the file (in case you were wondering). Code block A. works fine as its own program, but it wont work with the big program I am doing. Code block B. is the complete function that I tried to use in my program. Block B. is just code block A., but instead of using the int "score", it uses "player.score". Code block B. gives the same problem as the first block of code i posted.
    Thanks

    A
    Code:
    #include <iostream>
    #include <fstream>
    #include <conio2.h>    
        
    using namespace std;
        
    int main()
    {    
        char name[50];
        char exit;
        int score = 10;
    
        cout << "Please enter you name for the High Scores list : ";
      
        cin.getline(name, 50);
        ofstream WriteFileScore("HiScores.txt");
    
        if(!WriteFileScore.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScores.txt.";
            exit = getch();
            system("Pause");
            
        }    
        else    
        {
            WriteFileScore << score;
            WriteFileScore.close();    
        }    
        
        ofstream WriteFileName("HiScoreNames.txt");
        
        if(!WriteFileName.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScoreNames.txt.";
            exit = getch();
            system("Pause");
        } 
        
        else
        {
            WriteFileName << name;
            WriteFileName.close();
        }
    }
    B.
    Code:
    void HiScores()
    {
        
        char name[50];
        char exit;
    
        cout << "Please enter you name for the High Scores list : ";
      
        cin.getline(name, 50);
        ofstream WriteFileScore("HiScores.txt");
    
        if(!WriteFileScore.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScores.txt.";
            exit = getch();
            system("Pause");
            
        }    
        else    
        {
            WriteFileScore << player.score;
            WriteFileScore.close();    
        }    
        
        ofstream WriteFileName("HiScoreNames.txt");
        
        if(!WriteFileName.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScoreNames.txt.";
            exit = getch();
            system("Pause");
        } 
        
        else
        {
            WriteFileName << name;
            WriteFileName.close();
        }
           
    }
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that may be the scope of the player class, or it may be how you have the access modifiers set up... either way you'd be better passing that in as an parameter...

    Code:
    playerClass player;
    ...
    HiScores(player.getscore());
    ...
    Code:
    void HiScores(int PlayerScore)
    {
        
        char name&#091;50&#093;;
        char exit;
    
        cout << "Please enter you name for the High Scores list : ";
      
        cin.getline(name, 50);
        ofstream WriteFileScore("HiScores.txt");
    
        if(!WriteFileScore.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScores.txt.";
            exit = getch();
            system("Pause");
            
        }    
        else    
        {
            WriteFileScore << PlayerScore;
            WriteFileScore.close();    
        }    
        
        ofstream WriteFileName("HiScoreNames.txt");
        
        if(!WriteFileName.is_open())
        {                        //The file did not open
            cout << "Error Opening HiScoreNames.txt.";
            exit = getch();
            system("Pause");
        } 
        
        else
        {
            WriteFileName << name;
            WriteFileName.close();
        }
           
    }
    if you're wondering, to access that data member, your object should look something like this:

    Code:
    class playerClass
    {
        public:
            int getScore() { return score; }
        private:
            int score;
    };
    then call player.getScore() if you want to find out the score... you should never be able to directly access data members... if you want to be able to modify the score, create a method called setScore(int) in the public section and have that modify the private member...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    I still dont understand why it should close...
    Keyboard Not Found! Press any key to continue. . .

  6. #6
    Code:
    WriteFileName.close();
    all that does is close the file you are working with,

    For opening the file i could be wrong
    but the reason your file isnt being created i believe
    is because you never actually opened the file.

    Code:
    ofstream WriteFileScore("HiScores.txt");
    should be

    Code:
    ofstream WriteFileScore;
    WriteFileScore.open("HiScores.txt", ios::out);
    
    or even :
    
    ofstream WriteFileScore;
    WriteFileScore.open("HiScores.txt");
    Last edited by JarJarBinks; 10-25-2004 at 10:43 AM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>I still dont understand why it should close

    What scope does player have? Unless player has global scope B code won't work. If score is a private data member of player then even if player has global scope your code won't work. What class is player an instance of? Post code please.

    --------------------------------------------
    ofstream writeToFile("myFile.txt");

    and

    ofstream writeToFile;
    writeToFile.open("myFile.txts");

    are functionally the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 08:31 PM
  3. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM