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 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include<stdlib.h>
#include<string>
#include<iostream>
#include<iomanip>
#include<fstream>
#include <cstring>
void openFile(ofstream& filename);
void getFileName (char fileName[]);
void getInputs(char stockNum[], char description[], float& cost, float& price, int& quantity);
void writeToFile(ofstream& fileName, char stockNum[], char description[], float cost, float price, int quantity);
int main()
{
char stockNum[6];
char description[21];
float cost;
float price;
int quantity;
ofstream fileName;
getFileName(char fileName[]);
openFile(ofstream& filename);
while (strlen(stockNum) != 0)
{
getInputs(stockNum, description, cost, price, quantity);
if (cost != -999)
{
writeToFile(fileName, stockNum, description, cost, price, quantity);
}
cin.ignore(999,'\n');
cout << stockNum << endl;
cin.getline(stockNum, 5, '\n');
}
return 0;
}
//============================================================================================
void writeToFile(ofstream& fileName, char stockNum[], char description[], float cost, float price, int quantity)
{
fileName.open("stock.dat");
fileName << stockNum << " " << description << " " << cost << " " << price << " " << quantity << endl;
fileName.close();
exit(1);
}
//==============================================================================================
void getFileName(char fileName[])
{
cout<<"Name the file"<<endl;
cin.getline(fileName,101,'\n');
if(strlen(fileName)==0)
{
cout<<"No user input, stock.dat created"<<endl;
strcpy(fileName,"stock.dat");
}
else if(strstr(fileName,".dat"))
{
strcat(fileName,".dat");
cout<<fileName<<"was created.";
}
}
//=================================================================================================
void getInputs(char stockNum[], char description[], float& cost, float& price, int& quantity)
{
cout << "Enter stock Number";
cin.getline(stockNum, 6 , '\n');
cout << "Enter a description" << endl;
cin.getline(description, 20, '\n');
cout << "Enter cost between 0 and 10000.00(-999 to exit)" << endl;
cin >> cost;
while ((cost < 0 || cost > 10000) && cost != -999)
{
cout << "Enter cost between 0 and 10000 typing in -999 to exit" << endl;
cin >> cost;
cin.ignore(999,'\n');
}
if (cost != -999)
{
cout << "Enter the price:" << endl;
cin >> price;
cout << "How many in stock?" << endl;
cin >> quantity;
}
}
//=================================================================================================
void openFile(ofstream& fileName)
{
char fileNameStr[101];
cout << "File name?";
cin.getline(fileNameStr,101,'\n');
fileName.open(fileNameStr);
if(fileName.fail())
{
cout<<"Error Opening File"<<fileNameStr<<endl;
fileName.clear();
cout<<"File name?";
cin.getline(fileNameStr,100,'\n');
fileName.open(fileNameStr);
}
}
|