1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef struct
{
char code[5];
char name[30];
int quantity;
}MATERIAL;
int readMat(MATERIAL m[], int size);
void listMat(MATERIAL m[], int size);
void searchMat(MATERIAL m[], int size);
int main(void)
{
MATERIAL mat[50]; //store up to 50 materials
int MaterialNum = readMat(mat, 50);
listMat(mat, MaterialNum);
searchMat(mat, MaterialNum);
return 0;
}
int readMat(MATERIAL m[], int size)
{
ifstream inFile;
int numMat = 0;;
inFile.open("materials.txt");
if (!inFile)
{
cout << "Error opening file !\nCheck the name of the file !\n";
exit(1);
}
else
{
for (int i = 0; !inFile.eof(); i++)
{
fflush(stdin);
inFile.getline(m[i].code, 5, ',');
inFile.getline(m[i].name, 30, ',');
inFile >> m[i].quantity;
numMat++;
}
inFile.close();
}
return numMat;
}
void listMat(MATERIAL m[], int numMat)
{
int i;
cout << fixed << left;
cout << setw(10) << "Code";
cout << setw(50) << "Type";
cout << setw(10) << "Quantity";
cout << endl;
for (i = 0; i < numMat; i++)
{
cout << setw(10) << m[i].code;
cout << setw(50) << m[i].name;
cout << setw(10) << m[i].quantity;
cout << endl;
}
}
void searchMat(MATERIAL m[], int numMat)
{
char searchCode[5];
int found = 0;
cout << "\nEnter the material code number:";
cin >> searchCode;
for (int i = 0; i < numMat; i++)
{
if (_strcmpi(m[i].code, searchCode) == 0)
{
system("cls");
cout << fixed << left;
cout << setw(10) << "Code";
cout << setw(50) << "Type";
cout << setw(10) << "Quantity";
cout << endl;
cout << setw(10) << m[i].code;
cout << setw(50) << m[i].name;
cout << setw(10) << m[i].quantity;
cout << endl;
found++;
}
}
if (found == 0)
{
system("CLS");
cout << "The material ID does not exist. \n";
}
}
|