c++ code help

I have a problem with the output of my code again. This time it doesn't show negative loss. I'm supposed to input this values:

Acme Software, Inc.
100
25
25

With my current code, the output is this:

Company: Acme Software, Inc.
Shares: 100

Purchase/share: $25.00
Cost of stock: $2500.00
Cost of commission: $62.50
Total cost: $2562.50

Sale/share: $25.00
Income from stock: $2500.00
Cost of commission: $62.50
Total income: $2437.50

Gain or loss: $125.00

**The problem is I can't get the Gain or loss: $125.00 to be Gain or loss: $-125.00. It needs to be negative number. This is what the output should be below:

Company: Acme Software, Inc.
Shares: 100

Purchase/share: $25.00
Cost of stock: $2500.00
Cost of commission: $62.50
Total cost: $2562.50

Sale/share: $25.00
Income from stock: $2500.00
Cost of commission: $62.50
Total income: $2437.50

Gain or loss: $-125.00

*Here is my coding:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
string company_name;
getline(cin , company_name); //input name of company
string tmp;
double number_of_shares;
getline(cin , tmp); //input number of shares
number_of_shares = atof(tmp.c_str());
double purchase_price_per_share;
getline(cin, tmp); //input purchase price
purchase_price_per_share = atof(tmp.c_str());
double saling_price_per_share;
getline(cin,tmp); //input saling price
saling_price_per_share = atof(tmp.c_str());

cout << "Company: " << company_name << endl;
cout << "Shares: " << number_of_shares << endl;
cout<<endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
cout << "Purchase/share: $" << purchase_price_per_share << endl;
cout << "Cost of stock: $" << purchase_price_per_share*number_of_shares << endl;

cout << "Cost of commission: $" << purchase_price_per_share*number_of_shares*0.025 << endl;
const double Total_purchase = purchase_price_per_share*number_of_shares + purchase_price_per_share*number_of_shares*0.025;
cout << "Total cost: $" << Total_purchase << endl;
cout<<endl;
cout << "Sale/share: $" << saling_price_per_share <<endl;
cout << "Income from stock: $" << saling_price_per_share*number_of_shares << endl;
cout << "Cost of commission: $" << saling_price_per_share*number_of_shares*0.025 <<endl;
const double Total_income = saling_price_per_share*number_of_shares - saling_price_per_share*number_of_shares*0.025;
cout << "Total income: $" << Total_income << endl;
cout<<endl;

cout << "Gain or loss: $" << abs(Total_purchase - Total_income) << endl;
return 0;
}



*I really need help with this for the second time, so if anyone can correct my code to make the output look like it's supposed to, I would be grateful. Thanks!



Last edited on
Hello bal160730,

Your first problem is a lack of header files. You are missing "iomanip", for setprecision, and "string",for getline. The header file "bits/stdc++.h" I do not have this file in my include directory nor do I know what it is for.

OK I checked into "bits/stdc++.h" and my question would be, why include all the header files you do not use?, it is a waste of space and inflates your code for no reason. I Will show you a shorter version after my dinner.

When I ran the program all I saw was a blank screen waiting for something and I had no idea what to enter. Try something like this:

1
2
3
string company_name;
std::cout << "\n Enter Companyname: ";  // <--- Added.
getline(cin, company_name); //input name of company 


Once I entered the right header files it worked.

Hope that helps,

Andy
Hello bal160730,

I find this to be useful, but without all the extra header files you do not need.

stdhead
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef _STD_HEAD_
#define _STD_HEAD_

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>  //  std::setw(), std::fixed, std::setprecision, std::showpoint.
#include <limits>
#include <thread>  // For sleep time code
#include <chrono>  // For sleep time code

#endif // !_STD_HEAD_ 


This is the basic, I have some other code that I use that is not needed here.

I put this file in the normal include directory so I can use the <> around the name.

The last two files that say "for sleep time code" use this line:
std::this_thread::sleep_for(std::chrono::seconds()); // Requires header files "chrono" and "thread"
Fill in the number of seconds in the ().

The limits header file is for:std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
This is quite useful after "std::cin >> variable;" because the "\n" is left in the input buffer and needs to be cleared before a "std::getline()" which will get the "\n" from the input buffer first and nothing from what you want the input to come from, e.g., cin or a file.

After I include, what I call "stdhead" I add any header files that are needed for that program.

Watch header files like "bits/stdc++.h", Not everyone will have this file or want to use it.

Hope that helps,

Andy
closed account (48T7M4Gy)
At line 37, pseudocode:

1
2
3
4
if abs(...)<0
    print "loss"
else
    print "gain"
Thanks you!
Topic archived. No new replies allowed.