this program uses fstream
data file
format:
productid productname quantity purchasedate
Code:
10212 Nescafe 20 10/12/05
13114 Pepsi 11 10/27/05
11234 Nescafe 14 11/01/05
10212 Nescafe 30 11/05/05
10212 Pepsi 32 11/15/05
10212 Nescafe 45 11/21/05
13114 Nescafe 26 12/01/05
18456 Pepsi 23 12/20/05
12223 Frito -2 09/14/05
12223 Frito 20 9/14/05
12223 Frito 20 13/21/05
10212 Pepsi 32 01/15/06
code
Code:
#include <iostream>
#include <fstream>
#include <string>
#define SIZE 100
using namespace std;
class store
{
public:
int custnum;
string product;
int quantity;
string month;
string day;
string year;
};
store STORE1[SIZE];
int num_lines(char name[]) // this function counts the number of lines in file
{
char templine[100];
int line=0;
fstream in_file(name,ios::in);
if(!in_file)
{
cout << "error opening file";
exit(1);
}
while (!in_file.eof()){
in_file.getline(templine,100);
line++;
}
in_file.close();
return line;
}
void period()
{
}
void all()
{
cout << "THIS IS A TEST TO SEE IF THE FUNCTION WORKS" << endl;
}
void menu()
{
int option,line,custnum,quantity,tmp,y=0;
string product;
char month[80], day[80], year[80];
char name[20];
cout << "Enter name of file to load:";
cin >> name;
line = num_lines(name);
fstream in_file(name,ios::in);
if(!in_file)
{
cout << "error opening file";
exit(1);
}
for(int x=0;x<line;x++)
{
in_file >> custnum;
in_file >> product;
in_file >> quantity;
in_file.getline(month,100,'/');
in_file.getline(day,100,'/');
in_file >> year;
in_file.ignore(80,'\n');
tmp = atoi(month);
if(tmp > 12 || tmp < 1 || strlen(month) < 3 || quantity <0)
cout << "Line " << x << " has invalid format" << endl;
else {
STORE1[y].custnum = custnum;
STORE1[y].product = product;
STORE1[y].quantity = quantity;
STORE1[y].month = month;
STORE1[y].day = day;
STORE1[y].year = year;
y++;
}
}
in_file.close();
cout << "The month is " << STORE1[0].month << endl;
do {
cout << "Please select a menu option:" << endl;
cout << "1)Product sales in a given period" << endl;
cout << "2)Information about all sales" << endl;
cout << "3)Quit the program" << endl;
cout << "Option (1,2,3):";
cin >> option;
switch(option) {
case 1:
period();
break;
case 2:
all();
break;
}
} while(option!=3);
}
int main()
{
menu();
system("PAUSE");
return 0;
}
my plan for option one is for the user to enter the product name and specify a time period...
for example:
Code:
Please select a menu option:
1)Product sales in a given period
2)Information about all sales
3)Quit the program
Option (1,2,3): 1
Enter a product name: Nescafe
Enter a starting date: 10/01/05
Enter an ending date: 12/01/05
it would show
Code:
Product: Nescafe
Total sales: 135
First time sales: 60
Other sales: 75
i have a problem finding which are the first time purchases within the user entered time period... how would i do this?