Use your platform to create a project that input 10 instrument quotes from your keyboard, save the quotes to an array, and then save them to a text file.
Here, instrument includes only stock and option. The stock quote includes stock name(12 characters), bid price, bid size, ask price, ask size, and issue exchange. The option quote includes option name (26 characters), underlying name(12 characters), bid price, bid size, ask price, ask size, and expire date.
The instrument quote, the stock quote and the option quote must be handled by classes. The methods, input, save to an array, and save to text file must be implemented by polymorphism concept.
I am having trouble with the polymorphism concept...I have read about virtual functions and such but I'm not sure how to implement them in this situation.
My text file is also just outputting the last myoption[I] and mystock[I] in the respective tables how ever many times I. So in the end there is the same option information 10 times and the same stock information 10 times. Not sure how to fix that.
#include <iostream>
#include <fstream>
#include <string>
#include "Instrument.h"
usingnamespace std;
int I,m;
int main(){
cout <<"How many instruments will you input?"<<endl;
cin >> I;
Option myOption[10];//Initializes option array
Stock myStock[10];//Initializes stock array
for( int f=0; f<I; f++)//runs a loop with a choice for the option or stock
{
cout <<"Is this an Option or Stock?"<<endl;
cout <<"Type '1' for Option and '2' for Stock..."<<endl;
cin >> m;
switch(m)//Has user decide what the instrument is
{
case 1:
cout <<"What is the option name?"<<endl;
cin >>myOption[I].option;
cout <<"what is the underlying of the option?"<<endl;
cin >>myOption[I].under;
cout <<"What is the bid price?"<<endl;
cin >>myOption[I].bid_price;
cout <<"What is the bid size?"<<endl;
cin >>myOption[I].bid_size;
cout <<"What is the ask price?"<<endl;
cin >>myOption[I].ask_price;
cout <<"What is the ask size?"<<endl;
cin>>myOption[I].ask_size;
cout<<"What is the Expire date?"<<endl;
cout<<"Month(Numeric-MM): ";
cin>>myOption[I].t_month;
cout<<"Date(DD): ";
cin>>myOption[I].t_day;
cout<<"Year(YYYY): ";
cin>>myOption[I].t_year;
break;
case 2:
cout<<"What is the stock name?"<<endl;
cin>>myStock[I].stock;
cout<<"What is the bid price?"<<endl;
cin>>myStock[I].bid_price;
cout<<"What is the bid size?"<<endl;
cin>>myStock[I].bid_size;
cout<<"What is the ask price?"<<endl;
cin>>myStock[I].ask_price;
cout<<"What is the ask size?"<<endl;
cin>>myStock[I].ask_size;
cout<<"What is the Issue Exchange?"<<endl;
cin>>myStock[I].Issue;
break;
default :
cout<<"Not the correct input. Please enter '1' for Option and '2' for Stock."<<endl;
I=I+1;
break;
};
};
// Creates the text file 'Instrument'
ofstream myfile;
myfile.open("Instrument.txt");
//Creates the table for the Option Array
myfile << "Option Name | ";
myfile.width(12); //width function used so that there is consistency in the spacing
myfile<< "Underlying |" ;
myfile.width(10);
myfile<< "Bid Price |" ;
myfile.width(10);
myfile << "Bid Size |";
myfile.width(10);
myfile << "Ask Price |";
myfile.width(10);
myfile << "Ask Size |";
myfile.width(10);
myfile << "Expiration";
myfile.width(10);
myfile<< "\n";
//Loop to fill in the option table
for (int z=0;z<I;z++)
{
myfile << myOption[I].option << " | ";
myfile.width(12); //width function used so that there is consistency in the spacing
myfile<< myOption[I].under << " | " ;
myfile.width(10);
myfile<< myOption[I].bid_price << " | " ;
myfile.width(10);
myfile << myOption[I].bid_size;
myfile.width(10);
myfile << myOption[I].ask_price << " | ";
myfile.width(10);
myfile << myOption[I].ask_size << " | ";
myfile.width(10);
myfile << myOption[I].t_month << "/" << myOption[I].t_day << "/" << myOption[I].t_year;
myfile.width(10);
myfile << "\n";
}
//Creates the table for the Stock Array
myfile<<"\n";
myfile<<"\n";
myfile<<"\n";
myfile << "Stock Name | ";
myfile.width(12);
myfile<< "Bid Price |" ;
myfile.width(10);
myfile << "Bid Size |";
myfile.width(10);
myfile << "Ask Price |";
myfile.width(10);
myfile << "Ask Size |";
myfile.width(10);
myfile << "Issue Exchange";
myfile.width(10);
myfile<< "\n";
//Loop to fill in the stock table
for (int z=0;z<I;z++)
{
myfile << myStock[I].stock;
myfile.width(12);
myfile << myStock[I].bid_price;
myfile.width(10);
myfile << myStock[I].bid_size;
myfile.width(10);
myfile << myStock[I].ask_price;
myfile.width(10);
myfile << myStock[I].ask_size;
myfile.width(10);
myfile << myStock[I].Issue;
myfile.width(10);
myfile << "\n";
}
myfile.close();
return 0;
};
I think EricDu was referring to myfile << myStock[I].stock; and the rest in the loop. It should be myfile << myStock[z].stock; since that is the variable being increased up to the variable I.