C++ Files

Discussion in 'Software' started by button, Apr 22, 2004.

  1. button

    button Private E-2

    I have been trying to get the search facility below to work but it throws an error because of the memory being allocated I think.
    Please Please take a look at the code and see you can do anything...Much appreciated. Buttons

    #include <iostream.h> //Pre Processor Directives
    #include <fstream.h> //Libary for Input funcitons
    #include <iomanip.h> //Input/Output manipulation libary

    void main()
    {
    ofstream file("Database"); //Create a new file
    file.open("Database");

    char* name[9];
    name[1]="HP4p";
    name[2]="HP5L";
    name[3]="HP6p";
    name[4]="Cannon2A";
    name[5]="Cannon3B";
    name[6]="Epson2";
    name[7]="Epson3";
    name[8]="Epson4A";

    float cost[9];
    cost[1]=32.99;
    cost[2]=24.99;
    cost[3]=30.00;
    cost[4]=23.45;
    cost[5]=31.99;
    cost[6]=25.00;
    cost[7]=30.00;
    cost[8]=27.99;

    int quantity[9];
    quantity[1]=100;
    quantity[2]=50;
    quantity[3]=75;
    quantity[4]=30;
    quantity[5]=80;
    quantity[6]=10;
    quantity[7]=25;
    quantity[8]=30;

    char name8[13]="Cartridge";
    char cost8[11]="£00.00p";
    char quantity8[12]="Quantity";

    {
    file<<name<<' '<<cost<<' '<<quantity;
    }
    file.close(); //close the file

    int num=0,x;
    char sought[15]; //Declare Variables

    cout<<"Display all records press '1' "<<"Do a Search press '2' "<<" '0' to Quit "<<endl;
    cin>>num;

    while(!num==0)
    {
    if (num==1)
    {
    cout<<"Total Items in stock \n"<<endl<<endl;

    ifstream file("Database",ios::in); //ios::in for input from the file
    //while(!file.eof())
    cout<<setw(20)<<setfill(' ')<<setiosflags(ios::left)<<name8;
    cout<<setw(10)<<setfill(' ')<<setiosflags(ios::right)<<cost8;
    cout<<setw(12)<<setfill(' ')<<setiosflags(ios::right)<<quantity8<<endl;

    for (int x = 1; x < 9; x++)
    {
    cout<<endl<<setw(20)<<setfill(' ')<<setiosflags(ios::left)<<name[x];
    cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<cost[x];
    cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<quantity[x];
    }
    }

    cout<<endl<<endl<<"Display all records press '1' ";
    cout<<"Do a Search press '2' "<<" '0' to Quit "<<endl;
    cin>>num;

    if (num==2)
    {
    cout<<endl<<"To search by the printers make type in the name e.g HP4p: Type 'N' for no more"<<endl;
    cin>>sought;

    ifstream file("Database"); //open file for input FROM file
    //file.seekg(0); //set file pointer to first character

    while (strcmp(sought,"N")!=0)
    {
    strcpy(name[0],sought);

    x=9;

    while (strcmp(name[x],sought)!=0)
    x--;

    if (strcmp(name[x],sought)==0 && x>0)
    {
    cout<<endl<<setw(20)<<setfill(' ')<<setiosflags(ios::left)<<name;
    cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<cost;
    cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<quantity<<endl;
    }
    else
    {
    cout<<"No such item in stock"<<endl<<endl;
    }
    }
    file.close();

    cout<<"Display all records press '1' ";
    cout<<"Do a Search press '2' "<<" '0' to Quit"<<endl;
    cin>>num;

    }
    }
    }
     
  2. chaslang

    chaslang MajorGeeks Admin - Master Malware Expert Staff Member

    You're getting an error because you have not added #include <string.h> so that you can use the strcmp() functions.
     
  3. iamien

    iamien Cptn "Eh!"

    and please use code tags
    [ code]
    [/ code]
    without spaces before code obvious :d
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds