Reputation: 4419
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
//Initializing some variables.
string PairOfAverage = "blank";
string ConsoleInput = "";
double OrderEntry = 0;
double OrderAmount = 0;
double WeightFactor1[] = {};
double WeightFactor2 = 0;
string inputString = "";
int main() {
cout<< "Do you wish to determine average position for a pair? " << endl;
cout << "Please answer \"true\" or \"false\"."<< endl;
cin >> ConsoleInput;
//Here you should input "true" or "false".
bool DetermineAverage = ToBool(ConsoleInput);
cout << "Please enter the currency pair Symbol in XXXYYY format:";
cin >> PairOfAverage;
//Here you should input a string.
for (int i=0;DetermineAverage==true;i++) {
cout << "Please enter the order's entry price: ";
cin >> inputString;
//Here input should be a double.
OrderEntry = stod (inputString,NULL);
cout << "Please enter the order's amount: ";
cin >> inputString;
//Here input should be a double.
OrderAmount = stod (inputString,NULL);
sleep(1);
cout << OrderAmount << " <-- OrderAmount" << endl;
WeightFactor1[i] = OrderEntry*OrderAmount;
WeightFactor2 += OrderAmount;
cout << "weight factor: " << WeightFactor2 << endl;
cout << "Do you want to introduce more orders for the same pair? " << endl;
cout << "Please answer \"true\" or \"false\"."<< endl;
cin >> ConsoleInput;
//Here you should input "true" or "false".
DetermineAverage = ToBool(ConsoleInput);
sleep(1);
if (DetermineAverage == false) {
double sum = 0.0;
for (int j = 0; j <= i ; j++){
sum+=WeightFactor1[j];
cout<<"sum equals: " << sum << " " << j << endl;
}
double result = sum/WeightFactor2;
cout<< "Average Price for " << WeightFactor2 << " units of " << PairOfAverage << " is: " << result << endl;
}
}
return 0;
}
//This is a function to parse the string to bool type.
bool ToBool(string ConsoleInput){
bool InputBool ;
if (ConsoleInput == "false"){
InputBool = false;
}
else if(ConsoleInput == "true"){
InputBool = true;
}
return(InputBool);
}
//The error I get is the result of variable WeightFactor2. It does not add up
//the sum of the introduced valued the where passed as string through cin>>
//and then parsed to double.
//I hope you can help me on this.
Upvotes: 1
Views: 161
Reputation: 1729
You need to correct your usage of double WeightFactor1[] = {} to vector< double > WeightFactor1;
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
//Initializing some variables.
string PairOfAverage = "blank";
string ConsoleInput = "";
double OrderEntry = 0;
double OrderAmount = 0;
vector< double > WeightFactor1;
double WeightFactor2 = 0;
string inputString = "";
bool ToBool(string ConsoleInput){
bool InputBool ;
if (ConsoleInput == "false"){
InputBool = false;
}
else if(ConsoleInput == "true"){
InputBool = true;
}
return(InputBool);
}
int main() {
cout<< "Do you wish to determine average position for a pair? " << endl;
cout << "Please answer \"true\" or \"false\"."<< endl;
cin >> ConsoleInput;
//Here you should input "true" or "false".
bool DetermineAverage = ToBool(ConsoleInput);
cout << "Please enter the currency pair Symbol in XXXYYY format:";
cin >> PairOfAverage;
//Here you should input a string.
for (int i=0;DetermineAverage==true;i++) {
cout << "Please enter the order's entry price: ";
cin >> OrderEntry;
//Here input should be a double.
cout << "Please enter the order's amount: ";
cin >> OrderAmount;
//Here input should be a double.
//sleep(1);
cout << OrderAmount << " <-- OrderAmount" << endl;
WeightFactor1.push_back( OrderEntry*OrderAmount );
WeightFactor2 += OrderAmount;
cout << "weight factor: " << WeightFactor2 << endl;
cout << "Do you want to introduce more orders for the same pair? " << endl;
cout << "Please answer \"true\" or \"false\"."<< endl;
cin >> ConsoleInput;
//Here you should input "true" or "false".
DetermineAverage = ToBool(ConsoleInput);
//sleep(1);
if (DetermineAverage == false) {
double sum = 0.0;
for (int j = 0; j <= i ; j++){
sum+=WeightFactor1[j];
cout<<"sum equals: " << sum << " " << j << endl;
}
double result = sum/WeightFactor2;
cout<< "Average Price for " << WeightFactor2 << " units of " << PairOfAverage << " is: " << result << endl;
}
}
return 0;
}
//This is a function to parse the string to bool type.
//The error I get is the result of variable WeightFactor2. It does not add up
//the sum of the introduced valued the where passed as string through cin>>
//and then parsed to double.
//I hope you can help me on this.
Upvotes: 0
Reputation: 238421
The input stream has an overload for double
.
double value;
std::cin >> value;
There you go. This is the canonical way to convert standard input into a double
.
Upvotes: 0
Reputation: 144
Is there a reason for using conversion? You can read the input any primitive type you want, so you can directly read bool
or double
values such as cin >> OrderAmount
. No need for string conversion.
Upvotes: 1