Input/Output file problem

"John Doe wants to earn more money by investing in the stock market. In order to play it safe, he employs the diversity investment plan; investing in different things rather than putting all his eggs in one basket. Therefore, he will buy shares from three different stocks each day when the stock market opens. As the stock market closes, he will sell some of his shares. This might not be the best move, but this is John's strategy. He records his transactions in a file using the following format:

Stock's_Name
Buying_Cost Buying_commission_percentage number_of_shares_bought
Selling_cost Selling_commission_percentage number_of_shares_sold

e.g.,:
Google (GOOG)
522.01 2 100
520.66 1.5 80

Your task is to write a program that will help John analyze his investments. And, your program must perform the following tasks:

1. Prompt John for the name of his input file because he might use a different file for each day to help him keep track of all his transactions (see pg. 300, and other sources)
2. Prompt John for the name of his desired output file because he might use a different file for each day to keep track of his profit (see pg. 300, and other sources)
3. Compute the profit of each stock of the day by producing the following information all in one line for each stock into the output file

a. Stock Name (20 spaces, left justified)
b. Buying Cost (15 spaces, right justified)
c. Buying Commission (15 spaces, right justified)
d. Selling Amount (15 spaces, right justified)
e. Selling Commission (15 spaces, right justified)
f. Profit (15 spaces, right justified)
4. Once all stock data is processed, your system must display (in the output file) the following information (each in a separate line):
"

and this post keeps not posting so I won't waste anymore time typing anything else

Thanks for all help
I don't want anyone to do this fully for me I just don't understand how input output files work
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
40
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream inputFile;
    ofstream outputFile;
    
    string stock_name;
    double buying_Cost, buying_commission_percentage, selling_cost, selling_commission_percentage;
    int number_of_sales_bought, number_of_sales_sold, number;
    
    cout << "Enter the input filename: ";
    cin >> stock_name;
    
    inputFile.open(stock_name.c_str());
    
    if (inputFile)
    {
        while(inputFile >> number)
            cout << number << endl;
        
        inputFile.close();
    }
    else
    {
        cout << "Error opening the file.\n";
    }
    
    cout << setw(10) << "This" << setw(10) << "is" << setw(10) << "a" << setw(10) << "test" << endl;
    cin.get();
    
    
}


When I run the program I type "this.txt" (which is a .txt file i have on my desktop that reads "100") but it just says error opening files
bump
Are you sure? When I compiled and ran your program it quite happily output 100, then "This is a test" in response to a pre-prepared file this.txt containing "100".

List the file directory from which you are running the program and check that there is a non-blank file file with this name. Also, check that the file that you thought had "100" in hasn't been corrupted by some previous non-running draft of the program.

You do repeat the import of header <string>, but that wouldn't cause your problems.
Topic archived. No new replies allowed.