post #1 of 1
Thread Starter 
Iv got a project that I have been working on. There are a few things I need a little help with in it.

Ok so the first thing is the program is not writing output to my file. Not sure if I did the file output wrong or what.

Code:

file << Inventory.descript << "\
" << Inventory.Quantity 
                  << "\
" << Inventory.Wholesale << "\
" << Inventory.Retail 
                  << "\
" << Inventory.Date;
                  
                  
             file.close(); 
The other thing I needed help with was making the Item structure an array. I tried doing it before but couldn't get it to work correctly when trying to call certain pieces of it. Basically this project calls for a program that allows the user to perform the following:

*Add new records to a file
*Display any record in the file
*Change any record in the file

So based on the code I have, any suggestions? Thanks for help.

Entire Project code:
Code:

#include<iostream>

#include<fstream>
//#include<conio.h>
using namespace std;

struct Item
{
       string descript;
       int    Quantity;
       double Wholesale;
       double Retail;
       string Date;
};

       
int main()
{
    const int NUMBER=15;
    Item Inventory; 
    int answer=5;
    fstream file;
    
    
    do
    {
                
        system("CLS");
        cout << "\    Choose one of the following options: \
\
";
        cout << "\    1: Add new record\
\    2: Display a record\
\    3: Modify a record\
\    4: Exit the program\
";
        cin >> answer;
        
        if(answer==1)
        {
             file.open("Items.txt", ios::out);
             if(file.fail())
             {
                 cout << "Could not open file...\
";
                 system("PAUSE");
                 return 0;
             }   
             system("CLS");
             cout << "You are entering a new item record. ";
             system("PAUSE");
             system("CLS");
             cout << "Enter the item description: ";
             cin >> Inventory.descript;
             system("CLS");
             cout << "Enter the quantity: ";
             cin >> Inventory.Quantity;  
             system("CLS");
             cout << "Enter the wholesale cost: $";
             cin >> Inventory.Wholesale;
             system("CLS");
             cout << "Enter the retail cost: $";
             cin >> Inventory.Retail;
             system("CLS");
             cout << "Enter today's date(mm/dd/yyyy): ";
             cin >> Inventory.Date;
             system("CLS");
             
             cout << "The new item has been added. You entered:\
\

                  << Inventory.descript << "  " << Inventory.Quantity << "  $" 
                  << Inventory.Wholesale << "  $"  << Inventory.Retail << "  "  
                  << Inventory.Date << "  \
\
"  << endl;
                  
             system("PAUSE");
             
             file << Inventory.descript << "\
" << Inventory.Quantity 
                  << "\
" << Inventory.Wholesale << "\
" << Inventory.Retail 
                  << "\
" << Inventory.Date;
                  
                  
             file.close();
             //add new record
             
        }
        else if(answer==2)
        {
            // int size = sizeof(Inventory)/sizeof(Inventory[0]);  
          //   for(int i=0; i<size; i++)
          //   {
                  //file.open("Items.txt", ios::out);
                  
                  system("CLS");
                  cout << "\    Items:\
\
";
                  cout << "Item description: Quantity on Hand: Wholesale: Retail: Date Added:\
\
\
";   
                  cout << Inventory.descript << "  \    \    " << Inventory.Quantity << "  \    " 
                       << Inventory.Wholesale << "  "  << Inventory.Retail << "  "  
                       << Inventory.Date << "  "  << endl;
                       
                  file.open("Items.txt", ios::out);
                  
                  string FileInfo[15][5];
                  int count =0;
                  while( !file.eof() )
                     {
                         for(int i=0; i<5; i++)
                         {
                              file >> FileInfo[count][i]; 
                         }
                         
                         count++;
                     }  
                  
                  file.close();
                  system("PAUSE"); 
         //    }   
             
        }
        else if(answer==3)
        {
             //Modify file
             
        }
        else if(answer==4)
        {
             break;
        }
        else
        {
            system("CLS");
            cout << "You entered " << answer << " which is an invalid number...\
\
";
            system("PAUSE");
        }
        system("CLS");
        
        
    }while(true);
    
    
    return 0;