fork download
  1. //
  2. // main.cpp
  3. // AccountsReceivable
  4. //
  5. // Created by Moshe Berman on 10/11/12.
  6. // Copyright (c) 2012 Moshe Berman. All rights reserved.
  7. //
  8. // This is the first assignment for Professor Lowenthal's Data Structures class.
  9. //
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13. #include <stdexcept>
  14.  
  15. #include "Record.h"
  16. #include "Customer.h"
  17.  
  18. using namespace std;
  19.  
  20. //
  21. // Function prototypes
  22. //
  23.  
  24. bool openInputStream(ifstream &, string);
  25. bool openOutputStream(ofstream &, string);
  26. void readNCharactersFromStreamIntoArray(ifstream &, int, char*);
  27.  
  28. int main() {
  29. //
  30. // Some variables to work with files
  31. //
  32. string transactionFileName = "transactionAccountsReceivable";
  33. string masterFileName = "masterAccountsReceivable";
  34. ifstream transactionFileStream;
  35. ofstream masterFileStream;
  36. //
  37. // Variables for processing records
  38. //
  39. Record record = Record();
  40. Customer customer = Customer();
  41. //
  42. // Get the transaction type
  43. //
  44. char transactionType;
  45. //
  46. // Open the file streams
  47. //
  48. if(!openInputStream(transactionFileStream, transactionFileName)) {
  49. cerr << "Can't open file " << transactionFileName << ". " << '\n';
  50. return 1;
  51. }
  52. if(!openOutputStream(masterFileStream, masterFileName)) {
  53. cerr << "Can't open file " << masterFileName << ". " << '\n';
  54. return 1;
  55. }
  56. //
  57. // Process the transactions
  58. //
  59. while (transactionFileStream.good()) {
  60. if (!(transactionFileStream >> transactionType)) {
  61. throw std::runtime_error(std::string("Error reading transactionType"));
  62. }
  63. //
  64. // Store the appropriate record type for later.
  65. // I read it here because of the design of the
  66. // file format.
  67. //
  68. if (transactionType == 'P') {
  69. record.type = Payment;
  70. } else if(transactionType == 'O') {
  71. record.type = Order;
  72. }
  73. //
  74. // Get the customer number next
  75. //
  76. if (!(transactionFileStream >> customer.customerNumber)) {
  77. throw std::runtime_error(std::string("Error reading customerNumber"));
  78. }
  79. //
  80. // Process a payment record
  81. //
  82. if (record.type == Payment) {
  83. if (!(transactionFileStream >> record.cashAmount)) {
  84. throw std::runtime_error(std::string("Error reading cashAmount"));
  85. }
  86. }
  87. //
  88. // Process an Order record
  89. //
  90. else if (record.type == Order) {
  91. readNCharactersFromStreamIntoArray(transactionFileStream, 20, record.itemName);
  92. if (!(transactionFileStream >> record.itemQuantity)) {
  93. throw std::runtime_error(std::string("Error reading itemQuantity"));
  94. }
  95. if (!(transactionFileStream >> record.cashAmount)) {
  96. throw std::runtime_error(std::string("Error reading cashAmount"));
  97. }
  98. }
  99. //
  100. // We need to put the item into the master file
  101. //
  102. record.description();
  103. customer.description();
  104. }
  105. return 0;
  106. }
  107.  
  108. //
  109. // Opens an input stream
  110. //
  111. bool openInputStream(ifstream &stream, string fileName) {
  112. stream.open(fileName.c_str());
  113. if (!stream.good()) {
  114. return false;
  115. }
  116. return true;
  117. }
  118.  
  119.  
  120. //
  121. // Opens an output stream
  122. //
  123.  
  124. bool openOutputStream(ofstream &stream, string fileName) {
  125. stream.open(fileName.c_str());
  126. if (!stream.good()) {
  127. return false;
  128. }
  129. return true;
  130. }
  131.  
  132. void readNCharactersFromStreamIntoArray(ifstream &stream, int n, char*characters) {
  133. for (int i = 0; i<n; i++) {
  134. if (!(stream >> characters[n])) {
  135. return;
  136. }
  137. }
  138. }
  139.  
  140.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:15:20: error: Record.h: No such file or directory
prog.cpp:16:22: error: Customer.h: No such file or directory
prog.cpp: In function ‘int main()’:
prog.cpp:39: error: ‘Record’ was not declared in this scope
prog.cpp:39: error: expected `;' before ‘record’
prog.cpp:40: error: ‘Customer’ was not declared in this scope
prog.cpp:40: error: expected `;' before ‘customer’
prog.cpp:69: error: ‘record’ was not declared in this scope
prog.cpp:69: error: ‘Payment’ was not declared in this scope
prog.cpp:71: error: ‘record’ was not declared in this scope
prog.cpp:71: error: ‘Order’ was not declared in this scope
prog.cpp:76: error: ‘customer’ was not declared in this scope
prog.cpp:82: error: ‘record’ was not declared in this scope
prog.cpp:82: error: ‘Payment’ was not declared in this scope
prog.cpp:90: error: ‘Order’ was not declared in this scope
prog.cpp:102: error: ‘record’ was not declared in this scope
prog.cpp:103: error: ‘customer’ was not declared in this scope
stdout
Standard output is empty