0

Be gentle ... I'm 5 weeks into studying C++. I've dug and dug and cannot figure out why Visual Studio Express (and online compilers) are throwing errors about this.

Note that I've included all my declarations for the sake of clarity -- most are used in different section of code. The line that gets the errors is this one: newsharePrice = perchangeEnd * profitLoss << '\n';

The error I get is c2296, left operand has type double. I have no idea why it doesn't like this ... I multiply other doubles just fine.

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'; 
1
  • 1
    Remove the << '\n'. Commented Sep 25, 2015 at 13:15

2 Answers 2

3

Well, just read the problematic line:

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

That << '\n' is not part of the multiplication; a copy-pasta fail from your cout lines?

In this context, the compiler has no choice but to assume you're trying to perform a bitwise left-shift operation, which cannot be performed on doubles; only on integers.

Sign up to request clarification or add additional context in comments.

2 Comments

You know ... I can't say I know, but that fixed it. Thank you so much!
@AdamAdkins Then please mark this answer as accepted, by clicking on the tick on its left. On this site it's the best way to say "Thank you!".
0

While the compilation error is now fixed, your domain error is still there (today is Friday, isn't it?). Why would share price fluctuation affect your commission in any way? You already hace your position. You also measure your number of shares with floating-point precision. While in some cases you might have uneven number of shares, this happens quite seldom. Are you really account for this or just incorrectly use double? Most systmes would count number of shares as integer. Also, you can have negative position, which after all calctulations will give negative commission! Brokers would not agree to that ;). The last, but not the least, in US commission is rarely expressed as percentage of transaction value. It is usually charged in a form of cents per share (or fixed transaction cost for most retail brokers).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.