Strange variable outputs. "1.45667e+.009"

I'm new to all of this and am having trouble with a odd outputs of numbers. The program itself is meant to take user input for stock information, generate a random number from 1-100 as a percentage of change, use a coin flip to determine whether or not that change is positive or negative and output the results. Everything up to the break shown works properly (at least it seems to) but any output regarding the change of stock or any value it effects is showing like this

"1.45667e+.009"

I've probably left something out or placed something in the wrong spot that I just can't seem to figure out.


[code]
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <ctime>

using namespace std;
string stockName;
string stockSymbol;
int shares = 0;
double sharePrice = 0;
int commissionRate = 12;
int flip = 0;
int profitLoss = 0;


int main() {

//INPUT
cout << "Enter company name: ";
getline(cin, stockName);
cout << "nEnter the stock symbol: ";
getline(cin, stockSymbol);
cout << "Enter the number of shares you wish to buy: ";
cin >> shares;
cout << "What is the price per share?: ";
cin >> sharePrice;

//PROCESS

double shareCost = sharePrice*shares;
double commission = shareCost*commissionRate;
double totalCost = shareCost + commission;

*******************************************************************************

unsigned int seed = time(0);
srand(seed);
int stockChange = (rand() % 99) + 1;
double stockChange = seed / 100;
flip = rand() % 2 + 1;
if (flip == 1)
profitLoss = 1;
else
profitLoss = -1;
double percentGain = profitLoss*stockChange;
double newSharePrice = (percentGain*sharePrice) + sharePrice;
double newShareTotal = newSharePrice*shares;
double saleCommission = commissionRate*newShareTotal;
double totalProfit = (newShareTotal - saleCommission) - totalCost;
double returnRate = totalProfit / totalCost;

//OUTPUT

cout << "\n\nCost of shares: $" << shareCost;
cout << "\nCommission Rate: " << commissionRate;
cout << "\nCommission Charge: $" << commission;
cout << "\nTotal Cost of Purchase: $" << totalCost;
cout << "\nThe simulated Profit/Loss of your shares is : " << percentGain << "%";
cout << "\n\nYour new share price is: $" << newSharePrice;
cout << "\nThe new value of your stock is: $" << newShareTotal;
cout << "\n\nThe Profit/Loss of your stock is: $" << totalProfit;
cout << "\nThe return rate of your stock is: " << returnRate << "%";

cout << "\n\nPress Enter to exit........";

cin.ignore(1000, '\n');
cin.get();
return 0;
}
Hey =) You almost got the code tags right, you need to end it with a [/code].

1
2
int stockChange = (rand() % 99) + 1;
double stockChange = seed / 100;

You can't have two variables with the same name.
redacted
Last edited on
Would this be the proper way to change it?


unsigned int seed = time(0);
srand(seed);
int change = (rand() % 99) + 1;
double stockChange = change / 100;
@Lothcarn good change, just needs a tweak -

double stockChange = change / 100;
is a big nono. This is integer division, never ends well. change it to
double stockChange = change / 100.0; // note 100.0

The reason you're getting numbers like - "1.45667e+.009" is just because there's a lot of decimals. Read this thread and see if you can fix the program to fit your needs -
http://www.cplusplus.com/forum/general/35482/
Last edited on
Topic archived. No new replies allowed.