#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;
//在这里你应该输入&#34; true&#34;或&#34; false&#34;。
bool DetermineAverage = ToBool(ConsoleInput);
cout << "Please enter the currency pair Symbol in XXXYYY format:";
cin >> PairOfAverage;
//在这里你应该输入一个字符串。
for (int i=0;DetermineAverage==true;i++) {
cout << "Please enter the order's entry price: ";
cin >> inputString;
//这里的输入应该是双倍的。
OrderEntry = stod (inputString,NULL);
cout << "Please enter the order's amount: ";
cin >> inputString;
//这里的输入应该是双倍的。
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;
//在这里你应该输入&#34; true&#34;或&#34; false&#34;。
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;
}
//这是一个将字符串解析为bool类型的函数。
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.
答案 0 :(得分:1)
是否有使用转换的原因?您可以读取所需的任何基本类型的输入,因此您可以直接读取bool
或double
值,例如cin >> OrderAmount
。无需字符串转换。
答案 1 :(得分:0)
答案 2 :(得分:0)
你需要纠正你对double WeightFactor1 [] = {}的使用来向量&lt;双&gt; 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.