Hello SkytheWitcher,
inputdata.seekg(0, std::ios::beg);// I am not sure where to put this line.
To give you and idea:
100 125 150 175 200
^ file pointer here when file is opened.
100 125 150 175 200
^ file pointer here after first read.
100 125 150 175 200
^ file pointer here after second read.
100 125 150 175 200
^ file pointer here after third read.
100 125 150 175 200
^ file pointer here after forth read.
100 125 150 175 200
^ file pointer here after last read.
The next read will set the "EOF" bit on the file stream
because you are trying to read past "EOF".
|
Think about it. The use of
inputdata.seekg(0);
is when you are finished reading the file and need to start over. Considering that the input file will have more than two numbers in it your program will only read two numbers and not reach the end of the file. In this case using "seekg" will work. Should you reach the end of the file and the "eof" bit is set on the file stream you would have to use this:
1 2
|
inputdata.clear(); // <--- To reset the state bits on the stream.
inputdata.seekg(0); // <--- To set the file pointer to the beginning.
|
I still cannot get the output to go into my "TotalSold.txt" file |
This is because you need to close the "outputdata" stream before you open it with a new file name. Otherwise the new stream fails to open, so the "TotalSold.txt" is never created.
My professor does not seem to teach us the most efficient methods but perhaps it is all because we are all novices at this |
Been there, done that. I believe in the first class the idea is to teach the basics and leave the more advanced parts for the next classes. Some small part may be because you are a novice, but that means that you need to learn the basics first.
Back when I was taking classes my first programming class was in "Assembly language" for an IBM mainframe computer. The only thing he said during the entire ten weeks was "Read ahead. don't wait for me to lecture on what you need or you will not be able to finish your assignment". When it comes to your text book do not feel limited to the chapters or pages that you were told to read. At least look at the rest of the book and try to read what is not required. Also places like here are good sources for looking at the code of others and to ask questions.
In your code you have:
1 2 3 4 5 6 7 8
|
{
inputdata >> numberOfShares; //User enters data respectively
amountPaid = numberOfShares * PURCHASE_PRICE;
purchCom = COMMISSION_RATE * amountPaid;
totalPaid = amountPaid + purchCom;
outputdata << "The total paid is:" << totalPaid << endl;
}
|
You do not need the {}s to define a block for every section of code. Doing so limits the scope of the program to just that block and when the block ends so will that you may have created inside that block. This can actually work against you.
After looking at your program closer I realized some problems and feel this will work better:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include<iostream>
#include <iomanip>// <--- For the setprecision.
#include<fstream>
using namespace std; // <--- Best not to use.
int main()
{
ifstream inFile;
ofstream outFilePaid;
ofstream outFileSold;
const double COMMISSION_RATE = .02; // <--- Changed.
const double PURCHASE_PRICE = 32.87; // <--- Changed.
const double SELLING_PRICE = 33.92; // <--- Changed.
double amountPaid, purchCom, totalPaid,
stocksSoldFor, sellingCom, totalReceived, profitOrLoss;
int numberOfShares;
inFile.open("StocksBought.txt");
outFilePaid.open("TotalPaid.txt");
outFileSold.open("TotalSold.txt");
if (!inFile)
{
std::cout << "File \"StocksBought.txt\" did not open!" << std::endl;
return 1;
}
if (!outFilePaid)
{
std::cout << "File \"TotalPaid.txt\" has a problem!" << std::endl;
return 2;
}
if (!outFileSold)
{
std::cout << "File \"TotalSold.txt\" has a problem!" << std::endl;
return 3;
}
// <--- These need done only once.
outFilePaid << std::fixed << std::showpoint << std::setprecision(2);
outFileSold << std::fixed << std::showpoint << std::setprecision(2);
while (inFile >> numberOfShares)
{
//inFile >> numberOfShares; //User enters data respectively Reads from the file only once.
amountPaid = numberOfShares * PURCHASE_PRICE;
purchCom = COMMISSION_RATE * amountPaid;
totalPaid = amountPaid + purchCom;
stocksSoldFor = numberOfShares * SELLING_PRICE;
sellingCom = COMMISSION_RATE * stocksSoldFor;
totalReceived = stocksSoldFor - sellingCom;
outFilePaid << "The total paid is:" << totalPaid << endl;
outFileSold << "The total sold is:" << totalReceived << endl;
}
inFile.close();
outFilePaid.close();
outFileSold.close();
std::cout << "\n Processing finished." << std::endl;
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
|
The while loop will read everything that is in the file until the "eof" bit is set then the while condition will fail. Doing this will eliminate the need for "seekg" or having to close a file stream before opening it for a new file. And as line 50 implies that there will be more than one number in the input file. I used "100 125 150 175 200" for the "StocksBought.txt" file.
By combining the calculations as I did you can read one number, do all the calculations and write to the two output files before reading the next number and processing it.
Over time I have decided on calling the input file stream variable as
inFile
and output file stream variable as
outFile
. I found this easier to understand when writing and later reading the code. You may define the file streams anything you like, but it does help when you are consistent, i.e., you do not have to think as much.
The program produced these results:
The "TotalPaid.txt" contains:
The total paid is:3352.74
The total paid is:4190.93
The total paid is:5029.11
The total paid is:5867.30
The total paid is:6705.48
|
And "TotalSold.txt" contains:
The total sold is:3324.16
The total sold is:4155.20
The total sold is:4986.24
The total sold is:5817.28
The total sold is:6648.32
|
Hope that helps,
Andy