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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <Windows.h>
#undef min
#undef MAX
#include <fstream>
using namespace std; // <--- Best not to use.
#define CLS cls();
//Prototype Statements
void displayOutput(string[], string[], string[], double[], int);
int getData(const string, string[], string[], string[], double[], int &, double& total);
void SetColor(short foreground, short background);
void cls();
const int MAX_INVENTORY = 500;
int main()
{
const string filename = "motorsports inventory.txt"; // <--- Changed.
int discount{}, responce{};
double total{};
string model_number[MAX_INVENTORY], type[MAX_INVENTORY], description[MAX_INVENTORY];
double retail_price[MAX_INVENTORY]{};
int num_inventory{};
//system("color f0");
SetColor(0, 15);
CLS;
if(responce=getData(filename, model_number, type, description, retail_price, num_inventory, total))
return responce;
displayOutput(model_number, type, description, retail_price, num_inventory);
}
void displayOutput(string model_number[], string type[], string description[], double retail_price[], int num_inventory)
{
cout << endl << endl << setprecision(2) << fixed;
for (int c = 0; c < num_inventory; c++)
{
cout << left << setw(5) << model_number[c]
<< " " << setw(15) << type[c]
<< right << setw(7) << description[c]
<< setw(25) << "$" << retail_price[c] << endl;
}
}
int getData(const string filename, string model_number[], string type[], string description[], double retail_price[], int &num_inventory, double& total)
{
ifstream infile;
infile.open(filename); // <--- From C++11 on the ".c_str()" is not needed.
if (infile.fail()) // <--- if (!infile) will check for more than just the "fail" bit.
{
cerr << "\n Error: Cannot open file. "; // <--- Changed.
return 1;
}
string line;
int c = 0; // <--- This is not needed. Just use "num_inventory".
while (getline(infile, model_number[c]))
{
getline(infile, type[c]);
getline(infile, description[c]);
infile >> retail_price[c];
total += retail_price[c];
infile.ignore();
getline(infile, line); //read blank line
c++;
}
//infile.close(); // <--- Not required as the dtor will close the file when the function looses scope.
num_inventory = c; // <--- Not needed if you just use "num_inventory".
return 0;
}
void SetColor(short foreground, short background)
{
WORD consoleColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//consoleColor = (csbi.wAttributes & 0xF0) + (ColorPicker & 0x0F);
consoleColor = (foreground + (background * 16));
SetConsoleTextAttribute(hStdOut, consoleColor);
}
} // End SetColor()
void cls()
{
const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode;
if (hOut != INVALID_HANDLE_VALUE)
if (GetConsoleMode(hOut, &dwMode))
if (SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING))
printf("\x1b[H\x1b[2J"); // Home and clear screen
}
|