Thread: Program crashing when I use IO

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Program crashing when I use IO

    Hey dudes and dudettes

    I'm creating my first text RPG, and I'm having a little problem with File IO. I'm writing a register system using File IO, and it crashes when I write to the file. I don't want to go any further as my Login system will work on the same principles, and If they don't work, then my game doesn't work. Here are the files that I think that you will need to help me with my problem:

    RegisterMgr.h
    Code:
    #include <iostream>
    #include <fstream>
    
    class RegisterMgr
    {
        public:
        std::string sRegister(std::string sUsername, std::string sPassword);
        
        private:
        std::string m_sUsername;
        std::string m_sPassword;
        
    };
    RegisterMgr.cpp
    Code:
    #include "RegisterMgr.h"
    
    std::string RegisterMgr::sRegister(std::string sUsername, std::string sPassword)
    {
        std::ofstream oRegister("Register.txt", std::ios::app);
        sUsername = m_sUsername;
        sPassword = m_sPassword;
        oRegister << sUsername << std::endl;
        oRegister << sPassword << std::endl;
        oRegister.close();
    }
    When I get up to entering the desired username and password, the program crashes

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Are you sure the file is opened successfully?

    It's (almost) never a good idea to open a file and start using it without checking.

    Code:
    std::ofstream oRegister("Register.txt", std::ios::app);
    if (!oRegister) {
       ... file was not opened successfully
    }

  3. #3
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Hmm..... I'm pretty sure that it's opening successfully, as when i open the file outside of the program, manually, there are blank lines that have been written where the password and username should've been. Although I took your advice, and checked if the file was opened successfully, and as I predicted, it did. The crash happens after I write to the file.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I'm not too sure about the logics of your program, but it seems like you are just printing m_sUsername and m_sPassword to the file everytime, not the parametres, which is probably not what you want, because otherwise you wouldn't need the parametres.

    If m_sUsername and m_sPassword are blank, it's not surprising that 2 blank lines are written to the file. Meaning the crash actaully happened AFTER the write.

    (Note that you have 2 independent bugs here)

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Thanks cyberfish! I fixed the IO problem thanks to you. The file writing works, but now I have to deal with the crashing

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the error.

    By the way:
    • You should be using header inclusion guards.
    • You only need to #include <string> in RegisterMgr.h
    • You should #include <fstream>, <ostream>, "RegisterMgr.h" and optionally <string> in RegisterMgr.cpp.
    • You should pass std::string objects by (const) reference unless you really want to copy them.
    • The sRegister member function is declared as returning a std::string but does not actually return anything.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Using a debugger is the quickest way to find a crash.

  8. #8
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Hmmm.... I have did as you said Laserlight, and rewrote the function as a void instead of a std::string, and I have gotten past the crash... kindov it crashes when it ends the function, which i'm not sure how to stop. Here is the full function:

    RegisterMgr.h
    Code:
    #include <string>
    
    class RegisterMgr
    {
        public:
        void vRegister(std::string, std::string);
        
        private:
        
    };
    RegisterMgr.cpp
    Code:
    #include "RegisterMgr.h"
    #include <fstream>
    #include <ostream>
    
    void RegisterMgr::vRegister(std::string sUsername, std::string sPassword)
    {
        std::ofstream oRegister("Register.txt", std::ios::app);
        oRegister << sUsername << std::endl;
        oRegister << sPassword << std::endl;
        oRegister.close();
    }
    ResourceMgr.cpp
    Code:
    #include "ResourceMgr.h"
    #include <iostream>
    
    ...............................................
    
    void ResourceMgr::vDisplayRegisterMenu()
    {
        std::system("cls");
        std::cout << "Welcome, please fill out the required fields to Register:" << std::endl;
        std::cout << "\nDesired Username: ";
        std::cin >> srUsername;
        std::cout << "\nDesired Password: ";
        std::cin >> srPassword;
        vRegister(srUsername, srPassword);
        std::system("cls");
        std::cout << "Thank you for registering " << srUsername << std::endl;
    }
    It crashes at the end of vDisplayRegisterMenu(); :S

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by legit
    it crashes when it ends the function, which i'm not sure how to stop.
    cyberfish made a good suggestion: use your debugger, e.g., place break points and step through the code until you detect a crash.

    Quote Originally Posted by legit
    Here is the full function:
    It would be even better if you can give us something that we can compile and run. However, avoid giving us the entire code: give us the smallest and simplest compilable program that demonstrates the crash.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    AHA! Sorry bout this everyone, but i've found the problem! I had std::cin.get() in Main.cpp and in my exit function, so I switched the std::cin.get() in Main.cpp to std::cin.ignore() and it seems to work now Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM