dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need help in c++ filing. woking good in dev c++ but giving problem in MS visual 2010

  1. #1
    Join Date
    Feb 2018
    Posts
    1

    Question Need help in c++ filing. woking good in dev c++ but giving problem in MS visual 2010

    This project is working good in dev c++.
    In MS visual studio 2010 my modify function not working correctly. Problem is. when I modify last record using modify function record is modified as well as record at 2nd position appended at last. This problem is not occurred when programmed executed using dev c++. Please help me to figure out problem.

    Code:
    //***************************************************************
    //                   HEADER FILE USED IN PROJECT
    //***************************************************************
    #include<iostream>
    #include<conio.h>
    #include<fstream>
    #include<cstdlib>
    #include<iomanip>
    using namespace std;
    
    
    //***************************************************************
    //                   CLASS USED IN PROJECT
    //****************************************************************
    class item
    {
        int ino;
        char name[50];
        float price,qty,dis;
    public:
        /*item()
        {
            ino=0;
            price=0;
        }*/
        void add_item()
            {
                cout<<"\nPlease Enter the Item No. of the Item ";
                cin>>ino;
                cout<<"\n\nPlease Enter The Name of The Item ";
                cin.ignore();
                gets_s(name);
                cout<<"\nPlease Enter The Price of The Item ";
                cin>>price;
                cout<<"\nPlease Enter The Discount (%) ";
                cin>>dis;
            }
    
        void show_item()
        {
            cout<<"\nThe Item No. of The Item : "<<ino;
            cout<<"\nThe Name of The Item : ";
            puts(name);
            cout<<"The Price of The Item : "<<price;
            cout<<"\nDiscount (%) : "<<dis;
        }
    
        int  retino()
        {return ino;}
    
        float retprice()
        {return price;}
    
        char* retname()
        {return name;}
    
        float retdis()
        {return dis;}
    
    };         //class ends here
    
    
    
    //***************************************************************
    //        global declaration for stream object, object
    //****************************************************************
    fstream fp;
    item it;
    
    
    //***************************************************************
    //        function to write in file
    //****************************************************************
    
    void write_item()
       {
        fp.open("Shop.dat",ios::out|ios::app);
        it.add_item();
        fp.write((char*)&it,sizeof(item));
        fp.close();
        cout<<"\n\nThe Product Has Been Created ";
        _getch();
       }
    
    
    //***************************************************************
    //        function to read all records from file
    //****************************************************************
    
    
    void display_all()
    {
        system("cls");
        cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
        fp.open("Shop.dat",ios::in);
        while(fp.read((char*)&it,sizeof(item)))
        {
         it.show_item();
         cout<<"\n\n====================================\n";
         _getch();
         }
        fp.close();
        _getch();
    }
    
    
    //***************************************************************
    //        function to read specific record from file
    //****************************************************************
    
    
    void display_sp(int n)
    {
        int flag=0;
        fp.open("Shop.dat",ios::in);
        while(fp.read((char*)&it,sizeof(item)))
        {
         if(it.retino()==n)
            {
             system("cls");
             it.show_item();
             flag=1;
            }
        }
        fp.close();
    if(flag==0)
     cout<<"\n\nRecord not exist";
        _getch();
    }
    
    
    //****************************************************************
    //        function to modify record of file
    //****************************************************************
    
    
    void modify_item()
    {
        int no,found=0;
        system("cls");
        cout<<"\n\n\tTo Modify ";
        cout<<"\n\n\tPlease Enter The Product No. of The Product";
        cin>>no;
        fp.open("Shop.dat",ios::in|ios::out);
        while(((char*)&it,sizeof(item)) && found==0)
           {
            if(it.retino()==no)
               {
                it.show_item();
                cout<<"\nPlease Enter The New Details of Product"<<endl;
                it.add_item();
                long pos=sizeof(item);
                fp.seekp(-pos,ios::cur);
                fp.write((char*)&it,sizeof(item));
                cout<<"\n\n\t Record Updated";
                found=1;
               }
             }
        fp.close();
        if(found==0)
        cout<<"\n\n Record Not Found ";
        _getch();
    }
    
    
    //***************************************************************
    //        function to delete record of file
    //****************************************************************
    
    
    void delete_item()
       {
        int no;
        system("cls");
        cout<<"\n\n\n\tDelete Record";
        cout<<"\n\nPlease Enter The Item no. of The Item You Want To Delete";
        cin>>no;
        fp.open("Shop.dat",ios::in|ios::out);
        fstream fp2;
        fp2.open("Temp.dat",ios::out);
        fp.seekg(0,ios::beg);
        while(fp.read((char*)&it,sizeof(item)))
        {
         if(it.retino()!=no)
            {
             fp2.write((char*)&it,sizeof(item));
             }
         }
        fp2.close();
        fp.close();
        remove("Shop.dat");
        rename("Temp.dat","Shop.dat");
        cout<<"\n\n\tRecord Deleted ..";
        _getch();
        }
    
    
    //***************************************************************
    //        function to display all products price list
    //****************************************************************
    
        void menu()
        {
         system("cls");
         fp.open("Shop.dat",ios::in);
         if(!fp)
         {
           cout<<"ERROR!!! FILE COULD NOT BE OPEN\n\n\n Go To Admin Menu to create File";
           cout<<"\n\n\n Program is closing ....";
           _getch();
           exit(0);
         }
    
         cout<<"\n\n\t\tProduct MENU\n\n";
          cout<<"====================================================\n";
          cout<<"P.NO.\t\tNAME\t\t\tPRICE\n";
          cout<<"====================================================\n";
    
          while(fp.read((char*)&it,sizeof(item)))
         {
           cout<<it.retino()<<"\t\t"<<it.retname()<<"\t\t"<<it.retprice()<<endl;
        }
         fp.close();
    }
    
    
    
    
    //***************************************************************
    //        function to place order and generating bill for Products
    //****************************************************************
    
       void place_order()
       {
        int  order_arr[50],quan[50],c=0;
        float amt,damt,total=0;
        char ch='Y';
        menu();
        cout<<"\n============================";
        cout<<"\n    PLACE YOUR ORDER";
        cout<<"\n============================\n";
        do{
         cout<<"\n\nEnter The Item No. Of The Item : ";
         cin>>order_arr[c];
         cout<<"\nQuantity in number : ";
         cin>>quan[c];
         c++;
         cout<<"\nDo You Want To Order Another Product ? (y/n)";
         ch=_getche();
        }while(ch=='y' ||ch=='Y');
        cout<<"\n\nThank You For Placing The Order";
        _getch();
        system("cls");
          cout<<"\n\n********************************INVOICE************************\n";
          cout<<"\nPr No.\tPr Name\t\tQuantity \tPrice \tAmount \tDiscounted Amount\n";
          for(int x=0;x<=c;x++)
        {
             fp.open("Shop.dat",ios::in);
             fp.read((char*)&it,sizeof(item));
              while(!fp.eof())
                {
                if(it.retino()==order_arr[x])
                    {
                     amt=it.retprice()*quan[x];
                     damt=amt-(amt*it.retdis()/100);
                     cout<<"\n"<<order_arr[x]<<"\t"<<it.retname()<<"\t"<<quan[x]<<"\t\t"<<it.retprice()<<"\t"<<amt<<"\t\t"<<damt;
                     total+=damt;
                    }
                fp.read((char*)&it,sizeof(item));
                }
    
             fp.close();
         }
           cout<<"\n\n\t\t\t\t\tTOTAL = "<<total;
        _getch();
        }
    
    //***************************************************************
    //        INTRODUCTION FUNCTION
    //****************************************************************
    
    void intro()
    {
     system("cls");
     //gotoxy(31,11);
     cout<<"SUPER MARKET";
     //gotoxy(35,14);
     cout<<"BILLING";
     //gotoxy(35,17);
     cout<<"PROJECT";
     cout<<"\n\nMADE BY : ABC";
     cout<<"\n\nSCHOOL : KFUEIT";
     _getch();
    
    }
    
    
    
    
    //***************************************************************
    //        ADMINSTRATOR MENU FUNCTION
    //****************************************************************
    void admin_menu()
    {
      system("cls");
      char ch2;
      cout<<"\n\n\n\tADMIN MENU";
      cout<<"\n\n\t1. ADD ITEM";
      cout<<"\n\n\t2. DISPLAY ALL ITEM";
      cout<<"\n\n\t3. QUERY ";
      cout<<"\n\n\t4. MODIFY ITEM";
      cout<<"\n\n\t5. DELETE ITEM";
      cout<<"\n\n\t6. VIEW ITEM MENU";
      cout<<"\n\n\t7. BACK TO MAIN MENU";
      cout<<"\n\n\tPlease Enter Your Choice (1-7) ";
      ch2=_getche();
      switch(ch2)
      {
        case '1': system("cls");
              write_item();
              break;
        case '2': display_all();
            break;
        case '3':
               int num;
               system("cls");
               cout<<"\n\n\tPlease Enter The Product No. ";
               cin>>num;
               display_sp(num);
               break;
          case '4': modify_item();break;
          case '5': delete_item();break;
          case '6': menu();
            _getch();
          case '7': break;
          default:cout<<"\a";
              admin_menu();
       }
    }
    
    
    //***************************************************************
    //        THE MAIN FUNCTION OF PROGRAM
    //****************************************************************
    
    
    int main(void)
    {
      char ch;
      intro();
      do
        {
          system("cls");
          cout<<"\n\n\n\tMAIN MENU";
          cout<<"\n\n\t1. CUSTOMER";
          cout<<"\n\n\t2. ADMINISTRATOR";
          cout<<"\n\n\t3. EXIT";
          cout<<"\n\n\tPlease Select Your Option (1-3) ";
          ch=_getche();
          switch(ch)
          {
             case '1': system("cls");
                   place_order();
                   //getch();
                   break;
              case '2': admin_menu();
                    break;
              case '3':
                  exit(0);
              default :
                  cout<<"\a";
        }
        }while(ch!='3');
        return 0;
    }
    
    //***************************************************************
    //                END OF PROJECT
    //***************************************************************

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,410

    Re: Need help in c++ filing. woking good in dev c++ but giving problem in MS visual 2

    Code:
    while(((char*)&it,sizeof(item)) && found==0)
    This looks wrong. With a comma separated statement, the expressions are evaluated left to right with the last expression executed being the value of the statement. In this case the address of it as a pointer to char is obtained and discarded. sizeof(item) is always greater than 0 so is treated as true as part of a logical and. So effectively this statement becomes

    Code:
    while (found == 0)
    I think you mean

    Code:
    while (fp.read((char*)&it, sizeof(item)) && found == 0)
    Last edited by 2kaud; February 18th, 2018 at 05:38 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  3. #3
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    333

    Re: Need help in c++ filing. woking good in dev c++ but giving problem in MS visual 2

    You are not reading every register to seek for the element to be modified. So the while is running loose till the end of the file. Do as you did on the delete function reading sequentially every element after the seek. the sequence is:
    Code:
    open the file;
    found = false;
    while not found
       read the element;                <<<======= MISSING
      is this the element I want? 
        if so 
          go back one register;
          save it
          found = true;
        else  Seek the next element;
    end while;
    
    if found = false
      print "element not found"
    end if
    ...
    Last edited by Rabelo; February 18th, 2018 at 05:49 AM. Reason: Fixed code tags

  4. #4
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    333

    Re: Need help in c++ filing. woking good in dev c++ but giving problem in MS visual 2

    Actually there is a litle bug on this algorythm that will break the program (or freeze the computer... )
    if the element doses not exist the while wonŽt stop. So as this seems to be a school exercise IŽll let to you the solution...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)