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();
    }

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.

  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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.