使用"表达式的新手错误必须具有整数或枚举类型"

时间:2015-09-25 13:14:19

标签: c++

要温柔......我在学习C ++的5周时间。我挖掘和挖掘,无法弄清楚为什么Visual Studio Express(和在线编译器)会抛出错误。

请注意,为了清楚起见,我已将所有声明包含在内 - 大多数声明都用在代码的不同部分。得到错误的行是这一行:newsharePrice = perchangeEnd * profitLoss << '\n';

我得到的错误是c2296,左操作数是double类型。我没有想法为什么它不喜欢这个......我将其他双打倍增。

double numberShares,
    sharePrice,
     profitLoss,
    profitGain,
    commissionPaid,
    commissionCost,
    sharesCost,
    totalshareCost,
    newtotalshareCost,
    newcommissionCost,
    newsharePrice;
double perChange;
double perchangeEnd;

const int minVALUE = 1;
const int maxVALUE = 100;
int seed = time(0);
srand (seed);
perChange = (rand() % (maxVALUE - minVALUE + 1)) + minVALUE;
cout << perChange << '\n';
perchangeEnd = perChange / 100;

int flip = rand() % 2 + 1;
if (flip == 1)
    profitLoss = 1;
else
    profitLoss = -1;

newsharePrice = perchangeEnd * profitLoss << '\n';
newsharePrice = newsharePrice + sharePrice;
cout << newsharePrice << '\n'; 
newtotalshareCost = numberShares * newsharePrice;
cout << "You've now paid " << newtotalshareCost << " for your shares." << '\n';
newcommissionCost = newtotalshareCost * commissionRate;
cout << "The new amount of commission for this is " << newcommissionCost << " ." << '/n';

2 个答案:

答案 0 :(得分:3)

好吧,请阅读有问题的一行:

   newsharePrice = perchangeEnd * profitLoss << '\n';
//                                          ▲▲▲▲▲▲▲▲

<< '\n'不是乘法的一部分;你的cout行没有复制意大利面?

在这种情况下,编译器别无选择,只能假设您正在尝试执行按位左移操作,这不能在double上执行;只对整数。

答案 1 :(得分:0)

虽然现在修复了编译错误,但您的域错误仍然存​​在(今天是星期五,不是吗?)。为什么股价波动会以任何方式影响您的佣金?你已经占据了你的位置。您还可以使用浮点精度来衡量共享的数量。虽然在某些情况下您的股票数量可能不均匀,但这种情况很少发生。你真的是这个或只是错误地使用双重?大多数系统会将股数计为整数。此外,你可以有负面的位置,这毕竟会产生负面的佣金!经纪人不会同意;)。美国佣金中的最后一个,但并非最不重要,很少表示为交易价值的百分比。它通常以每股美分的形式收取(或大多数零售经纪人的固定交易成本)。