The program is supposed to read my file but for some reason it isn't working. The file is .rtf but .txt has the same effects. I checked to see if the file wasn't opening but it does.
Also, this is on a MAC, if that matters.
Thanks for the help again.
#include <iostream>
#include <fstream>
#define N 10
usingnamespace std;
void printStock (float [], int);
float sales (float [], int);
int main()
{
ifstream myfile;
myfile.open ("/Users/Soul/Documents/School/CISC/Assignment6/file.rtf");
float inv[N], cmt;
int acct, type;
printStock(inv,N);
myfile>>type>>acct>>cmt;
while(myfile){
if (type == 1){
inv[acct] += cmt;
cout << "Deposit: " << cmt;
}
elseif (type == 2){
inv[acct] -= cmt;
cout << "Withdrawal: " << cmt;
}
if (inv[acct] < cmt)
cout << "ERROR: Overdraw" << endl;
if (type != 1 && type != 2)
cout << "ERROR: Invalid Transation number" << endl;
}
myfile.close();
printStock(inv,N);
cout << "Total Income: " << sales(inv,N) << endl;
return 0;
}
void printStock(float inv[], int nelts){
cout << "Car Number Car Value" << endl;
for (int n = 0; n<nelts; n++)
cout << n << inv[n] << endl;
return;
}
float sales(float inv[], int nelts){
float sum = 0;
for (int n = 0; n<nelts; n++)
sum += inv[n];
return sum;
}
This is the output:
Last login: Mon Nov 10 17:07:55 on ttys000
/Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Soul/Documents/School/CISC/Assignment6/bin/Debug/Assignment6
Christians-MacBook-Pro:~ Soul$ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Soul/Documents/School/CISC/Assignment6/bin/Debug/Assignment6
Car Number Car Value
00
10
20
30
40
50
60
70
80
90
Car Number Car Value
00
10
20
30
40
50
60
70
80
90
Total Income: 0
Process returned 0 (0x0) execution time : 0.005 s
Press ENTER to continue.
Sorry
The file is to print the car number (numbers in the middle) and the current value via printStock.
Then if the transaction number (the first column) is a 1, adds to the value of the car number, which is what the array if for. If the transaction number is 2, it subtracts.
If the transaction number is neither, it rejects the transaction and if the number subtracted is higher than the value, it rejects it which prevent a negative value.
The it uses printStock again to give us the final values for each of the cars.