Thread: Assistance requested with ofstream problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2016
    Location
    Montana, USA
    Posts
    1

    Assistance requested with ofstream problem

    Hello all. I'm still relatively new to C and C++, so please forgive me if my question seems rather elementary. I've searched the posts and goggled my problem with no luck. I have studied the ostream references, frankly, I'm at a loss here so any tips anyone has to offer would be greatly appreciated.


    I'm writing a C++ console program to store and process geographical data. The idea is the user enters data describing a waypoint, the data is stored in a structure declared globally, the user verifies the data, the program writes/appends it to a file. The problem is that while the program runs without error, the user input is not written to the file. The program creates the file, but after the user inputs and verifies the data, the file remains empty and I'm afraid I am at a loss to explain why. The program is being written on and for Linux using Kdevelop and no errors are being encountered in the build process. Here is the relevant code:


    Code:
    int input_verify () {
        char good_entry;
        system("clear");
        cout << "-------------------------------------------------------------------------------------------------------------------------------------------------\n";
        cout << "                                                         NAVIGATIONAL INFORMATION SYSTEM                                                         \n";
        cout << "                                                           NEW ENTRY INPUT VERIFICATION                                                          \n";
        cout << "                                                   NOTE: THIS PROGRAM IS STILL UNDER DEVELOPMENT!                                                \n";
        cout << "-------------------------------------------------------------------------------------------------------------------------------------------------\n";
        cout << "\n";
        cout << "Please Verify the entered information.\n";
        cout << "\n";
        cout << newentry.wpt_friendly_name << "\n";
        cout << newentry.wpt_ident << "\n";
        cout << newentry.lat << " LAT\n";
        cout << newentry.lon << " LON\n";
        cout << newentry.wpt_misc_data << "\n";
        cout << "\n";
        cout << "Is the above information correct?\n";
        cout << "Enter (Y)es to commit the information to the database or\n";
        cout << "(N)o to re-enter or (A)bort to return to the main menu.\n";
        cout << "\n";
        if (fail_count > 0) {
            cout << "Invalid Entry! Please Seclect Y, N, or A.\n";
        }
        cin >> good_entry;
        if (good_entry == 'Y') {
            ofstream waypointfile ("m_wptdb", ios::app);
        waypointfile << "\n";
            waypointfile << newentry.wpt_friendly_name << "\n";
            waypointfile << newentry.wpt_ident << "\n";
            waypointfile << newentry.lat << " lat    " << "\n";
            waypointfile << newentry.lon << " lon    " << "\n";
            waypointfile << newentry.wpt_misc_data << "\n";
            cout << "Data written to file\n";
            system("read -p \"Press a key to continue...\" -n 1 -s");
            main_menu();
        }
        else if (good_entry == 'y') {
            ofstream waypointfile ("m_wptdb", ios::app);
        waypointfile << "\n";
            waypointfile << newentry.wpt_friendly_name << "\n";
            waypointfile << newentry.wpt_ident << "\n";
            waypointfile << newentry.lat << " lat    " << "\n";
            waypointfile << newentry.lon << " lon    " << "\n";
            waypointfile << newentry.wpt_misc_data << "\n";
            cout << "Data written to file\n";
            system("read -p \"Press a key to continue...\" -n 1 -s");
            main_menu();
        }
        else if (good_entry == 'a') {
            main_menu();
        }
        else if (good_entry == 'A') {
            main_menu();
        }
        else if (good_entry == 'n') {
            wptadd();
        }
        else if (good_entry == 'N') {
            wptadd();
        }
        else {
            fail_count = 1;
            input_verify();
        }
    }

    If you need to see other parts of the program code, let me know and I'll post it.


    I know the 'system(“clear”);' statement is not the preferred way of doing things, I intend to replace it with portable code later, for now it's just a shortcut for debug/testing purposes.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It looks like you're using quite a few global variables, stop and learn to pass the required variables to and from your functions as required.

    You should also verify that the file open operation succeeds. And you can check for 'Y' and 'y' in the if() statement without the else if().

    Code:
    else if (good_entry == 'a' || good_entry == 'A') {
    Also it appears to me that this function is doing too much. IMO it should just verify the information and return a bool value back to the calling function indicating that the data is good or bad.

    Be careful with all that recursion as well.

    Jim

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    The problem is that while the program runs without error, the user input is not written to the file
    Have you tried closing the files before the program exits ?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You mean right after the last file operation, right?

    Just closing the files before the program terminates should not make a difference, but because of all the recursive calls in this function you may be on the correct path.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c code requested- really hard problem
    By amnakhan786 in forum C Programming
    Replies: 4
    Last Post: 11-13-2011, 03:21 AM
  2. Ofstream Problem
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2010, 05:21 PM
  3. problem using ofstream
    By stien in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2007, 01:48 PM
  4. Pathfinding assistance requested
    By Thantos in forum Game Programming
    Replies: 1
    Last Post: 06-02-2004, 07:03 PM
  5. ofstream problem
    By XSquared in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2003, 01:06 PM

Tags for this Thread