#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct Item 
{
    string name;
    int quantity;
    double unitPrice;
    string description;
};    

struct ItemTotal

{
    string name;
    int quantity;
    double unitPrice;
    string description;
};   


void sort(Item list[], int n) 
{                   
   for(int i=0;i<n;i++)
   {
           for(int j=0;j<n-i-1;j++)
                       if (list[j].description<list[j+1].description)
                           swap(list[j],list[j+1]);
   }
}

void display(Item a[], int size) 
{
    for(int i = 0; i < size; i++)
        cout << a[i].name << " " << a[i].quantity << " " << a[i].unitPrice << "\n";
             cout << "\n";
}  

void swap(Item& c1, Item& c2) 
{
    Item temp = c1;
    c1 = c2;
    c2 = temp;
}  

int main(int argc, char *argv[])
{
   string infilename,outfilename;

    ifstream infile;
    char inFileName[20],outFileName[20];

    do
    {
         infile.clear();
         cout<<"Please enter file containing details of items: "<<flush;
         cin>>inFileName; 
         infile.open(inFileName); 
    } while(!infile.good());

      cout<<"Please enter output file: "<<flush;
        cin>>outFileName; 
         ofstream outfile;
         outfile.open(outFileName); 
         string line;
         getline(infile,line);
         Item Item1[10];   
          for(int i=0;i<10;i++)
         {
                  infile>>Item1[i].name;
             for (int k=0;k<5;k++)
                  infile>>Item1[i].unitPrice[k];

          Item1[i].quantity=0;
             for(int j=0;j<4;j++)
             {          Item1[i].quantity+=Item1[i].unitPrice[j];
                        Item1[i].quantity+=Item1[i].unitPrice[4]/2;
             }
         }

          for(int i=0;i<10;i++)
                    outfile<<setw(10)<<setfill(' ')<<left<<Item1[i].name<<setw(10)<<setfill(' ')<<right<<"- "<<fixed<<setprecision(2)<<Item1[i].quantity<<endl;


    system("PAUSE");
    return EXIT_SUCCESS;
}
Salem commented: you have 17 posts, and haven't figured out code tags -6

Recommended Answers

All 5 Replies

Can anyone please find whats wrong in this code

Yes. It doesn't compile and therefore won't work.

read this
and this

Use code tags.

You have given us nothing to go on here, does the compiler throw errors at you? Is the output wrong or not what you expected?

Remember, a decent question gives a decent answer.

Of course, your code is not place in BB tag.

first of, include stringstream library.

sort-function cannot use swap-function because it hasn't been declared before its called.\

and finally, indexing your itemlist-array like this

Item1[i].unitPrice[j];

will not work. For every item in your list there is only one unitPrice.

Dear you dont include #include<string> . Now check it.

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.