Hello poonamp6792,
When I tested your program this is what I found:
You are missing the include file "<string>".
The line
using namespace std;
should be avoided.
The struct is better defined like this:
1 2 3 4 5 6 7 8 9 10
|
struct StockInfo
{
//structure member variables
string coname;
int numShares{};
double PurPrice{};
double CurrPrice{};
double ProfitLoss{};
}; // Leave off "portfolio[10]" as it is defined in main.
|
The extra blank lines are not really needed, but OK. For floating point numbers it is better to use "double"s than "float"s. "double"s will store the number better and are less prone to the problems "float"s have. You also have more precision, i.e., more places to the right of the decimal pointnwith a double. Better for calculations.
In your proto types, that you call function declarations, the use of "struct" is not necessary. Just the type name, i.e., "StockInfo".
In main:
"struct" is not needed before "StockInfo" and "static" is not needed before "int count = 0; as main is the only place that it is changed.
Inside the do/while loop I started the first "cout" with a "\n" to break up the output for better readability.
Inside your if statement "count++;" should be the last line of the block so it will start with zero not one.
Hint: an if statement, for loop or while loop with only one line does not need the {} to form a block, but OK if you use them.
In option 3 using "exit(0);" works, but not the best way to end the program. What I like to do is define
bool cont{ true };
then in the end of the do /while loop say
while (cont);
. And for option 3 say
cont = false;
. You may find this more useful in the future.
Lastly in the "displayRecord" function I added this line at the beginning:
std::cout << std::fixed << std::showpoint << std::setprecision(2);
. The "fixed" says to use a regular decimal number not scientific notation. The " std::showpoint" will print ".0" in the output. And the "std::setprecision(2)" tells it how many numbers to print to the right of the decimal point. This only needs to be done once and will stay this way until changed or the program ends.
In the for loop you have
i<= count
this should be
i < count
. The "=" causes one to many elements of the array to be used and will cause the loop to go one past the end of the array.
In the "cout" statement the "setw(15),which I changed to 16, needs to go before what it is controling. Putting it after " portfolio[i].coname" has no effect. This is what I did for a quick fix to your "cout" statement:
cout << std::left << setw(16) << portfolio[i].coname << "$" << std::right << setw(5) << ProfitLossoss << endl;
I added the "std::left" and "std::right so the output would come out correctlu on my system. These may not be needed for your system.
In the end initializing the variables in the struct and any others may eliminate your problem because uninitialized you are using whatever is at the memory location when that variable is defined and whatever is there is turned into a large number that yo do not want. On my computer this us usually a vary large negative number.
Hope this helps,
Andy