Problems calling file back in and displaying it?

This is a discussion on Problems calling file back in and displaying it? within the C++ Programming forums, part of the General Programming Boards category; hey guys, im writing a CD Database program in Visual C++, i can write out to the file no probs, ...

  1. #1
    yo_da84
    Guest

    Problems calling file back in and displaying it?

    hey guys, im writing a CD Database program in Visual C++, i can write out to the file no probs, but when i call it back in if theres more than one record in the DB file it throws a wobbler and gives a black screen in Dos. heres the code:
    Yo_da84@hotmail.com if you can help.
    thanks

    #include<iostream>
    #include<fstream>
    #include<cstring>

    using namespace std;


    void addToDb();
    void displayDb();
    void searchByArtist();
    void searchByTitle();
    int menu();

    void main()
    {
    int choice;

    choice=menu();
    while (choice !=5)
    {
    switch (choice)
    {
    case 1: addToDb();
    break;

    case 2: searchByArtist();
    break;

    case 3: searchByTitle();
    break;

    case 4: displayDb();
    break;

    default:
    cout<<"\nInvalid Choice";
    }
    choice = menu();
    }
    cout<<"\nTerminating Program\n\n";
    }

    int menu()
    {
    int option;
    cout<<"\nMain Menu\n\n";
    cout<<"1. Add new CD\n2. Search list by Artist";
    cout<<"\n3. Search by CD Title\n4. Display whole Db";
    cout<<"\n5. Quit Program";
    cout<<"\n\nSelect Now: ";
    cin>>option;
    cin.get();
    return option;
    }

    void addToDb()
    {
    char cdnum[3], title[30], artist[25], tracks[3], rel[10];

    ofstream outfile;
    outfile.open("cddb.dat", ios::app);

    if(!outfile)
    {
    cout<<"\n!!Error opening File!!\n";
    }
    else
    {
    cout<<"\nEnter CD: ";
    cin.getline(cdnum, 3);
    cout<<"\nEnter Album Title: ";
    cin.getline(title, 30);
    cout<<"\nEnter Name of Artist: ";
    cin.getline(artist, 25);
    cout<<"\nEnter Tracks: ";
    cin.getline(tracks, 3);
    cout<<"\nEnter Album Release Date: ";
    cin.getline(rel, 10);

    outfile<<cdnum<<"/"<<title<<"/"<<artist<<"/"<<tracks<<"/"<<rel<<"/"<<endl;

    }
    outfile.close();
    }

    void displayDb()
    {

    char cdnum[3], title[30], artist[25], tracks[3], rel[10];

    ifstream infile;
    infile.open("cddb.dat", ios::in);

    if(!infile)
    {
    cout<<"\nCan't Open File\n";
    }
    else
    {

    while (!infile.eof())
    {

    infile.getline(cdnum, 3, '/');
    infile.getline(title, 30, '/');
    infile.getline(artist, 25, '/');
    infile.getline(tracks, 3, '/');
    infile.getline(rel, 10, '/');

    cout<<artist<<"\t"<<title<<"\t"<<tracks<<"\t"<<rel <<"\t"<<cdnum;
    }
    cout<<endl;
    }
    infile.close();
    }

    void searchByArtist()
    {

    }

    void searchByTitle()
    {

    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Please read this, and also read this.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I highly recommend that you sit down and debug your own program. As a future software developer, you need as much practice as possible.

    Kuphryn

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21