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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void askForFileName(string &fileName);
void displayMenu(string &choice);
void enterInfo(string fileName, string names[], double sales[]);
void displayInfo(string fileName);
int findLargest(double sales[], int &sz, string fileName);
int main()
{
//declare variables
double sales[100];
string names[100];
string fileName = "";
string choice = "";
int size = 0;
int indexOfLargest = 0;
//intro
cout << "Welcome to the sales Record Program!" << endl;
askForFileName(fileName);
indexOfLargest = findLargest(sales, size, fileName);
do{
displayMenu(choice);
if (choice == "1")
{
enterInfo(fileName, names, sales);
}
else if (choice == "2")
{
displayInfo(fileName);
cout << size;
cout << indexOfLargest;
}
}while (choice != "x");
cout << "Goodbye!" << endl;
}
//this one works just fine
void askForFileName(string &fileName)
{
cout << "Please enter the filename: ";
getline(cin, fileName);
ifstream fin(fileName.c_str(), ofstream::app);
if (fin)
{
cout << "File opened." << endl;
fin.close();
}
return;
}
//this one works fine too
void displayMenu(string &choice)
{
cout << "\nWould you like to:" << endl;
cout << " 1 - add a sales record" << endl;
cout << " 2 - view the sales report" << endl;
cout << " x - exit" << endl;
cout << " > ";
getline(cin, choice);
return;
}
//so is this one
void enterInfo(string fileName, string names[], double sales[])
{
//declare variables
string newName = "";
double newSales = 0;
int counter = 0;
cout << "Enter the name: ";
getline(cin, newName);
cout << "\nEnter the sales: ";
cin >> newSales;
cin.clear();
cin.ignore(100, '\n');
//read file
ifstream fin(fileName.c_str(), ofstream::app);
if (fin)
{
while (isalnum(fin.peek()) && counter < 100)
{
getline(fin, names[counter]);
fin >> sales[counter];
fin.ignore(100, '\n');
if (names[counter] != newName)
counter++;
}
fin.close();
}
//write to file
ofstream fout(fileName.c_str(), ofstream::app);
if (fout)
{
for (int i = 0; i < counter; i++)
{
fout << names[i] << "\n";
fout << sales[i] << "\n";
}
fout << newName << "\n";
fout << newSales << "\n";
fout.close();
cout << "success!\n";
}
else
cout << "failure.\n";
return;
}
//this one works? but it's very weird
//practice one!
void displayInfo(string fileName)
{
string names;
double sales = 0;
ifstream fin(fileName.c_str());
if (fin)
{
cout << "file opened" << endl;
while (isalnum(fin.peek()))
{
getline(fin, names);
fin >> sales;
fin.ignore(5, '\n');
cout << setw(10) << names << setw(10) << sales << " *asterisks* " << endl;
}
}
return;
}
//currently editing this bit
//cannot figure out a way to find the biggest number. I thought it was working but I guess not.
int findLargest(double sales[], int &sz, string fileName)
{
double numbers = 0;
string names;
int indexOfLargest = 0;
ifstream fin(fileName.c_str());
if (fin)
{
cout << "file opened" << endl;
while (isalnum(fin.peek()))
{
getline(fin, names);
fin >> numbers;
fin.ignore(5, '\n');
sz++;
}
//size = 4
if (sz == 0) return -1; // no data means no highest index
for (int i = 0; i < sz; i++)
if (sales[i] > sales[indexOfLargest])
indexOfLargest = i;
cout << indexOfLargest;
fin.close();
}
return indexOfLargest;
}
| |