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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int ReadStkData(char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int maxSize);
void SortStkData(char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int size);
int main()
{
// declarations
const int max_Size = 15; // declaring array size
char trSym[max_Size][9];
char cmpName[max_Size][31];
int numShrs[max_Size];
float pPrc[max_Size];
float cPrc[max_Size];
//calling modules to perform actions
ReadStkData(trSym, cmpName, numShrs, pPrc, cPrc, max_Size);
SortStkData(trSym, cmpName, numShrs, pPrc, cPrc, max_Size);
system("pause");
return 0;
}
int ReadStkData( char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int max_Size)
{
ifstream inCode; // input file stream variable
char inFile[21]; // variable to store name of text file
// get file name
cout << "Enter the input file name: ";
cin.getline(inFile, max_Size);
// open file
inCode.open(inFile);
if (!inCode)
{
cout << "Unable to open the file " << inFile; // file not valid
exit(EXIT_FAILURE);
}
// initialize variables using input file
int i = 0;
while (i < max_Size)
{
inCode >> trSym[i];
inCode >> cmpName[i];
inCode >> numShrs[i];
inCode >> pPrc[i];
inCode >> cPrc[i];
i++;
}
inCode.close();
// return variables
return trSym, cmpName, numShrs, pPrc, cPrc, max_Size;
}
void SortStkData(char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int size)
{
int temp;
// using a bubble sort
for (int pass=0; pass < size -1; pass++) // passes
for (int i=0; i < size - i; i++) // one pass
if (trSym[i] > trSym[i+1]) // compare
{
temp = trSym[i][9];
trSym[i][9] = trSym[i+1][9];
trSym[i+1][9] = temp;
}
for(int index = 0; index < size; index++)
{
cout << setw(9) << trSym[index]
<< setw(31) << cmpName[index] << setw(10) << numShrs[index] << endl;
}
}
|