Stumped on a project

Here is the project:

In this program you will make suggestions as to what stock to buy among the ones that are owned. There is a file named stocks.dat. Copy this file into your directory.

Each line of the file has four values on it. The first is the stock symbol. The second is the number of shares owned. The third value is the current price of the stock. The fourth and last is the buying price for the stock. When you read these values in you are to store these values in an array for each of the types of values (4 arrays). You will have an array for the stock symbol, an array for the number of shares owned, an array for the current price of the stock, and finally an array for the buying price of the stock.

Once you have the values read into the arrays you are to find stocks that may be possible “buy” candidates. A buy candidate is defined as follows:

The number shares owned must be less than 100 shares.
The current price of the stock should be less than or equal to the buying price of the stock.

The output of this program should be as follows:

Stock Symbol Current Price Bought Price Shares Owned
=========== =========== ========== ===========
MSFT 18-3/4 21-3/4 55
AAPL 572-1/2 800 75

And so on for as many stocks that you can find that meet the criteria above.

Please note you must display the stock price as a whole number followed by a fraction as shown above. For example, if the current stock price is 18.25, this would be displayed as 18-1/4. You must convert any fractional part to its equivalent whole fraction.

All of the tasks in this program should be carried out in functions. The main function should consist of calls to functions only. You cannot use GLOBAL variables. Any information that needs to be passed between functions should be either pass-by-value or pass-by-reference (if you need to change the value).


--

Here is the code:


#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;

const int ARRAY_SIZE = 100;

//Instruct the user.
void instructUser()
{
cout << "This program will yadda yadda" << endl;
}

//Open file.
bool openFile(ifstream &fileInput)
{
string fileName;
cout << "Enter file name: ";
cin >> fileName;

const char *file = fileName.c_str();

fileInput.open(file, ios::in);

if(!fileInput)
{
cout << "***ERROR: Cannot open file";
return false;
}
else{
return true;
}
}

double stringToDouble(const string &Text)
{
stringstream ss(Text);
double result;
return ss >> result ? result : 0;
}


double processFractionInput(string input)
{
string intPart = "";
string numerator = "";
string denomenator = "";
int i, j, k;

for(i = 0; i < input.length() && input[i] != '-'; i++)
{
intPart = intPart + input[i];
}
for(j = i + 1; j < input.length() && input[j] != '/'; j++)
{
numerator = numerator + input[j];
}
for(k = j + 1; k < input.length(); k++)
{
denomenator = denomenator + input[k];
}
return 1;
// return stringToDouble(intPart) +
// (stringToDouble(numerator) / stringToDouble(denomenator));
}


//Read data from file.
void readFile(ifstream &fileInput, string symbol[], double currentPrice[],
double boughtPrice[], double shares[], int &numElements)
{
numElements = 0;
while(!fileInput.eof() && numElements < ARRAY_SIZE)
{
string currPrice, boughtPrice;
fileInput >> symbol[numElements];
fileInput >> currPrice;
currentPrice[numElements] = processFractionInput(currPrice);
fileInput >> boughtPrice;
boughtPrice[numElements] = processFractionInput(boughtPrice);
fileInput >> shares[numElements];
numElements++;
}
}

bool isABuy(double currPrice, double boughtPrice, double numShares)
{
if(numShares < 100)
{
if(currPrice <= boughtPrice)
{
return true;
}
else
return false;
}
else
return false;
}

void printResults(string symbol[], double currentPrice[],
double boughtPrice[], double shares[], int numElements)
{
cout << setw(20) << "Stock Symbol";
cout << setw(20) << "Current Price";
cout << setw(20) << "Bought Price";
cout << setw(20) << "Shares Owned";
cout << endl;
cout << setw(20) << "===========";
cout << setw(20) << "===========";
cout << setw(20) << "===========";
cout << setw(20) << "===========";
cout << endl;

for(int i = 0; i < numElements; i++)
{
if(isABuy(currentPrice[i], boughtPrice[i], shares[i]))
{
cout << setw(20) << symbol[i];
cout << setw(20) << currentPrice[i];
cout << setw(20) << boughtPrice[i];
cout << setw(20) << shares[i];
cout << endl;
}
}
}

int main()
{

ifstream file;

string symbol[ARRAY_SIZE];
double currentPrice[ARRAY_SIZE];
double boughtPrice[ARRAY_SIZE];
double shares[ARRAY_SIZE];
int numElements;

instructUser();

if(openFile(file))
{
readFile(file, symbol, currentPrice, boughtPrice, shares, numElements);
printResults(symbol, currentPrice, boughtPrice, shares, numElements);

}

return 0;
}

Last edited on
Part of your problem is probably contained in the following snippet:
1
2
3
4
5
6
7
void readFile(ifstream &fileInput, string symbol[], double currentPrice[],
              double boughtPrice[], double shares[], int &numElements)
{
   numElements = 0;
   while(!fileInput.eof() && numElements < ARRAY_SIZE)
   {
      string currPrice, boughtPrice;


Both of the variables in the last line share their names with parameter names. That means that these local variables will be used in this section of the code, not the parameters, which is probably not what you want.

Also have you learned about structures or classes? Using a structure or class to hold all the variables you will read from the file will make things much easier.

Topic archived. No new replies allowed.