My program compiles but doesn't actually work. It seems to only display the last record entered where I want it to display all the records that are entered. I know I am close but I just can't see where the issue is! Any Help is appreciated. Thank you in advance.
Write two programs which will work with a structure.
a) The first program will use a structure to store the following inventory data in a file:
Item Description
Quantity on Hand
Wholesale Cost
Retail Cost
Date Added to Inventory
The program will write five records to an output file for different items; the quantity, costs, and date for each item will be different from the other items.
b) The second program will read the file created by the first program using the same structure. The second program will calculate and display the following information:
total wholesale value of the inventory
total retail value of the inventory
total quantity of all items in the inventory*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
constint DES = 20;
//declare variables
void Add();
void Display();
void Edit();
struct Inventory
{
char item[DES];
int qty;
int wcost;
int rcost;
char date[10];
};
//Main function
int main()
{
int choice;
do
{
cout << "MENU" << endl;
cout << "1. Add Record: " << endl;
cout << "2. Display Records: " << endl;
cout << "Please enter your selection." << endl;
cin >> choice;
switch (choice)
{
case 1:
Add();
break; //Add record
case 2:
Display();
break; //Display record
default: cout << "Invalid Selection" << endl;
}
} while
(choice <= 2);
system("PAUSE");
return 0;
}
//Add funtion
void Add()
{
fstream fout;
constint size = 3;
char ch;
int i = 0;
fout.open("Records.txt", ios::out);
Inventory inv;
//get data
do
{
cout << "Enter item description: " << endl;
cin.ignore();
cin.getline(inv.item, DES);
cout << "Enter quantity: " << endl;
cin >> inv.qty;
cout << "Enter wholesale cost: " << endl;
cin >> inv.wcost;
cout << "Enter retail cost: " << endl;
cin >> inv.rcost;
cout << "Enter date: " << endl;
cin.ignore();
cin.getline(inv.date, 10);
//write record to file
fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
cout << "Do you want to add another record? " << endl;
cin >> ch;
} while
(ch == 'Y' && 1 < 3);
//close the file
fout.close();
}
//"Display" function
void Display()
{
fstream fout;
fout.open("Records.txt", ios::in);
Inventory inv;
fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
while (!fout.eof())
{
cout << "\nDescription\t: ";
cout << inv.item;
cout << "\nQuantity\t: ";
cout << inv.qty;
cout << "\nWholesale Cost\t: ";
cout << inv.wcost;
cout << "\nRetail Cost\t: ";
cout << inv.rcost;
cout << "\nDate\t: ";
cout << inv.date;
fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
}
//close the file
fout.close();
}
MENU
1. Add Record:
2. Display Records:
Please enter your selection.
1
Enter item description:
asd
Enter quantity:
4
Enter wholesale cost:
55
Enter retail cost:
56
Enter date:
12/10/10
Do you want to add another record?
Y
Enter item description:
www
Enter quantity:
5
Enter wholesale cost:
1
Enter retail cost:
55
Enter date:
34/56/78
Do you want to add another record?
N
MENU
1. Add Record:
2. Display Records:
Please enter your selection.
2
Description : asd
Quantity : 4
Wholesale Cost : 55
Retail Cost : 56
Date : 12/10/10
Description : www
Quantity : 5
Wholesale Cost : 1
Retail Cost : 55
Date : 34/56/78MENU
1. Add Record:
2. Display Records:
Please enter your selection.
Your problem is with line fout.open("Records.txt", ios::out);
It will set write pointer to the beginning of the file. Meaning that you will start writing from the beginning, overwriting previous information.
Use either ios::cout | ios::ate or ios::cout | ios::app (read what they do to select one) or manually seek to the end of the file
MiiNiPaa! Thank you! I can't believe it was that simple. I kept trying to rewrite a whole portion of my code and was getting no where! Thank you kindly