0

Hey guys i am having a problem with this function. Its a fucntion to generate bills and i want to add a condition in where if users enters a wrong product number th program should not proceed. Any help is appreciated.

   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 Product No. Of The Product : ";
     cin>>order_arr[c];
     cout<<"\nQuantity in number : ";
     cin>>quan[c];
     c++;
     cout<<"\nDo You Want To Order Another Product ? (y/n)";
     cin>>ch;
    }while(ch=='y' ||ch=='Y');
    cout<<"\n\nThank You For Placing The Order";getch();clrscr();
      cout<<"\n\n********************************INVOICE************************\n";
      cout<<"\nPr No.\tPr Name\tQuantity \tPrice \tAmount \tAmount after discount\n";
      for(int x=0;x<=c;x++)
    {
         fp.open("Shop.dat",ios::in);
         fp.read((char*)&pr,sizeof(product));
          while(!fp.eof())
            {
            if(pr.retpno()==order_arr[x])
                {
                 amt=pr.retprice()*quan[x];
                 damt=amt-(amt*pr.retdis()/100);
                 cout<<"\n"<<order_arr[x]<<"\t"<<pr.retname()<<"\t"<<quan[x]<<"\t\t"<<pr.retprice()<<"\t"<<amt<<"\t\t"<<damt;
                 total+=damt;
                }
            fp.read((char*)&pr,sizeof(product));
            }

         fp.close();
     }
       cout<<"\n\n\t\t\t\t\tTOTAL = "<<total;
    getch();
    }
3
Contributors
4
Replies
27
Views
1 Year
Discussion Span
Last Post by David W
0

Your code would do well to have a fresh start ...

Do NOT use getch if you want your code to be portable.

Do not mix C and C++ style.

Use functions with good discriptive names to handle each job.

For example, your input could be handled by a function and your validation of the input could also be handled by a function.

You my like to check here ...
http://developers-heaven.net/forum/index.php/topic,2019.0.html
to get some ideas about using functions to validate input.

Edited by David W

0
  1. Use endl instead of '\n' for newlines in C++ code, especially when pushing to cout. It will force the stream to flush the data to output, which the newline will not.
  2. Where do you declare/define the pr variable? It is not in the function you show.
  3. Just saying you are having a problem is not particularly helpful. You also need to be more specific about what is going on.
  4. @DavidW has some other good suggestions.
0

In particular, if you wish to validate a pruduct number entered by the user, you could maintain a data stucture of valid numbers, either in a file, or loaded into memory in some container like a C++ (sorted) vector and do a binary search / table lookup ... or you may like to use a C++ map or a C++ set container to hold the data for you to do your lookup of unique numbers to see if one already exist in the container.

0

And ... if you would like to see an example of customer order validation, using some of the power of C++, just ask.

Edited by David W

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.