Thread: Reading data and Saving Data to file while using structures

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Reading data and Saving Data to file while using structures

    I'm having trouble finishing this program i have been working on. It works fine with the stuff i had edited out, but once i try to add the cost option the program doesn't get past saving and opening the initial data. The cost data appears to be the same as all my other options any idea where I'm going wrong?

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    
    
    void main ()
    
    {	
    int numanswer;
    int redo;
    int i;
    int k;
    
    char answer,answer2, answer3, answer4, phone_search[20];
    struct dvds_r_us
    {
    char title[30];
    char main_star[30];
    char length[15];
    char genre[30];
    char renter_name[30];
    char renter_number[20];
    char cost[15];
    };
    
    dvds_r_us renter_search[100];
    
    ofstream outfile;
    ifstream infile;
    
    cout << "Is this the first time you have run this program?(y/n)\n";
    cin >> answer2;
    
    
    
    if(answer2 == 'y' || answer2 == 'Y')
    {
    
    outfile.open("renters.DAT",ios::out);
    if (outfile)
    {
    		
    for (k = 1; k <= 100; k++)
    {
    
    strcpy(renter_search[k].title,"Empty");
    strcpy(renter_search[k].main_star,"Empty");
    strcpy(renter_search[k].length,"Empty");
    strcpy(renter_search[k].genre,"Empty");
    strcpy(renter_search[k].renter_name,"Empty");
    strcpy(renter_search[k].renter_number,"Empty");
    //strcpy(renter_search[k].cost,"Empty");
    
    outfile << renter_search[k].title << endl;
    outfile << renter_search[k].main_star << endl;
    outfile << renter_search[k].length << endl;
    outfile << renter_search[k].genre << endl;
    outfile << renter_search[k].renter_name << endl;
    outfile << renter_search[k].renter_number << endl;
    //outfile << renter_search[k].cost << endl;
    
    }
    }
    else
    {
    cout << "An error has occured opening the file.\n";
    }
    
    outfile.close();
    
    }
    
    do
    {
    
    answer3 = 0;
    
    infile.open("renters.DAT",ios::in);
    
    if (infile)
    {
    for (k = 1; k <= 100; k++)
    {
    infile.get(renter_search[k].title,30);
    infile.ignore(80,'\n');
    infile.get(renter_search[k].main_star,30);
    infile.ignore(80,'\n');
    infile.get(renter_search[k].length,15);
    infile.ignore(80,'\n');
    infile.get(renter_search[k].genre,30);
    infile.ignore(80,'\n');
    infile.get(renter_search[k].renter_name,30);
    infile.ignore(80,'\n');
    infile.get(renter_search[k].renter_number,20);
    infile.ignore(80,'\n');
    //infile.get(renter_search[k].cost,15);
    //infile.ignore(80,'\n');
    }
    
    cout << "Do you wish to search for a renter by his/her phone number?(y/n)\n";
    cin >> answer4;
    
    if (answer4 == 'y' || answer4 == 'Y')
    {
    cout << "Enter the phone number of the person you are searching for\n";
    cin >> phone_search;
    cin.ignore(80, '\n');
    
    for (i = 1; i <= 100; i++)
    {
    if (strcmp(renter_search[i].renter_number,phone_search) == 0)
    {
    cout << "The current statistics for the DVD number which is the " << i << " most popular." << endl;
    cout << "1.(title) " << renter_search[i].title << endl;
    cout << "2.(main_star) " << renter_search[i].main_star << endl;
    cout << "3.(length) " << renter_search[i].length << endl;
    cout << "4.(genre) " << renter_search[i].genre << endl;
    cout << "5.(renter's name) " << renter_search[i].renter_name << endl;
    cout << "6.(renter's phone #) " << renter_search[i].renter_number << endl;
    //cout << "7.(cost) " << renter_search[i].cost << endl;
    }
    }
    }
    if (answer4 == 'n' || answer4 == 'N')
    {
    
    
    cout << "Please enter the number with which you wish to view the stats of that DVD. \n";
    cin >> i;
    
    cout << "The current statistics for the DVD number which is the " << i << " most popular." << endl;
    cout << "1.(title) " << renter_search[i].title << endl;
    cout << "2.(main_star) " << renter_search[i].main_star << endl;
    cout << "3.(length) " << renter_search[i].length << endl;
    cout << "4.(genre) " << renter_search[i].genre << endl;
    cout << "5.(renter's name) " << renter_search[i].renter_name << endl;
    cout << "6.(renter's phone #) " << renter_search[i].renter_number << endl;
    //cout << "7.(cost) " << renter_search[i].cost << endl;
    }
    }
    else
    {
    cout << "An error occurred while opening the file.\n";
    }
    
    infile.close();
    
    cout << "Do you wish to change any of these stats?(y/n)\n";
    cin >> answer;
    
    if (answer == 'y' || answer == 'Y')
    {
    
    do
    {
    		
    redo = 0;
    
    cout << "Which statistic do you wish to change?(#)\n";
    cin >> numanswer;
    
    if (numanswer == 1)
    {
    cout << "Enter the new title for this movie.\n";
    cin >> renter_search[i].title;
    }
    
    if (numanswer == 2)
    {
    cout << "Enter the new main star for this movie.\n";
    cin >> renter_search[i].main_star;
    }
    
    if (numanswer == 3)
    {
    cout << "Enter the new length for this movie.\n";
    cin >> renter_search[i].length;
    }
    
    if (numanswer == 4)
    {
    cout << "Enter the new genre for this movie.\n";
    cin >> renter_search[i].genre;
    }
    
    if (numanswer == 5)
    {
    cout << "Enter the new renter for this movie.\n";
    cin >> renter_search[i].renter_name;
    }
    if (numanswer == 6)
    {
    cout << "Enter the new renter's phone number for this movie.\n";
    cin >> renter_search[i].renter_number;
    }
    if (numanswer == 7)
    {
    cout << "Enter the new cost for this movie.\n";
    cin >> renter_search[i].cost;
    }
    
    if (numanswer < 1 || numanswer > 7)
    {
    redo = 1;
    }
    
    }
    while (redo == 1);
    
    }
    	
    
    
    outfile.open("renters.DAT",ios::out);
    for (i = 1; i <= 100; i++)
    {
    if (outfile)
    {
    outfile << renter_search[i].title << endl;
    outfile << renter_search[i].main_star << endl;
    outfile << renter_search[i].length << endl;
    outfile << renter_search[i].genre << endl;
    outfile << renter_search[i].renter_name << endl;
    outfile << renter_search[i].renter_number << endl;
    //outfile << renter_search[i].cost << endl;
    }
    else
    {
    cout << "An error has occured opening the file.\n";
    }
    }
    
    outfile.close();
    
    cout << "Do you wish to continue with this program?(y/n)";
    cin >> answer3;
    
    }
    while (answer3 != 'n');
    
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Some better indentation would be helpful:

    Code:
    #include <iostream.h>  // Use <iostream> no ".h" instead
    #include <string.h>    // Use <cstring> no ".h" instead
    #include <fstream.h>   // Use <fstream> no ".h" instead
    
    using namespace std;  // Needed due to change in headers
    
    
    void main ()  // main always returns an int
    {	
        int numanswer;
        int redo;
        int i;
        int k;
    
        char answer,answer2, answer3, answer4, phone_search[20];
        struct dvds_r_us
        {
            // It's usually better to work with string containers instead of C-style character arrays
            char title[30];
            char main_star[30];
            char length[15];
            char genre[30];
            char renter_name[30];
            char renter_number[20];
            char cost[15];
        };
    
        dvds_r_us renter_search[100];
    
        ofstream outfile;
        ifstream infile;
    
        cout << "Is this the first time you have run this program?(y/n)\n";
        cin >> answer2;
    
    
        if(answer2 == 'y' || answer2 == 'Y')
        {
            // ios::out is assumed for ofstream objects
            outfile.open("renters.DAT",ios::out);
            if (outfile)
            {
                // Major issue: Arrays are 0 based in C/C++, that means your loop should look like:
                // ( k = 0; k < 100; ++k )
                // You are currently writing to an index that does not exist
                for (k = 1; k <= 100; k++)
                {
                    // All these strcpy's are pointless, just write "Empty" directly to the file
                    strcpy(renter_search[k].title,"Empty");
                    strcpy(renter_search[k].main_star,"Empty");
                    strcpy(renter_search[k].length,"Empty");
                    strcpy(renter_search[k].genre,"Empty");
                    strcpy(renter_search[k].renter_name,"Empty");
                    strcpy(renter_search[k].renter_number,"Empty");
                    //strcpy(renter_search[k].cost,"Empty");
    
                    outfile << renter_search[k].title << endl;
                    outfile << renter_search[k].main_star << endl;
                    outfile << renter_search[k].length << endl;
                    outfile << renter_search[k].genre << endl;
                    outfile << renter_search[k].renter_name << endl;
                    outfile << renter_search[k].renter_number << endl;
                    //outfile << renter_search[k].cost << endl;
    
                }
            }
            else
            {
                cout << "An error has occured opening the file.\n";
            }
    
            outfile.close();
    
        }
    
        do
        {
            answer3 = 0;
    
            // ios::in is assumed for ifstream objects
            infile.open("renters.DAT",ios::in);
            if (infile)
            {
                // More array indexing problems
                for (k = 1; k <= 100; k++)
                {
                    infile.get(renter_search[k].title,30);
                    infile.ignore(80,'\n');
                    infile.get(renter_search[k].main_star,30);
                    infile.ignore(80,'\n');
                    infile.get(renter_search[k].length,15);
                    infile.ignore(80,'\n');
                    infile.get(renter_search[k].genre,30);
                    infile.ignore(80,'\n');
                    infile.get(renter_search[k].renter_name,30);
                    infile.ignore(80,'\n');
                    infile.get(renter_search[k].renter_number,20);
                    infile.ignore(80,'\n');
                    //infile.get(renter_search[k].cost,15);
                    //infile.ignore(80,'\n');
                }
    
                cout << "Do you wish to search for a renter by his/her phone number?(y/n)\n";
                cin >> answer4;
    
                if (answer4 == 'y' || answer4 == 'Y')
                {
                    cout << "Enter the phone number of the person you are searching for\n";
                    cin >> phone_search;
                    cin.ignore(80, '\n');
    
                    // More array indexing problems
                    for (i = 1; i <= 100; i++)
                    {
                        if (strcmp(renter_search[i ].renter_number,phone_search) == 0)
                        {
                            cout << "The current statistics for the DVD number which is the " << i << " most popular." << endl;
                            cout << "1.(title) " << renter_search[i ].title << endl;
                            cout << "2.(main_star) " << renter_search[i ].main_star << endl;
                            cout << "3.(length) " << renter_search[i ].length << endl;
                            cout << "4.(genre) " << renter_search[i ].genre << endl;
                            cout << "5.(renter's name) " << renter_search[i ].renter_name << endl;
                            cout << "6.(renter's phone #) " << renter_search[i ].renter_number << endl;
                            //cout << "7.(cost) " << renter_search[i ].cost << endl;
                        }
                    }
                }
                if (answer4 == 'n' || answer4 == 'N')
                {
                    cout << "Please enter the number with which you wish to view the stats of that DVD. \n";
                    cin >> i;
    
                    cout << "The current statistics for the DVD number which is the " << i << " most popular." << endl;
                    cout << "1.(title) " << renter_search[i ].title << endl;
                    cout << "2.(main_star) " << renter_search[i ].main_star << endl;
                    cout << "3.(length) " << renter_search[i ].length << endl;
                    cout << "4.(genre) " << renter_search[i ].genre << endl;
                    cout << "5.(renter's name) " << renter_search[i ].renter_name << endl;
                    cout << "6.(renter's phone #) " << renter_search[i ].renter_number << endl;
                    //cout << "7.(cost) " << renter_search[i ].cost << endl;
                }
            }
            else
            {
                cout << "An error occurred while opening the file.\n";
            }
    
            infile.close();
    
            cout << "Do you wish to change any of these stats?(y/n)\n";
            cin >> answer;
    
            if (answer == 'y' || answer == 'Y')
            {
                do
                {
                    redo = 0;
    
                    cout << "Which statistic do you wish to change?(#)\n";
                    cin >> numanswer;
    
                    if (numanswer == 1)
                    {
                        cout << "Enter the new title for this movie.\n";
                        cin >> renter_search[i ].title;
                    }
    
                    if (numanswer == 2)
                    {
                        cout << "Enter the new main star for this movie.\n";
                        cin >> renter_search[i ].main_star;
                    }
    
                    if (numanswer == 3)
                    {
                        cout << "Enter the new length for this movie.\n";
                        cin >> renter_search[i ].length;
                    }
    
                    if (numanswer == 4)
                    {
                        cout << "Enter the new genre for this movie.\n";
                        cin >> renter_search[i ].genre;
                    }
    
                    if (numanswer == 5)
                    {
                        cout << "Enter the new renter for this movie.\n";
                        cin >> renter_search[i ].renter_name;
                    }
    
                    if (numanswer == 6)
                    {
                        cout << "Enter the new renter's phone number for this movie.\n";
                        cin >> renter_search[i ].renter_number;
                    }
    
                    if (numanswer == 7)
                    {
                        cout << "Enter the new cost for this movie.\n";
                        cin >> renter_search[i ].cost;
                    }
    
                    if (numanswer < 1 || numanswer > 7)
                    {
                        redo = 1;
                    }
    
                } while (redo == 1);
            }
    	
            // ios::out is assumed for ofstream objects
            outfile.open("renters.DAT",ios::out);
            // More array indexing problems
            for (i = 1; i <= 100; i++)
            {
                if (outfile)
                {
                    outfile << renter_search[i ].title << endl;
                    outfile << renter_search[i ].main_star << endl;
                    outfile << renter_search[i ].length << endl;
                    outfile << renter_search[i ].genre << endl;
                    outfile << renter_search[i ].renter_name << endl;
                    outfile << renter_search[i ].renter_number << endl;
                    //outfile << renter_search[i ].cost << endl;
                }
                else
                {
                    cout << "An error has occured opening the file.\n";
                }
            }
    
            outfile.close();
    
            cout << "Do you wish to continue with this program?(y/n)";
            cin >> answer3;
    
        } while (answer3 != 'n');
    
        return 0;
    
    }
    I stopped looking since there was quite a few basic things you already need to fix. Make those fixes, if you still are having your problems then repost your new code after making sure the code is nicely formated, (that's what the "Preview Post" button is for).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Thanks program is running fine now, though your header suggestions didnt seem to work, next time i post a code it will be a lot easier to read

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. saving data into a file
    By afrm in forum C Programming
    Replies: 4
    Last Post: 05-22-2006, 09:54 AM
  3. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM