Hi i have a program that reads data from a file.. if the quantity or date is wrong it would ignore that entry completely for further comparison how would i go about putting only valid types into the array..
text file :
Code:
10212 Nescafe 20 1/12/05
13114 Pepsi -11 10/27/05
Code:
#include <iostream>
#include <fstream>
#include <string>
#define SIZE 100
using namespace std;
class store
{
public:
int custnum;
string product;
int quantity;
char tmp[20];
char month[80];
char day[80];
char year[80];
};
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,tmp;
char name[20],c;
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 >> STORE1[x].custnum;
in_file >> STORE1[x].product;
in_file >> STORE1[x].quantity;
in_file.getline(STORE1[x].month,100,'/');
in_file.getline(STORE1[x].day,100,'/');
in_file >> STORE1[x].year;
in_file.ignore(80,'\n');
tmp = atoi(STORE1[x].month);
if(tmp > 12 || tmp < 1 || strlen(STORE1[x].month) < 3)
cout << "Line " << x << " has invalid date" << endl;
if(STORE1[x].quantity < 0)
cout << "Line " << x << " has invalid quantity " << endl;
}
in_file.close();
cout << "The month is " << STORE1[1].year << 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;
}